Update cli.go

This commit is contained in:
Adrien Delorme 2020-05-08 11:59:10 +02:00
parent a5ab87ca44
commit 5290beb23e
1 changed files with 35 additions and 2 deletions

View File

@ -2,11 +2,14 @@ package command
import ( import (
"flag" "flag"
"fmt"
"math" "math"
"strings"
"github.com/hashicorp/packer/helper/enumflag" "github.com/hashicorp/packer/helper/enumflag"
kvflag "github.com/hashicorp/packer/helper/flag-kv" kvflag "github.com/hashicorp/packer/helper/flag-kv"
sliceflag "github.com/hashicorp/packer/helper/flag-slice" sliceflag "github.com/hashicorp/packer/helper/flag-slice"
"github.com/hashicorp/packer/packer"
) )
// NewMetaArgs parses cli args and put possible values // 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", "") 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 // MetaArgs defines commonalities between all comands
type MetaArgs struct { type MetaArgs struct {
Args []string Args []string
Only, Except []string Only, Except []string
Vars map[string]string Vars map[string]string
VarFiles []string VarFiles []string
Ui packer.Ui
} }
func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) { 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 { func (ba *BuildArgs) ParseArgvs(args []string) int {
flags := flag.NewFlagSet("build", flag.ContinueOnError) flags := flag.NewFlagSet("build", flag.ContinueOnError)
flags.Usage = func() { ba.Ui.Say(ba.Help()) }
ba.AddFlagSets(flags) ba.AddFlagSets(flags)
err := flags.Parse(args) err := flags.Parse(args)
if err != nil { if err != nil {
@ -66,6 +96,7 @@ type BuildArgs struct {
func (ca *ConsoleArgs) ParseArgvs(args []string) int { func (ca *ConsoleArgs) ParseArgvs(args []string) int {
flags := flag.NewFlagSet("console", flag.ContinueOnError) flags := flag.NewFlagSet("console", flag.ContinueOnError)
flags.Usage = func() { ca.Ui.Say(ca.Help()) }
ca.AddFlagSets(flags) ca.AddFlagSets(flags)
err := flags.Parse(args) err := flags.Parse(args)
if err != nil { if err != nil {
@ -86,7 +117,8 @@ func (fa *FixArgs) AddFlagSets(flags *flag.FlagSet) {
} }
func (fa *FixArgs) ParseArgvs(args []string) int { 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) fa.AddFlagSets(flags)
err := flags.Parse(args) err := flags.Parse(args)
if err != nil { if err != nil {
@ -110,7 +142,8 @@ func (va *ValidateArgs) AddFlagSets(flags *flag.FlagSet) {
} }
func (va *ValidateArgs) ParseArgvs(args []string) int { 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) va.AddFlagSets(flags)
err := flags.Parse(args) err := flags.Parse(args)
if err != nil { if err != nil {