mirror of https://github.com/hashicorp/terraform
Merge pull request #27865 from hashicorp/alisdair/command-views-refresh
cli: Migrate refresh to command viewspull/27864/head
commit
eeb5dfbf76
@ -0,0 +1,61 @@
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/tfdiags"
|
||||
)
|
||||
|
||||
// Refresh represents the command-line arguments for the apply command.
|
||||
type Refresh struct {
|
||||
// State, Operation, and Vars are the common extended flags
|
||||
State *State
|
||||
Operation *Operation
|
||||
Vars *Vars
|
||||
|
||||
// InputEnabled is used to disable interactive input for unspecified
|
||||
// variable and backend config values. Default is true.
|
||||
InputEnabled bool
|
||||
|
||||
// ViewType specifies which output format to use
|
||||
ViewType ViewType
|
||||
}
|
||||
|
||||
// ParseRefresh processes CLI arguments, returning a Refresh value and errors.
|
||||
// If errors are encountered, a Refresh value is still returned representing
|
||||
// the best effort interpretation of the arguments.
|
||||
func ParseRefresh(args []string) (*Refresh, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
refresh := &Refresh{
|
||||
State: &State{},
|
||||
Operation: &Operation{},
|
||||
Vars: &Vars{},
|
||||
}
|
||||
|
||||
cmdFlags := extendedFlagSet("refresh", refresh.State, refresh.Operation, refresh.Vars)
|
||||
cmdFlags.BoolVar(&refresh.InputEnabled, "input", true, "input")
|
||||
|
||||
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) > 0 {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Too many command line arguments",
|
||||
"Expected at most one positional argument.",
|
||||
))
|
||||
}
|
||||
|
||||
diags = diags.Append(refresh.Operation.Parse())
|
||||
|
||||
switch {
|
||||
default:
|
||||
refresh.ViewType = ViewHuman
|
||||
}
|
||||
|
||||
return refresh, diags
|
||||
}
|
||||
@ -0,0 +1,173 @@
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
)
|
||||
|
||||
func TestParseRefresh_basicValid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *Refresh
|
||||
}{
|
||||
"defaults": {
|
||||
nil,
|
||||
&Refresh{
|
||||
InputEnabled: true,
|
||||
ViewType: ViewHuman,
|
||||
},
|
||||
},
|
||||
"input=flase": {
|
||||
[]string{"-input=false"},
|
||||
&Refresh{
|
||||
InputEnabled: false,
|
||||
ViewType: ViewHuman,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseRefresh(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
// Ignore the extended arguments for simplicity
|
||||
got.State = nil
|
||||
got.Operation = nil
|
||||
got.Vars = nil
|
||||
if *got != *tc.want {
|
||||
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_invalid(t *testing.T) {
|
||||
got, diags := ParseRefresh([]string{"-frob"})
|
||||
if len(diags) == 0 {
|
||||
t.Fatal("expected diags but got none")
|
||||
}
|
||||
if got, want := diags.Err().Error(), "flag provided but not defined"; !strings.Contains(got, want) {
|
||||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want)
|
||||
}
|
||||
if got.ViewType != ViewHuman {
|
||||
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_tooManyArguments(t *testing.T) {
|
||||
got, diags := ParseRefresh([]string{"saved.tfplan"})
|
||||
if len(diags) == 0 {
|
||||
t.Fatal("expected diags but got none")
|
||||
}
|
||||
if got, want := diags.Err().Error(), "Too many command line arguments"; !strings.Contains(got, want) {
|
||||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want)
|
||||
}
|
||||
if got.ViewType != ViewHuman {
|
||||
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_targets(t *testing.T) {
|
||||
foobarbaz, _ := addrs.ParseTargetStr("foo_bar.baz")
|
||||
boop, _ := addrs.ParseTargetStr("module.boop")
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want []addrs.Targetable
|
||||
wantErr string
|
||||
}{
|
||||
"no targets by default": {
|
||||
args: nil,
|
||||
want: nil,
|
||||
},
|
||||
"one target": {
|
||||
args: []string{"-target=foo_bar.baz"},
|
||||
want: []addrs.Targetable{foobarbaz.Subject},
|
||||
},
|
||||
"two targets": {
|
||||
args: []string{"-target=foo_bar.baz", "-target", "module.boop"},
|
||||
want: []addrs.Targetable{foobarbaz.Subject, boop.Subject},
|
||||
},
|
||||
"invalid traversal": {
|
||||
args: []string{"-target=foo."},
|
||||
want: nil,
|
||||
wantErr: "Dot must be followed by attribute name",
|
||||
},
|
||||
"invalid target": {
|
||||
args: []string{"-target=data[0].foo"},
|
||||
want: nil,
|
||||
wantErr: "A data source name is required",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseRefresh(tc.args)
|
||||
if len(diags) > 0 {
|
||||
if tc.wantErr == "" {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
|
||||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, tc.wantErr)
|
||||
}
|
||||
}
|
||||
if !cmp.Equal(got.Operation.Targets, tc.want) {
|
||||
t.Fatalf("unexpected result\n%s", cmp.Diff(got.Operation.Targets, tc.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_vars(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want []FlagNameValue
|
||||
}{
|
||||
"no var flags by default": {
|
||||
args: nil,
|
||||
want: nil,
|
||||
},
|
||||
"one var": {
|
||||
args: []string{"-var", "foo=bar"},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var", Value: "foo=bar"},
|
||||
},
|
||||
},
|
||||
"one var-file": {
|
||||
args: []string{"-var-file", "cool.tfvars"},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var-file", Value: "cool.tfvars"},
|
||||
},
|
||||
},
|
||||
"ordering preserved": {
|
||||
args: []string{
|
||||
"-var", "foo=bar",
|
||||
"-var-file", "cool.tfvars",
|
||||
"-var", "boop=beep",
|
||||
},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var", Value: "foo=bar"},
|
||||
{Name: "-var-file", Value: "cool.tfvars"},
|
||||
{Name: "-var", Value: "boop=beep"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseRefresh(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
|
||||
t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want))
|
||||
}
|
||||
if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want {
|
||||
t.Fatalf("expected Empty() to return %t, but was %t", want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform/command/arguments"
|
||||
"github.com/hashicorp/terraform/states"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/hashicorp/terraform/tfdiags"
|
||||
)
|
||||
|
||||
// The Refresh view is used for the refresh command.
|
||||
type Refresh interface {
|
||||
Outputs(outputValues map[string]*states.OutputValue)
|
||||
|
||||
Operation() Operation
|
||||
Hooks() []terraform.Hook
|
||||
|
||||
Diagnostics(diags tfdiags.Diagnostics)
|
||||
HelpPrompt(command string)
|
||||
}
|
||||
|
||||
// NewRefresh returns an initialized Refresh implementation for the given ViewType.
|
||||
func NewRefresh(vt arguments.ViewType, runningInAutomation bool, view *View) Refresh {
|
||||
switch vt {
|
||||
case arguments.ViewHuman:
|
||||
return &RefreshHuman{
|
||||
View: *view,
|
||||
inAutomation: runningInAutomation,
|
||||
countHook: &countHook{},
|
||||
}
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown view type %v", vt))
|
||||
}
|
||||
}
|
||||
|
||||
// The RefreshHuman implementation renders human-readable text logs, suitable for
|
||||
// a scrolling terminal.
|
||||
type RefreshHuman struct {
|
||||
View
|
||||
|
||||
inAutomation bool
|
||||
|
||||
countHook *countHook
|
||||
}
|
||||
|
||||
var _ Refresh = (*RefreshHuman)(nil)
|
||||
|
||||
func (v *RefreshHuman) Outputs(outputValues map[string]*states.OutputValue) {
|
||||
if len(outputValues) > 0 {
|
||||
v.streams.Print(v.colorize.Color("[reset][bold][green]\nOutputs:\n\n"))
|
||||
NewOutput(arguments.ViewHuman, &v.View).Output("", outputValues)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *RefreshHuman) Operation() Operation {
|
||||
return NewOperation(arguments.ViewHuman, v.inAutomation, &v.View)
|
||||
}
|
||||
|
||||
func (v *RefreshHuman) Hooks() []terraform.Hook {
|
||||
return []terraform.Hook{
|
||||
v.countHook,
|
||||
NewUiHook(&v.View),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/command/arguments"
|
||||
"github.com/hashicorp/terraform/internal/terminal"
|
||||
"github.com/hashicorp/terraform/states"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
// Ensure that the correct view type and in-automation settings propagate to the
|
||||
// Operation view.
|
||||
func TestRefreshHuman_operation(t *testing.T) {
|
||||
streams, done := terminal.StreamsForTesting(t)
|
||||
defer done(t)
|
||||
v := NewRefresh(arguments.ViewHuman, true, NewView(streams)).Operation()
|
||||
if hv, ok := v.(*OperationHuman); !ok {
|
||||
t.Fatalf("unexpected return type %t", v)
|
||||
} else if hv.inAutomation != true {
|
||||
t.Fatalf("unexpected inAutomation value on Operation view")
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that Hooks includes a UI hook
|
||||
func TestRefreshHuman_hooks(t *testing.T) {
|
||||
streams, done := terminal.StreamsForTesting(t)
|
||||
defer done(t)
|
||||
v := NewRefresh(arguments.ViewHuman, true, NewView(streams))
|
||||
hooks := v.Hooks()
|
||||
|
||||
var uiHook *UiHook
|
||||
for _, hook := range hooks {
|
||||
if ch, ok := hook.(*UiHook); ok {
|
||||
uiHook = ch
|
||||
}
|
||||
}
|
||||
if uiHook == nil {
|
||||
t.Fatalf("expected Hooks to include a UiHook: %#v", hooks)
|
||||
}
|
||||
}
|
||||
|
||||
// Basic test coverage of Outputs, since most of its functionality is tested
|
||||
// elsewhere.
|
||||
func TestRefreshHuman_outputs(t *testing.T) {
|
||||
streams, done := terminal.StreamsForTesting(t)
|
||||
v := NewRefresh(arguments.ViewHuman, false, NewView(streams))
|
||||
|
||||
v.Outputs(map[string]*states.OutputValue{
|
||||
"foo": {Value: cty.StringVal("secret")},
|
||||
})
|
||||
|
||||
got := done(t).Stdout()
|
||||
for _, want := range []string{"Outputs:", `foo = "secret"`} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("wrong result\ngot: %q\nwant: %q", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Outputs should do nothing if there are no outputs to render.
|
||||
func TestRefreshHuman_outputsEmpty(t *testing.T) {
|
||||
streams, done := terminal.StreamsForTesting(t)
|
||||
v := NewRefresh(arguments.ViewHuman, false, NewView(streams))
|
||||
|
||||
v.Outputs(map[string]*states.OutputValue{})
|
||||
|
||||
got := done(t).Stdout()
|
||||
if got != "" {
|
||||
t.Errorf("output should be empty, but got: %q", got)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue