terraform/command
Alisdair McDiarmid 4991cc4835 cli: Improve error for invalid -target flags
Errors encountered when parsing flags for apply, plan, and refresh were
being suppressed. This resulted in a generic usage error when using an
invalid `-target` flag.

This commit makes several changes to address this. First, these commands
now output the flag parse error before exiting, leaving at least some
hint about the error. You can verify this manually with something like:

    terraform apply -invalid-flag

We also change how target attributes are parsed, moving the
responsibility from the flags instance to the command. This allows us to
customize the diagnostic output to be more user friendly. The
diagnostics now look like:

```shellsession
$ terraform apply -no-color -target=foo

Error: Invalid target "foo"

Resource specification must include a resource type and name.
```

Finally, we add test coverage for both parsing of target flags, and at
the command level for successful use of resource targeting. These tests
focus on the UI output (via the change summary and refresh logs), as the
functionality of targeting is covered by the context tests in the
terraform package.
2021-02-08 13:48:04 -05:00
..
cliconfig Grammar nit: "setup" as a verb should be spelled "set up" 2021-01-26 20:39:11 +01:00
clistate move remaining helper packages to internal 2021-01-20 13:54:00 -05:00
e2etest command: Use different warning messaging in init command and move above potentially confusing output (#27514) 2021-02-01 10:50:08 -05:00
format command: no visual warning hierarchy in -no-color 2021-01-27 09:26:53 -08:00
jsonconfig Ensure depends_on is in module calls for config 2020-08-20 07:49:03 +10:00
jsonplan command/jsonconfig: Use correct provider address to access schema 2020-04-06 09:24:23 -07:00
jsonprovider command/jsonprovider: staticcheck 2020-12-02 13:59:20 -05:00
jsonstate command: Fix terraform show not outputting child_modules properly in certain circumstances (#27352) 2021-01-11 12:31:20 -05:00
testdata cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
webbrowser command: Revert local patch for pkg/browser 2020-12-10 09:27:02 -05:00
012_config_upgrade.go staticcheck 2020-12-02 13:59:19 -05:00
012_config_upgrade_test.go command/0.13upgrade: Remove this subcommand 2020-09-29 10:00:35 -07:00
013_config_upgrade.go staticcheck 2020-12-02 13:59:19 -05:00
013_config_upgrade_test.go command/0.13upgrade: Remove this subcommand 2020-09-29 10:00:35 -07:00
apply.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
apply_destroy_test.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
apply_test.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
autocomplete.go
autocomplete_test.go
cli_ui.go
cli_ui_test.go
command.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
command_test.go Grammar nit: "setup" as a verb should be spelled "set up" 2021-01-26 20:39:11 +01:00
console.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
console_interactive.go move remaining helper packages to internal 2021-01-20 13:54:00 -05:00
console_interactive_solaris.go
console_test.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
flag_kv.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
flag_kv_test.go
fmt.go command: Improve consistency of the command short descriptions 2020-10-26 09:55:21 -07:00
fmt_test.go command: remove unused method on pluginSHA256LockFile (#26402) 2020-09-29 08:46:51 -04:00
get.go command: Improve consistency of the command short descriptions 2020-10-26 09:55:21 -07:00
get_test.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
graph.go cli: Remove positional plan argument from graph 2021-02-02 13:21:26 -05:00
graph_test.go cli: Remove positional plan argument from graph 2021-02-02 13:21:26 -05:00
hook_count.go cli: Move resource count code to command package 2021-01-29 15:29:35 -05:00
hook_count_test.go cli: Move resource count code to command package 2021-01-29 15:29:35 -05:00
hook_module_install.go
hook_ui.go command: Remove stubbed-out apply diff output code 2020-07-22 14:19:00 -04:00
hook_ui_test.go Merge pull request #18823 from minamijoyo/fix-multibyte-trucate 2019-08-06 14:24:24 -04:00
import.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
import_test.go update MockProvider usage 2021-01-12 17:47:55 -05:00
init.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
init_test.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
login.go Add scopes support to password grant type 2020-09-15 19:32:41 -04:00
login_test.go Add command/login test for scopes 2020-09-15 19:30:48 -04:00
logout.go command: Simplify Meta.process helper method 2020-04-01 15:01:08 -04:00
logout_test.go command/logout: Add terraform logout command 2020-02-06 15:00:55 -05:00
meta.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
meta_backend.go Grammar nit: "setup" as a verb should be spelled "set up" 2021-01-26 20:39:11 +01:00
meta_backend_migrate.go Grammar nit: "setup" as a verb should be spelled "set up" 2021-01-26 20:39:11 +01:00
meta_backend_test.go command staticcheck 2020-12-02 13:59:19 -05:00
meta_config.go remove unused code (#26503) 2020-10-07 11:00:06 -04:00
meta_dependencies.go command/init: Read, respect, and update provider dependency locks 2020-10-09 09:26:23 -07:00
meta_new.go prune dead code from command/meta 2019-07-18 16:11:41 -04:00
meta_providers.go command: Use different warning messaging in init command and move above potentially confusing output (#27514) 2021-02-01 10:50:08 -05:00
meta_test.go command staticcheck 2020-12-02 13:59:19 -05:00
meta_vars.go vendor: switch to HCL 2.0 in the HCL repository 2019-10-02 15:10:21 -07:00
output.go cli: Remove dead code for output -module flag 2021-01-27 14:02:59 -05:00
output_test.go cli: Remove dead code for output -module flag 2021-01-27 14:02:59 -05:00
plan.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
plan_test.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
plugins.go run built-in provisioners in-process 2020-12-02 12:45:00 -05:00
plugins_lock.go command: remove unused method on pluginSHA256LockFile (#26402) 2020-09-29 08:46:51 -04:00
plugins_lock_test.go command: remove unused method on pluginSHA256LockFile (#26402) 2020-09-29 08:46:51 -04:00
plugins_test.go command: Make the tests compile again 2020-04-06 09:24:23 -07:00
providers.go backend: Validate remote backend Terraform version 2020-11-19 13:19:40 -05:00
providers_lock.go command: Improve consistency of the command short descriptions 2020-10-26 09:55:21 -07:00
providers_mirror.go command: Improve consistency of the command short descriptions 2020-10-26 09:55:21 -07:00
providers_schema.go backend: Validate remote backend Terraform version 2020-11-19 13:19:40 -05:00
providers_schema_test.go deprecate helper/copy 2020-10-08 08:42:16 -04:00
providers_test.go internal: Fix lockfile constraint output for 1.2.* 2020-10-20 10:14:03 -04:00
push.go
refresh.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
refresh_test.go cli: Improve error for invalid -target flags 2021-02-08 13:48:04 -05:00
show.go backend/local: Use terminal properties to tweak the plan output 2021-01-13 15:37:04 -08:00
show_test.go update MockProvider usage 2021-01-12 17:47:55 -05:00
state_command.go
state_list.go command staticcheck 2020-12-02 13:59:19 -05:00
state_list_test.go command/state list: list resources in nested and expanded modules (#27268) 2020-12-14 11:07:15 -05:00
state_meta.go command/state list: list resources in nested and expanded modules (#27268) 2020-12-14 11:07:15 -05:00
state_mv.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
state_mv_test.go add missing import 2021-01-27 09:54:28 -08:00
state_pull.go docs: Note: state pull upgrades to current version 2021-01-25 10:31:11 -05:00
state_pull_test.go deprecate helper/copy 2020-10-08 08:42:16 -04:00
state_push.go backend: Validate remote backend Terraform version 2020-11-19 13:19:40 -05:00
state_push_test.go Grammar nit: "setup" as a verb should be spelled "set up" 2021-01-26 20:39:11 +01:00
state_replace_provider.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
state_replace_provider_test.go vendor: Upgrade mitchellh/cli 2020-09-10 15:23:02 -04:00
state_rm.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
state_rm_test.go deprecate helper/copy 2020-10-08 08:42:16 -04:00
state_show.go command staticcheck 2020-12-02 13:59:19 -05:00
state_show_test.go update MockProvider usage 2021-01-12 17:47:55 -05:00
state_test.go
taint.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
taint_test.go command: no visual warning hierarchy in -no-color 2021-01-27 09:26:53 -08:00
ui_input.go Exit with error if UI input scan fails (#26509) 2020-10-21 14:10:06 -04:00
ui_input_test.go Exit with error if UI input scan fails (#26509) 2020-10-21 14:10:06 -04:00
unlock.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
unlock_test.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
untaint.go Update docstring for ignore-remote-version 2021-01-27 15:40:00 -05:00
untaint_test.go command: no visual warning hierarchy in -no-color 2021-01-27 09:26:53 -08:00
validate.go command: Use different warning messaging in init command and move above potentially confusing output (#27514) 2021-02-01 10:50:08 -05:00
validate_test.go update MockProvider usage 2021-01-12 17:47:55 -05:00
version.go Remove revision from version command 2021-01-12 16:35:30 -05:00
version_test.go Remove revision from version command 2021-01-12 16:35:30 -05:00
workspace_command.go command staticcheck 2020-12-02 13:59:19 -05:00
workspace_command_test.go remove legacy types from command package 2020-12-02 12:33:18 -05:00
workspace_delete.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
workspace_list.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
workspace_new.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
workspace_select.go cli: Remove legacy positional path arguments 2021-02-02 13:21:26 -05:00
workspace_show.go command: Always validate workspace name 2020-08-11 12:33:12 -04:00