command/push: allow specifying a -name param for push target

This commit is contained in:
Jack Pearkes 2015-04-15 12:53:57 -07:00
parent b49d74d999
commit 344c741642
3 changed files with 52 additions and 2 deletions

View File

@ -34,6 +34,7 @@ type pushUploadFn func(
func (c *PushCommand) Run(args []string) int {
var token string
var message string
var name string
var create bool
f := flag.NewFlagSet("push", flag.ContinueOnError)
@ -41,6 +42,7 @@ func (c *PushCommand) Run(args []string) int {
f.StringVar(&token, "token", "", "token")
f.StringVar(&message, "m", "", "message")
f.StringVar(&message, "message", "", "message")
f.StringVar(&name, "name", "", "name")
f.BoolVar(&create, "create", false, "create (deprecated)")
if err := f.Parse(args); err != nil {
return 1
@ -65,11 +67,17 @@ func (c *PushCommand) Run(args []string) int {
return 1
}
// If we didn't pass name from the CLI, use the template
if name == "" {
name = tpl.Push.Name
}
// Validate some things
if tpl.Push.Name == "" {
if name == "" {
c.Ui.Error(fmt.Sprintf(
"The 'push' section must be specified in the template with\n" +
"at least the 'name' option set."))
"at least the 'name' option set. Alternatively, you can pass the\n" +
"name parameter from the CLI."))
return 1
}
@ -245,6 +253,9 @@ Options:
-m, -message=<detail> A message to identify the purpose or changes in this
Packer template much like a VCS commit message
-name=<name> The destination build in Atlas. This is in a format
"username/name".
-token=<token> The access token to use to when uploading
`

View File

@ -120,6 +120,42 @@ func TestPush_noName(t *testing.T) {
}
}
func TestPush_cliName(t *testing.T) {
var actual []string
var actualOpts *uploadOpts
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
actual = testArchive(t, r)
actualOpts = opts
doneCh := make(chan struct{})
close(doneCh)
return doneCh, nil, nil
}
c := &PushCommand{
Meta: testMeta(t),
uploadFn: uploadFn,
}
args := []string{
"-name=foo/bar",
filepath.Join(testFixture("push-no-name"), "template.json"),
}
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
expected := []string{
archiveTemplateEntry,
"template.json",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestPush_uploadError(t *testing.T) {
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
return nil, nil, fmt.Errorf("bad")

View File

@ -33,6 +33,9 @@ must be completed within the template.
service such as Atlas. This can also be specified within the push
configuration in the template.
* `-name` - The name of the build in the service. This typically
looks like `hashicorp/precise64`.
## Examples
Push a Packer template: