From 52bbc57c62e0bcb49bcd59337ace3b27f7155baa Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Fri, 13 Feb 2026 14:43:54 +0100 Subject: [PATCH] refactor state-pull command argument parsing --- internal/command/arguments/state_pull.go | 32 ++++++++++ internal/command/arguments/state_pull_test.go | 64 +++++++++++++++++++ internal/command/state_pull.go | 7 +- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 internal/command/arguments/state_pull.go create mode 100644 internal/command/arguments/state_pull_test.go diff --git a/internal/command/arguments/state_pull.go b/internal/command/arguments/state_pull.go new file mode 100644 index 0000000000..caebef0c47 --- /dev/null +++ b/internal/command/arguments/state_pull.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package arguments + +import ( + "github.com/hashicorp/terraform/internal/tfdiags" +) + +// StatePull represents the command-line arguments for the state pull command. +type StatePull struct { +} + +// ParseStatePull processes CLI arguments, returning a StatePull value and +// diagnostics. If errors are encountered, a StatePull value is still returned +// representing the best effort interpretation of the arguments. +func ParseStatePull(args []string) (*StatePull, tfdiags.Diagnostics) { + var diags tfdiags.Diagnostics + pull := &StatePull{} + + cmdFlags := defaultFlagSet("state pull") + + if err := cmdFlags.Parse(args); err != nil { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Failed to parse command-line flags", + err.Error(), + )) + } + + return pull, diags +} diff --git a/internal/command/arguments/state_pull_test.go b/internal/command/arguments/state_pull_test.go new file mode 100644 index 0000000000..a1b95f5ec3 --- /dev/null +++ b/internal/command/arguments/state_pull_test.go @@ -0,0 +1,64 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package arguments + +import ( + "testing" + + "github.com/hashicorp/terraform/internal/tfdiags" +) + +func TestParseStatePull_valid(t *testing.T) { + testCases := map[string]struct { + args []string + want *StatePull + }{ + "defaults": { + nil, + &StatePull{}, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + got, diags := ParseStatePull(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 TestParseStatePull_invalid(t *testing.T) { + testCases := map[string]struct { + args []string + want *StatePull + wantDiags tfdiags.Diagnostics + }{ + "unknown flag": { + []string{"-boop"}, + &StatePull{}, + tfdiags.Diagnostics{ + tfdiags.Sourceless( + tfdiags.Error, + "Failed to parse command-line flags", + "flag provided but not defined: -boop", + ), + }, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + got, gotDiags := ParseStatePull(tc.args) + if *got != *tc.want { + t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want) + } + tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags) + }) + } +} diff --git a/internal/command/state_pull.go b/internal/command/state_pull.go index 781295170b..6eb6da85b5 100644 --- a/internal/command/state_pull.go +++ b/internal/command/state_pull.go @@ -21,10 +21,9 @@ type StatePullCommand struct { } func (c *StatePullCommand) Run(args []string) int { - args = c.Meta.process(args) - cmdFlags := c.Meta.defaultFlagSet("state pull") - if err := cmdFlags.Parse(args); err != nil { - c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) + _, diags := arguments.ParseStatePull(c.Meta.process(args)) + if diags.HasErrors() { + c.showDiagnostics(diags) return 1 }