2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2015-01-27 10:57:34 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// FlagType is the type of the build flag
|
2015-01-27 10:57:34 -05:00
|
|
|
type FlagType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
boolType FlagType = iota
|
|
|
|
stringType
|
|
|
|
)
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// BFlags contains all flags information for the builder
|
|
|
|
type BFlags struct {
|
2015-01-27 10:57:34 -05:00
|
|
|
Args []string // actual flags/args from cmd line
|
|
|
|
flags map[string]*Flag
|
|
|
|
used map[string]*Flag
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// Flag contains all information for a flag
|
2015-01-27 10:57:34 -05:00
|
|
|
type Flag struct {
|
2015-07-22 01:29:03 -04:00
|
|
|
bf *BFlags
|
2015-01-27 10:57:34 -05:00
|
|
|
name string
|
|
|
|
flagType FlagType
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2016-08-29 06:37:14 -04:00
|
|
|
// NewBFlags returns the new BFlags struct
|
2015-07-22 01:29:03 -04:00
|
|
|
func NewBFlags() *BFlags {
|
|
|
|
return &BFlags{
|
2015-01-27 10:57:34 -05:00
|
|
|
flags: make(map[string]*Flag),
|
|
|
|
used: make(map[string]*Flag),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
// NewBFlagsWithArgs returns the new BFlags struct with Args set to args
|
|
|
|
func NewBFlagsWithArgs(args []string) *BFlags {
|
|
|
|
flags := NewBFlags()
|
|
|
|
flags.Args = args
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// AddBool adds a bool flag to BFlags
|
2015-08-07 11:03:59 -04:00
|
|
|
// Note, any error will be generated when Parse() is called (see Parse).
|
2015-07-22 01:29:03 -04:00
|
|
|
func (bf *BFlags) AddBool(name string, def bool) *Flag {
|
2015-01-27 10:57:34 -05:00
|
|
|
flag := bf.addFlag(name, boolType)
|
|
|
|
if flag == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if def {
|
|
|
|
flag.Value = "true"
|
|
|
|
} else {
|
|
|
|
flag.Value = "false"
|
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// AddString adds a string flag to BFlags
|
2015-08-07 11:03:59 -04:00
|
|
|
// Note, any error will be generated when Parse() is called (see Parse).
|
2015-07-22 01:29:03 -04:00
|
|
|
func (bf *BFlags) AddString(name string, def string) *Flag {
|
2015-01-27 10:57:34 -05:00
|
|
|
flag := bf.addFlag(name, stringType)
|
|
|
|
if flag == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
flag.Value = def
|
|
|
|
return flag
|
|
|
|
}
|
|
|
|
|
2015-08-07 11:03:59 -04:00
|
|
|
// addFlag is a generic func used by the other AddXXX() func
|
|
|
|
// to add a new flag to the BFlags struct.
|
|
|
|
// Note, any error will be generated when Parse() is called (see Parse).
|
2015-07-22 01:29:03 -04:00
|
|
|
func (bf *BFlags) addFlag(name string, flagType FlagType) *Flag {
|
2015-01-27 10:57:34 -05:00
|
|
|
if _, ok := bf.flags[name]; ok {
|
|
|
|
bf.Err = fmt.Errorf("Duplicate flag defined: %s", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newFlag := &Flag{
|
|
|
|
bf: bf,
|
|
|
|
name: name,
|
|
|
|
flagType: flagType,
|
|
|
|
}
|
|
|
|
bf.flags[name] = newFlag
|
|
|
|
|
|
|
|
return newFlag
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// IsUsed checks if the flag is used
|
2015-01-27 10:57:34 -05:00
|
|
|
func (fl *Flag) IsUsed() bool {
|
|
|
|
if _, ok := fl.bf.used[fl.name]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// IsTrue checks if a bool flag is true
|
2015-01-27 10:57:34 -05:00
|
|
|
func (fl *Flag) IsTrue() bool {
|
|
|
|
if fl.flagType != boolType {
|
|
|
|
// Should never get here
|
|
|
|
panic(fmt.Errorf("Trying to use IsTrue on a non-boolean: %s", fl.name))
|
|
|
|
}
|
|
|
|
return fl.Value == "true"
|
|
|
|
}
|
|
|
|
|
2015-08-07 11:03:59 -04:00
|
|
|
// Parse parses and checks if the BFlags is valid.
|
|
|
|
// Any error noticed during the AddXXX() funcs will be generated/returned
|
|
|
|
// here. We do this because an error during AddXXX() is more like a
|
|
|
|
// compile time error so it doesn't matter too much when we stop our
|
|
|
|
// processing as long as we do stop it, so this allows the code
|
|
|
|
// around AddXXX() to be just:
|
2015-12-13 11:00:39 -05:00
|
|
|
// defFlag := AddString("description", "")
|
2015-08-07 11:03:59 -04:00
|
|
|
// w/o needing to add an if-statement around each one.
|
2015-07-22 01:29:03 -04:00
|
|
|
func (bf *BFlags) Parse() error {
|
2015-01-27 10:57:34 -05:00
|
|
|
// If there was an error while defining the possible flags
|
|
|
|
// go ahead and bubble it back up here since we didn't do it
|
|
|
|
// earlier in the processing
|
|
|
|
if bf.Err != nil {
|
|
|
|
return fmt.Errorf("Error setting up flags: %s", bf.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range bf.Args {
|
|
|
|
if !strings.HasPrefix(arg, "--") {
|
|
|
|
return fmt.Errorf("Arg should start with -- : %s", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg == "--" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = arg[2:]
|
|
|
|
value := ""
|
|
|
|
|
|
|
|
index := strings.Index(arg, "=")
|
|
|
|
if index >= 0 {
|
|
|
|
value = arg[index+1:]
|
|
|
|
arg = arg[:index]
|
|
|
|
}
|
|
|
|
|
|
|
|
flag, ok := bf.flags[arg]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unknown flag: %s", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok = bf.used[arg]; ok {
|
|
|
|
return fmt.Errorf("Duplicate flag specified: %s", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
bf.used[arg] = flag
|
|
|
|
|
|
|
|
switch flag.flagType {
|
|
|
|
case boolType:
|
|
|
|
// value == "" is only ok if no "=" was specified
|
|
|
|
if index >= 0 && value == "" {
|
|
|
|
return fmt.Errorf("Missing a value on flag: %s", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
lower := strings.ToLower(value)
|
|
|
|
if lower == "" {
|
|
|
|
flag.Value = "true"
|
|
|
|
} else if lower == "true" || lower == "false" {
|
|
|
|
flag.Value = lower
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Expecting boolean value for flag %s, not: %s", arg, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
case stringType:
|
|
|
|
if index < 0 {
|
|
|
|
return fmt.Errorf("Missing a value on flag: %s", arg)
|
|
|
|
}
|
|
|
|
flag.Value = value
|
|
|
|
|
|
|
|
default:
|
2016-12-25 01:37:31 -05:00
|
|
|
panic("No idea what kind of flag we have! Should never get here!")
|
2015-01-27 10:57:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|