From ede76a85e0989d6b6f132e4d46441694652b7b5e Mon Sep 17 00:00:00 2001 From: hc-github-team-nomad-core <82989552+hc-github-team-nomad-core@users.noreply.github.com> Date: Tue, 12 Dec 2023 02:48:17 -0600 Subject: [PATCH] backport of commit 71ea1deda707b706c36da7567caba7721e51db19 (#19444) Co-authored-by: James Rasell --- .changelog/19423.txt | 3 +++ command/var_put.go | 7 +++++++ command/var_put_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 .changelog/19423.txt diff --git a/.changelog/19423.txt b/.changelog/19423.txt new file mode 100644 index 000000000..31ca52976 --- /dev/null +++ b/.changelog/19423.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: Fix a bug in the `var put` command which prevented combining items as CLI arguments and other parameters as flags +``` diff --git a/command/var_put.go b/command/var_put.go index 9a80dd7e6..4b12feb0e 100644 --- a/command/var_put.go +++ b/command/var_put.go @@ -393,6 +393,13 @@ func (c *VarPutCommand) makeVariable(path string) (*api.Variable, error) { return nil, fmt.Errorf("unknown format flag value") } + // It is possible a specification file was used which did not declare any + // items. Therefore, default the entry to avoid panics and ensure this type + // of use is valid. + if out.Items == nil { + out.Items = make(map[string]string) + } + // Handle cases where values are provided by CLI flags that modify the // the created variable. Typical of a "copy" operation, it is a convenience // to reset the Create and Modify metadata to zero. diff --git a/command/var_put_test.go b/command/var_put_test.go index 9c51c6eef..a14fa5af8 100644 --- a/command/var_put_test.go +++ b/command/var_put_test.go @@ -6,6 +6,7 @@ package command import ( "encoding/json" "fmt" + "os" "regexp" "strings" "testing" @@ -107,6 +108,40 @@ func TestVarPutCommand_GoodJson(t *testing.T) { require.Equal(t, api.VariableItems{"k1": "v1", "k2": "v2"}, outVar.Items) } +func TestVarPutCommand_FlagsWithSpec(t *testing.T) { + ci.Parallel(t) + + // Create a server + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := cli.NewMockUi() + cmd := &VarPutCommand{Meta: Meta{Ui: ui}} + + // Create a temporary file and ensure the proper cleanup is run once the + // test ends. + osFile, err := os.CreateTemp("", "nomad-cli-var-put-test-*.hcl") + must.NoError(t, err) + + t.Cleanup(func() { + _ = osFile.Close() + _ = os.Remove(osFile.Name()) + }) + + // Write out a partial spec that includes the namespace and variable path. + _, err = osFile.Write([]byte("path = \"path/to/variable\"\nnamespace = \"default\"")) + must.NoError(t, err) + + // Create the variables, ensure we clean it up, and check the command + // response. + code := cmd.Run([]string{"-address=" + url, "@" + osFile.Name(), "k1=v1", "k2=v2"}) + must.Zero(t, code) + + must.StrContains(t, ui.OutputWriter.String(), "path/to/variable") + must.StrContains(t, ui.OutputWriter.String(), "\"k1\": \"v1\"") + must.StrContains(t, ui.OutputWriter.String(), "\"k2\": \"v2\"") +} + func TestVarPutCommand_AutocompleteArgs(t *testing.T) { ci.Parallel(t) srv, client, url := testServer(t, true, nil)