From 5290beb23edfe817397901bac835d86a7a2f9599 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 8 May 2020 11:59:10 +0200 Subject: [PATCH] Update cli.go --- command/cli.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/command/cli.go b/command/cli.go index 7b81998a9..02ec5973f 100644 --- a/command/cli.go +++ b/command/cli.go @@ -2,11 +2,14 @@ package command import ( "flag" + "fmt" "math" + "strings" "github.com/hashicorp/packer/helper/enumflag" kvflag "github.com/hashicorp/packer/helper/flag-kv" sliceflag "github.com/hashicorp/packer/helper/flag-slice" + "github.com/hashicorp/packer/packer" ) // NewMetaArgs parses cli args and put possible values @@ -17,12 +20,38 @@ func (ma *MetaArgs) AddFlagSets(fs *flag.FlagSet) { fs.Var((*kvflag.StringSlice)(&ma.VarFiles), "var-file", "") } +// ConfigType tells what type of config we should use, it can return values +// like "hcl" or "json". +// Make sure Args was correctly set before. +func (ma *MetaArgs) ConfigType() (string, error) { + switch len(ma.Args) { + // TODO(azr): in the future, I want to allow passing multiple arguments to + // merge HCL confs together; but this will probably need an RFC first. + // TODO(azr): To allow piping HCL2 confs (when args is "-"), we probably + // will need to add a setting that says "this is an HCL config". + case 1: + name := ma.Args[0] + if strings.HasSuffix(name, ".pkr.hcl") || + strings.HasSuffix(name, ".pkr.json") { + return "hcl", nil + } + isDir, err := isDir(name) + if isDir { + return "hcl", err + } + return "json", err + default: + return "", fmt.Errorf("packer only takes on argument: %q", ma.Args) + } +} + // MetaArgs defines commonalities between all comands type MetaArgs struct { Args []string Only, Except []string Vars map[string]string VarFiles []string + Ui packer.Ui } func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) { @@ -42,6 +71,7 @@ func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) { func (ba *BuildArgs) ParseArgvs(args []string) int { flags := flag.NewFlagSet("build", flag.ContinueOnError) + flags.Usage = func() { ba.Ui.Say(ba.Help()) } ba.AddFlagSets(flags) err := flags.Parse(args) if err != nil { @@ -66,6 +96,7 @@ type BuildArgs struct { func (ca *ConsoleArgs) ParseArgvs(args []string) int { flags := flag.NewFlagSet("console", flag.ContinueOnError) + flags.Usage = func() { ca.Ui.Say(ca.Help()) } ca.AddFlagSets(flags) err := flags.Parse(args) if err != nil { @@ -86,7 +117,8 @@ func (fa *FixArgs) AddFlagSets(flags *flag.FlagSet) { } func (fa *FixArgs) ParseArgvs(args []string) int { - flags := flag.NewFlagSet("build", flag.ContinueOnError) + flags := flag.NewFlagSet("fix", flag.ContinueOnError) + flags.Usage = func() { fa.Ui.Say(fa.Help()) } fa.AddFlagSets(flags) err := flags.Parse(args) if err != nil { @@ -110,7 +142,8 @@ func (va *ValidateArgs) AddFlagSets(flags *flag.FlagSet) { } func (va *ValidateArgs) ParseArgvs(args []string) int { - flags := flag.NewFlagSet("build", flag.ContinueOnError) + flags := flag.NewFlagSet("validate", flag.ContinueOnError) + flags.Usage = func() { va.Ui.Say(va.Help()) } va.AddFlagSets(flags) err := flags.Parse(args) if err != nil {