Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add support for multiple env #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ And you can use your favorite flag or cli library!
- [x] Required
- [ ] Placeholders (by `name`)
- [x] Deprecated and hidden options
- [ ] Multiple ENV names
- [x] Multiple ENV names
- [x] Interface for user types.
- [x] [Validation](https://godoc.org/github.com/urfave/sflags/validator/govalidator#New) (using [govalidator](https://github.com/asaskevich/govalidator) package)
- [x] Anonymous nested structure support (anonymous structures flatten by default)
Expand Down
2 changes: 1 addition & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package sflags
type Flag struct {
Name string // name as it appears on command line
Short string // optional short name
EnvName string
EnvNames []string
Usage string // help message
Value Value // value as set
DefValue string // default value (as text); for usage message
Expand Down
2 changes: 1 addition & 1 deletion gen/gcli/gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GenerateTo(src []*sflags.Flag, dst *[]cli.Flag) {
}
*dst = append(*dst, &cli.GenericFlag{
Name: name,
EnvVars: []string{srcFlag.EnvName},
EnvVars: srcFlag.EnvNames,
Aliases: aliases,
Hidden: srcFlag.Hidden,
Usage: srcFlag.Usage,
Expand Down
2 changes: 1 addition & 1 deletion gen/gcli/gcliv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GenerateToV3(src []*sflags.Flag, dst *[]cli.Flag) {
}
*dst = append(*dst, &cli.GenericFlag{
Name: name,
Sources: cli.EnvVars(srcFlag.EnvName),
Sources: cli.EnvVars(srcFlag.EnvNames...),
Aliases: aliases,
Hidden: srcFlag.Hidden,
Usage: srcFlag.Usage,
Expand Down
4 changes: 2 additions & 2 deletions gen/gkingpin/gkingpin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func GenerateTo(src []*sflags.Flag, dst flagger) {
for _, srcFlag := range src {
flag := dst.Flag(srcFlag.Name, srcFlag.Usage)
flag.SetValue(srcFlag.Value)
if srcFlag.EnvName != "" {
flag.Envar(srcFlag.EnvName)
if len(srcFlag.EnvNames) > 0 && srcFlag.EnvNames[0] != "" {
flag.Envar(srcFlag.EnvNames[0])
}
if srcFlag.Hidden {
flag.Hidden()
Expand Down
50 changes: 30 additions & 20 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,47 @@ func parseFlagTag(field reflect.StructField, opt opts) *Flag {
return &flag
}

func parseEnv(flagName string, field reflect.StructField, opt opts) string {
ignoreEnvPrefix := false
envVar := flagToEnv(flagName, opt.flagDivider, opt.envDivider)
func parseEnv(flagName string, field reflect.StructField, opt opts) []string {
var envVars []string
flagEnvVar := flagToEnv(flagName, opt.flagDivider, opt.envDivider)
if envTags := strings.Split(field.Tag.Get(defaultEnvTag), ","); len(envTags) > 0 {
switch envName := envTags[0]; envName {
case "-":
// if tag is `env:"-"` then won't fill flag from environment
envVar = ""
return envVars
case "":
// if tag is `env:""` then env var will be taken from flag name
envVars = append(envVars, opt.envPrefix+flagEnvVar)
default:
// if tag is `env:"NAME"` then env var is envPrefix_flagPrefix_NAME
// if tag is `env:"~NAME"` then env var is NAME
if strings.HasPrefix(envName, "~") {
envVar = envName[1:]
ignoreEnvPrefix = true
} else {
envVar = envName
if opt.prefix != "" {
envVar = flagToEnv(
opt.prefix,
opt.flagDivider,
opt.envDivider) + envVar
for _, envName := range envTags {
ignoreEnvPrefix := false
var envVar string
if strings.HasPrefix(envName, "~") {
envVar = envName[1:]
ignoreEnvPrefix = true
} else {
envVar = envName
if opt.prefix != "" {
envVar = flagToEnv(
opt.prefix,
opt.flagDivider,
opt.envDivider) + envVar
}
}
if envVar != "" {
if !ignoreEnvPrefix {
envVars = append(envVars, opt.envPrefix+envVar)
} else {
envVars = append(envVars, envVar)
}
}
}
}
} else if flagEnvVar != "" {
envVars = append(envVars, opt.envPrefix+flagEnvVar)
}
if envVar != "" && opt.envPrefix != "" && !ignoreEnvPrefix {
envVar = opt.envPrefix + envVar
}
return envVar
return envVars
}

// ParseStruct parses structure and returns list of flags based on this structure.
Expand Down Expand Up @@ -247,7 +257,7 @@ fields:
continue fields
}

flag.EnvName = parseEnv(flag.Name, field, opt)
flag.EnvNames = parseEnv(flag.Name, field, opt)
flag.Usage = field.Tag.Get(opt.descTag)
prefix := flag.Name + opt.flagDivider
if field.Anonymous && opt.flatten {
Expand Down
Loading