diff --git a/command/push.go b/command/push.go index 55c76e1ab..2fe416099 100644 --- a/command/push.go +++ b/command/push.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/atlas-go/archive" "github.com/hashicorp/atlas-go/v1" + "github.com/mitchellh/packer/helper/flag-kv" "github.com/mitchellh/packer/template" ) @@ -188,6 +189,17 @@ func (c *PushCommand) Run(args []string) int { uploadOpts.Builds[b.Name] = info } + // Collect the variables from CLI args and any var files + uploadOpts.Vars = make(map[string]string) + if vs := f.Lookup("var"); vs != nil { + flags := vs.Value.(*kvflag.Flag) + vars := map[string]string(*flags) + + for k, v := range vars { + uploadOpts.Vars[k] = v + } + } + // Add the upload metadata metadata := make(map[string]interface{}) if message != "" { @@ -331,7 +343,7 @@ func (c *PushCommand) upload( // Start the upload doneCh, errCh := make(chan struct{}), make(chan error) go func() { - err := c.client.UploadBuildConfigVersion(&version, opts.Metadata, r, r.Size) + err := c.client.UploadBuildConfigVersion(&version, opts.Metadata, opts.Vars, r, r.Size) if err != nil { errCh <- err return @@ -348,6 +360,7 @@ type uploadOpts struct { Slug string Builds map[string]*uploadBuildInfo Metadata map[string]interface{} + Vars map[string]string } type uploadBuildInfo struct { diff --git a/command/push_test.go b/command/push_test.go index 2a37381f2..8d1ebf39b 100644 --- a/command/push_test.go +++ b/command/push_test.go @@ -205,16 +205,24 @@ func TestPush_vars(t *testing.T) { args := []string{ "-var", "name=foo/bar", + "-var", "one=two", filepath.Join(testFixture("push-vars"), "template.json"), } if code := c.Run(args); code != 0 { fatalCommand(t, c.Meta) } - expected := "foo/bar" - if actualOpts.Slug != expected { + if actualOpts.Slug != "foo/bar" { t.Fatalf("bad: %#v", actualOpts.Slug) } + + expected := map[string]string{ + "name": "foo/bar", + "one": "two", + } + if !reflect.DeepEqual(actualOpts.Vars, expected) { + t.Fatalf("bad: %#v", actualOpts.Vars) + } } func testArchive(t *testing.T, r io.Reader) []string {