2013-07-15 02:16:30 +00:00
|
|
|
package cli
|
|
|
|
|
2013-12-07 15:10:04 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-03-31 07:24:15 +00:00
|
|
|
"io/ioutil"
|
2016-05-01 12:36:17 +00:00
|
|
|
"reflect"
|
2016-02-09 16:36:13 +00:00
|
|
|
"runtime"
|
2013-12-07 15:10:04 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-09-12 03:32:43 +00:00
|
|
|
"syscall"
|
2014-08-02 21:32:32 +00:00
|
|
|
"time"
|
2013-12-07 15:10:04 +00:00
|
|
|
)
|
2013-07-15 02:16:30 +00:00
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
const defaultPlaceholder = "value"
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
2017-05-06 03:07:18 +00:00
|
|
|
var BashCompletionFlag Flag = BoolFlag{
|
2016-05-01 12:36:17 +00:00
|
|
|
Name: "generate-bash-completion",
|
|
|
|
Hidden: true,
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
2014-04-12 13:32:53 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// VersionFlag prints the version for the application
|
2017-05-06 03:07:18 +00:00
|
|
|
var VersionFlag Flag = BoolFlag{
|
2014-07-11 22:13:10 +00:00
|
|
|
Name: "version, v",
|
|
|
|
Usage: "print the version",
|
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// HelpFlag prints the help for all commands and subcommands
|
2014-12-02 04:20:21 +00:00
|
|
|
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
|
|
|
|
// unless HideHelp is set to true)
|
2017-05-06 03:07:18 +00:00
|
|
|
var HelpFlag Flag = BoolFlag{
|
2014-07-11 22:13:10 +00:00
|
|
|
Name: "help, h",
|
|
|
|
Usage: "show help",
|
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// FlagStringer converts a flag definition to a string. This is used by help
|
|
|
|
// to display a flag.
|
2016-05-02 17:07:57 +00:00
|
|
|
var FlagStringer FlagStringFunc = stringifyFlag
|
2016-05-02 17:05:21 +00:00
|
|
|
|
2017-08-26 11:42:25 +00:00
|
|
|
// FlagNamePrefixer converts a full flag name and its placeholder into the help
|
|
|
|
// message flag prefix. This is used by the default FlagStringer.
|
|
|
|
var FlagNamePrefixer FlagNamePrefixFunc = prefixedNames
|
|
|
|
|
|
|
|
// FlagEnvHinter annotates flag help message with the environment variable
|
|
|
|
// details. This is used by the default FlagStringer.
|
|
|
|
var FlagEnvHinter FlagEnvHintFunc = withEnvHint
|
|
|
|
|
2017-04-10 14:45:51 +00:00
|
|
|
// FlagFileHinter annotates flag help message with the environment variable
|
|
|
|
// details. This is used by the default FlagStringer.
|
|
|
|
var FlagFileHinter FlagFileHintFunc = withFileHint
|
|
|
|
|
2016-10-13 14:54:57 +00:00
|
|
|
// FlagsByName is a slice of Flag.
|
|
|
|
type FlagsByName []Flag
|
|
|
|
|
|
|
|
func (f FlagsByName) Len() int {
|
|
|
|
return len(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FlagsByName) Less(i, j int) bool {
|
2017-10-28 07:00:11 +00:00
|
|
|
return lexicographicLess(f[i].GetName(), f[j].GetName())
|
2016-10-13 14:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FlagsByName) Swap(i, j int) {
|
|
|
|
f[i], f[j] = f[j], f[i]
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Flag is a common interface related to parsing flags in cli.
|
2016-02-09 16:36:13 +00:00
|
|
|
// For more advanced flag parsing techniques, it is recommended that
|
2013-11-01 14:31:37 +00:00
|
|
|
// this interface be implemented.
|
2013-07-15 02:16:30 +00:00
|
|
|
type Flag interface {
|
2013-07-19 00:39:42 +00:00
|
|
|
fmt.Stringer
|
2013-11-01 14:31:37 +00:00
|
|
|
// Apply Flag settings to the given flag set
|
2013-07-19 00:39:42 +00:00
|
|
|
Apply(*flag.FlagSet)
|
2015-12-15 17:29:03 +00:00
|
|
|
GetName() string
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// errorableFlag is an interface that allows us to return errors during apply
|
|
|
|
// it allows flags defined in this library to return errors in a fashion backwards compatible
|
|
|
|
// TODO remove in v2 and modify the existing Flag interface to return errors
|
|
|
|
type errorableFlag interface {
|
|
|
|
Flag
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
ApplyWithError(*flag.FlagSet) error
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
|
2013-07-24 14:35:45 +00:00
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
2013-09-24 01:41:31 +00:00
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
for _, f := range flags {
|
2016-09-17 23:54:29 +00:00
|
|
|
//TODO remove in v2 when errorableFlag is removed
|
2016-11-17 19:48:03 +00:00
|
|
|
if ef, ok := f.(errorableFlag); ok {
|
2016-11-14 00:15:05 +00:00
|
|
|
if err := ef.ApplyWithError(set); err != nil {
|
2016-09-17 23:54:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.Apply(set)
|
|
|
|
}
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
2016-09-17 23:54:29 +00:00
|
|
|
return set, nil
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
func eachName(longName string, fn func(string)) {
|
|
|
|
parts := strings.Split(longName, ",")
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
fn(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
// Generic is a generic parseable type identified by a specific flag
|
|
|
|
type Generic interface {
|
|
|
|
Set(value string) error
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2015-01-08 18:58:34 +00:00
|
|
|
// Apply takes the flagset and calls Set on the generic flag with the value
|
|
|
|
// provided by the user for parsing by the flag
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores parsing errors
|
2014-04-15 14:16:47 +00:00
|
|
|
func (f GenericFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError takes the flagset and calls Set on the generic flag with the value
|
2016-09-17 23:54:29 +00:00
|
|
|
// provided by the user for parsing by the flag
|
2016-11-14 00:15:05 +00:00
|
|
|
func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := f.Value
|
2017-10-26 18:08:03 +00:00
|
|
|
if fileEnvVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
if err := val.Set(fileEnvVal); err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as value for flag %s: %s", fileEnvVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2014-04-15 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// StringSlice is an opaque type for []string to satisfy flag.Value and flag.Getter
|
2013-09-24 01:41:31 +00:00
|
|
|
type StringSlice []string
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Set appends the string value to the list of values
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) Set(value string) error {
|
|
|
|
*f = append(*f, value)
|
2013-09-24 01:41:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) String() string {
|
|
|
|
return fmt.Sprintf("%s", *f)
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Value returns the slice of strings set by this flag
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) Value() []string {
|
|
|
|
return *f
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// Get returns the slice of strings set by this flag
|
|
|
|
func (f *StringSlice) Get() interface{} {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-09-24 01:41:31 +00:00
|
|
|
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f StringSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
newVal := &StringSlice{}
|
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if err := newVal.Set(s); err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as string value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
f.Value = newVal
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-05-31 20:50:23 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = &StringSlice{}
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// IntSlice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
2013-09-24 19:36:01 +00:00
|
|
|
type IntSlice []int
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) Set(value string) error {
|
|
|
|
tmp, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-09 14:15:05 +00:00
|
|
|
*f = append(*f, tmp)
|
2013-09-24 19:36:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) String() string {
|
2016-06-16 14:14:28 +00:00
|
|
|
return fmt.Sprintf("%#v", *f)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Value returns the slice of ints set by this flag
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) Value() []int {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// Get returns the slice of ints set by this flag
|
|
|
|
func (f *IntSlice) Get() interface{} {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f IntSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
newVal := &IntSlice{}
|
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if err := newVal.Set(s); err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as int slice value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
f.Value = newVal
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-05-31 20:50:23 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = &IntSlice{}
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// Int64Slice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
2016-06-12 04:54:33 +00:00
|
|
|
type Int64Slice []int64
|
|
|
|
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
|
func (f *Int64Slice) Set(value string) error {
|
|
|
|
tmp, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*f = append(*f, tmp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
|
|
|
func (f *Int64Slice) String() string {
|
2016-06-16 14:14:28 +00:00
|
|
|
return fmt.Sprintf("%#v", *f)
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the slice of ints set by this flag
|
|
|
|
func (f *Int64Slice) Value() []int64 {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2016-09-17 23:54:29 +00:00
|
|
|
// Get returns the slice of ints set by this flag
|
|
|
|
func (f *Int64Slice) Get() interface{} {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2016-06-12 04:54:33 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2016-06-12 04:54:33 +00:00
|
|
|
func (f Int64SliceFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
newVal := &Int64Slice{}
|
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if err := newVal.Set(s); err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as int64 slice value for flag %s: %s", envVal, f.Name, err)
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
f.Value = newVal
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = &Int64Slice{}
|
|
|
|
}
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
return nil
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f BoolFlag) ApplyWithError(set *flag.FlagSet) error {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := false
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
if envVal == "" {
|
|
|
|
val = false
|
|
|
|
} else {
|
|
|
|
envValBool, err := strconv.ParseBool(envVal)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
val = envValBool
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
set.Bool(name, val, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2014-03-06 01:24:22 +00:00
|
|
|
func (f BoolTFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f BoolTFlag) ApplyWithError(set *flag.FlagSet) error {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := true
|
2016-09-12 03:32:43 +00:00
|
|
|
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
if envVal == "" {
|
|
|
|
val = false
|
|
|
|
} else {
|
|
|
|
envValBool, err := strconv.ParseBool(envVal)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
val = envValBool
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 21:39:38 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
set.Bool(name, val, f.Usage)
|
2014-03-06 01:24:22 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2014-03-06 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-07-17 02:59:04 +00:00
|
|
|
func (f StringFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f StringFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
f.Value = envVal
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.String(name, f.Value, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f IntFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f IntFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
f.Value = int(envValInt)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.IntVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Int(name, f.Value, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:22:07 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2016-06-11 22:22:07 +00:00
|
|
|
func (f Int64Flag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f Int64Flag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err)
|
2016-06-11 22:22:07 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
f.Value = envValInt
|
2016-06-11 22:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
if f.Destination != nil {
|
|
|
|
set.Int64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Int64(name, f.Value, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2016-06-11 22:22:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:13:32 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2016-06-16 15:13:32 +00:00
|
|
|
func (f UintFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f UintFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err)
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
f.Value = uint(envValInt)
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
if f.Destination != nil {
|
|
|
|
set.UintVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Uint(name, f.Value, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2016-06-16 15:13:32 +00:00
|
|
|
func (f Uint64Flag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f Uint64Flag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as uint64 value for flag %s: %s", envVal, f.Name, err)
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
f.Value = uint64(envValInt)
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
if f.Destination != nil {
|
|
|
|
set.Uint64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Uint64(name, f.Value, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2014-08-02 21:32:32 +00:00
|
|
|
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f DurationFlag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValDuration, err := time.ParseDuration(envVal)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as duration for flag %s: %s", envVal, f.Name, err)
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
f.Value = envValDuration
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.DurationVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-08-02 21:32:32 +00:00
|
|
|
set.Duration(name, f.Value, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-09-17 23:54:29 +00:00
|
|
|
// Ignores errors
|
2013-12-03 13:42:09 +00:00
|
|
|
func (f Float64Flag) Apply(set *flag.FlagSet) {
|
2016-11-14 00:15:05 +00:00
|
|
|
f.ApplyWithError(set)
|
2016-09-17 23:54:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 00:15:05 +00:00
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
func (f Float64Flag) ApplyWithError(set *flag.FlagSet) error {
|
2017-03-31 07:24:15 +00:00
|
|
|
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
|
|
envValFloat, err := strconv.ParseFloat(envVal, 10)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s as float64 value for flag %s: %s", envVal, f.Name, err)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
f.Value = float64(envValFloat)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.Float64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-12-03 13:42:09 +00:00
|
|
|
set.Float64(name, f.Value, f.Usage)
|
|
|
|
})
|
2016-09-17 23:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:36:17 +00:00
|
|
|
func visibleFlags(fl []Flag) []Flag {
|
|
|
|
visible := []Flag{}
|
|
|
|
for _, flag := range fl {
|
2017-05-06 03:07:18 +00:00
|
|
|
field := flagValue(flag).FieldByName("Hidden")
|
|
|
|
if !field.IsValid() || !field.Bool() {
|
2016-05-01 12:36:17 +00:00
|
|
|
visible = append(visible, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visible
|
2015-03-15 10:11:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 18:51:46 +00:00
|
|
|
func prefixFor(name string) (prefix string) {
|
|
|
|
if len(name) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
} else {
|
|
|
|
prefix = "--"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2013-11-20 08:05:18 +00:00
|
|
|
|
2016-04-22 03:02:06 +00:00
|
|
|
// Returns the placeholder, if any, and the unquoted usage string.
|
|
|
|
func unquoteUsage(usage string) (string, string) {
|
|
|
|
for i := 0; i < len(usage); i++ {
|
|
|
|
if usage[i] == '`' {
|
|
|
|
for j := i + 1; j < len(usage); j++ {
|
|
|
|
if usage[j] == '`' {
|
|
|
|
name := usage[i+1 : j]
|
|
|
|
usage = usage[:i] + name + usage[j+1:]
|
|
|
|
return name, usage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", usage
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefixedNames(fullName, placeholder string) string {
|
|
|
|
var prefixed string
|
2013-11-20 08:05:18 +00:00
|
|
|
parts := strings.Split(fullName, ",")
|
|
|
|
for i, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
prefixed += prefixFor(name) + name
|
2016-04-22 03:02:06 +00:00
|
|
|
if placeholder != "" {
|
|
|
|
prefixed += " " + placeholder
|
|
|
|
}
|
2013-11-21 01:25:13 +00:00
|
|
|
if i < len(parts)-1 {
|
2013-11-20 08:05:18 +00:00
|
|
|
prefixed += ", "
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 03:02:06 +00:00
|
|
|
return prefixed
|
2013-11-20 08:05:18 +00:00
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
|
|
|
|
func withEnvHint(envVar, str string) string {
|
|
|
|
envText := ""
|
|
|
|
if envVar != "" {
|
2016-01-22 14:08:27 +00:00
|
|
|
prefix := "$"
|
|
|
|
suffix := ""
|
|
|
|
sep := ", $"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
prefix = "%"
|
|
|
|
suffix = "%"
|
|
|
|
sep = "%, %"
|
|
|
|
}
|
|
|
|
envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(strings.Split(envVar, ","), sep), suffix)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
return str + envText
|
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
|
2017-04-10 14:45:51 +00:00
|
|
|
func withFileHint(filePath, str string) string {
|
|
|
|
fileText := ""
|
|
|
|
if filePath != "" {
|
|
|
|
fileText = fmt.Sprintf(" [%s]", filePath)
|
|
|
|
}
|
|
|
|
return str + fileText
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:34:44 +00:00
|
|
|
func flagValue(f Flag) reflect.Value {
|
2016-05-02 17:05:21 +00:00
|
|
|
fv := reflect.ValueOf(f)
|
2016-06-08 14:34:44 +00:00
|
|
|
for fv.Kind() == reflect.Ptr {
|
|
|
|
fv = reflect.Indirect(fv)
|
|
|
|
}
|
|
|
|
return fv
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyFlag(f Flag) string {
|
|
|
|
fv := flagValue(f)
|
2016-05-02 23:42:08 +00:00
|
|
|
|
|
|
|
switch f.(type) {
|
|
|
|
case IntSliceFlag:
|
2017-04-10 14:45:51 +00:00
|
|
|
return FlagFileHinter(
|
|
|
|
fv.FieldByName("FilePath").String(),
|
|
|
|
FlagEnvHinter(
|
|
|
|
fv.FieldByName("EnvVar").String(),
|
|
|
|
stringifyIntSliceFlag(f.(IntSliceFlag)),
|
|
|
|
),
|
|
|
|
)
|
2016-06-12 04:54:33 +00:00
|
|
|
case Int64SliceFlag:
|
2017-04-10 14:45:51 +00:00
|
|
|
return FlagFileHinter(
|
|
|
|
fv.FieldByName("FilePath").String(),
|
|
|
|
FlagEnvHinter(
|
|
|
|
fv.FieldByName("EnvVar").String(),
|
|
|
|
stringifyInt64SliceFlag(f.(Int64SliceFlag)),
|
|
|
|
),
|
|
|
|
)
|
2016-05-02 23:42:08 +00:00
|
|
|
case StringSliceFlag:
|
2017-04-10 14:45:51 +00:00
|
|
|
return FlagFileHinter(
|
|
|
|
fv.FieldByName("FilePath").String(),
|
|
|
|
FlagEnvHinter(
|
|
|
|
fv.FieldByName("EnvVar").String(),
|
|
|
|
stringifyStringSliceFlag(f.(StringSliceFlag)),
|
|
|
|
),
|
|
|
|
)
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 17:05:21 +00:00
|
|
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
needsPlaceholder := false
|
2016-05-02 17:05:21 +00:00
|
|
|
defaultValueString := ""
|
2016-05-02 23:42:08 +00:00
|
|
|
|
2017-05-06 03:07:18 +00:00
|
|
|
if val := fv.FieldByName("Value"); val.IsValid() {
|
2016-05-02 23:42:08 +00:00
|
|
|
needsPlaceholder = true
|
2016-05-02 23:58:16 +00:00
|
|
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
|
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
if val.Kind() == reflect.String && val.String() != "" {
|
2016-05-02 23:58:16 +00:00
|
|
|
defaultValueString = fmt.Sprintf(" (default: %q)", val.String())
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
if defaultValueString == " (default: )" {
|
|
|
|
defaultValueString = ""
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
if needsPlaceholder && placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultValueString))
|
2016-05-02 23:42:08 +00:00
|
|
|
|
2017-04-10 14:45:51 +00:00
|
|
|
return FlagFileHinter(
|
|
|
|
fv.FieldByName("FilePath").String(),
|
|
|
|
FlagEnvHinter(
|
|
|
|
fv.FieldByName("EnvVar").String(),
|
|
|
|
fmt.Sprintf("%s\t%s", FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder), usageWithDefault),
|
|
|
|
),
|
|
|
|
)
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyIntSliceFlag(f IntSliceFlag) string {
|
|
|
|
defaultVals := []string{}
|
2016-06-12 04:54:33 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyInt64SliceFlag(f Int64SliceFlag) string {
|
|
|
|
defaultVals := []string{}
|
2016-05-02 23:42:08 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyStringSliceFlag(f StringSliceFlag) string {
|
|
|
|
defaultVals := []string{}
|
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, s := range f.Value.Value() {
|
|
|
|
if len(s) > 0 {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%q", s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifySliceFlag(usage, name string, defaultVals []string) string {
|
|
|
|
placeholder, usage := unquoteUsage(usage)
|
|
|
|
if placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultVal := ""
|
|
|
|
if len(defaultVals) > 0 {
|
|
|
|
defaultVal = fmt.Sprintf(" (default: %s)", strings.Join(defaultVals, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
|
2017-08-26 11:42:25 +00:00
|
|
|
return fmt.Sprintf("%s\t%s", FlagNamePrefixer(name, placeholder), usageWithDefault)
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
2017-03-31 07:24:15 +00:00
|
|
|
|
|
|
|
func flagFromFileEnv(filePath, envName string) (val string, ok bool) {
|
|
|
|
for _, envVar := range strings.Split(envName, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok {
|
|
|
|
return envVal, true
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 18:08:03 +00:00
|
|
|
for _, fileVar := range strings.Split(filePath, ",") {
|
|
|
|
if data, err := ioutil.ReadFile(fileVar); err == nil {
|
|
|
|
return string(data), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
2017-03-31 07:24:15 +00:00
|
|
|
}
|