mirror of https://github.com/hashicorp/terraform
parent
da74b59ea1
commit
f7cde006ff
@ -0,0 +1,54 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
// StateShow represents the command-line arguments for the state show command.
|
||||
type StateShow struct {
|
||||
// StatePath is an optional path to a state file, overriding the default.
|
||||
StatePath string
|
||||
|
||||
// Address is the resource instance address to show.
|
||||
Address string
|
||||
}
|
||||
|
||||
// ParseStateShow processes CLI arguments, returning a StateShow value and
|
||||
// diagnostics. If errors are encountered, a StateShow value is still returned
|
||||
// representing the best effort interpretation of the arguments.
|
||||
func ParseStateShow(args []string) (*StateShow, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
show := &StateShow{}
|
||||
|
||||
var statePath string
|
||||
cmdFlags := defaultFlagSet("state show")
|
||||
cmdFlags.StringVar(&statePath, "state", "", "path")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
err.Error(),
|
||||
))
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
if len(args) != 1 {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Required argument missing",
|
||||
"Exactly one argument expected: the address of a resource instance to show.",
|
||||
))
|
||||
}
|
||||
|
||||
show.StatePath = statePath
|
||||
|
||||
if len(args) > 0 {
|
||||
show.Address = args[0]
|
||||
}
|
||||
|
||||
return show, diags
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
func TestParseStateShow_valid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *StateShow
|
||||
}{
|
||||
"address only": {
|
||||
[]string{"test_instance.foo"},
|
||||
&StateShow{
|
||||
StatePath: "",
|
||||
Address: "test_instance.foo",
|
||||
},
|
||||
},
|
||||
"with state path": {
|
||||
[]string{"-state=foobar.tfstate", "test_instance.foo"},
|
||||
&StateShow{
|
||||
StatePath: "foobar.tfstate",
|
||||
Address: "test_instance.foo",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseStateShow(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if *got != *tc.want {
|
||||
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStateShow_invalid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *StateShow
|
||||
wantDiags tfdiags.Diagnostics
|
||||
}{
|
||||
"no arguments": {
|
||||
nil,
|
||||
&StateShow{
|
||||
StatePath: "",
|
||||
Address: "",
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Required argument missing",
|
||||
"Exactly one argument expected: the address of a resource instance to show.",
|
||||
),
|
||||
},
|
||||
},
|
||||
"too many arguments": {
|
||||
[]string{"test_instance.foo", "test_instance.bar"},
|
||||
&StateShow{
|
||||
StatePath: "",
|
||||
Address: "test_instance.foo",
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Required argument missing",
|
||||
"Exactly one argument expected: the address of a resource instance to show.",
|
||||
),
|
||||
},
|
||||
},
|
||||
"unknown flag": {
|
||||
[]string{"-boop"},
|
||||
&StateShow{
|
||||
StatePath: "",
|
||||
Address: "",
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
"flag provided but not defined: -boop",
|
||||
),
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Required argument missing",
|
||||
"Exactly one argument expected: the address of a resource instance to show.",
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, gotDiags := ParseStateShow(tc.args)
|
||||
if *got != *tc.want {
|
||||
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
|
||||
}
|
||||
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue