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

Env var handling should prefer old names #931

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,3 @@ HOOKS
if a hook fails and a new hash is synced during the backoff period, the
retried hook will fire for the newest hash.
```

168 changes: 132 additions & 36 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,42 @@ import (
"github.com/spf13/pflag"
)

// Tests can set this or set it to nil.
var envWarnfOverride func(format string, args ...any)

func envWarnf(format string, args ...any) {
if envWarnfOverride != nil {
envWarnfOverride(format, args...)
} else {
fmt.Fprintf(os.Stderr, format, args...)
}
}

func envString(def string, key string, alts ...string) string {
if val := os.Getenv(key); val != "" {
return val
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return val
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = alt
}
}
return def
if found == 0 {
return def
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}
return result
}
func envFlagString(key string, def string, usage string, alts ...string) *string {
registerEnvFlag(key, "string", usage)
Expand All @@ -60,16 +85,31 @@ func envStringArray(def string, key string, alts ...string) []string {
return strings.Split(s, ":")
}

if val := os.Getenv(key); val != "" {
return parse(val)
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return parse(val)
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = key
}
}
return parse(def)
if found == 0 {
return parse(def)
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}

return parse(result)
}

func envBoolOrError(def bool, key string, alts ...string) (bool, error) {
Expand All @@ -81,16 +121,30 @@ func envBoolOrError(def bool, key string, alts ...string) (bool, error) {
return false, fmt.Errorf("invalid bool env %s=%q: %w", key, val, err)
}

if val := os.Getenv(key); val != "" {
return parse(key, val)
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return parse(alt, val)
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = key
}
}
return def, nil
if found == 0 {
return def, nil
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}
return parse(resultKey, result)
}
func envBool(def bool, key string, alts ...string) bool {
val, err := envBoolOrError(def, key, alts...)
Expand All @@ -111,16 +165,30 @@ func envIntOrError(def int, key string, alts ...string) (int, error) {
return 0, fmt.Errorf("invalid int env %s=%q: %w", key, val, err)
}

if val := os.Getenv(key); val != "" {
return parse(key, val)
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return parse(alt, val)
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = key
}
}
return def, nil
if found == 0 {
return def, nil
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}
return parse(resultKey, result)
}
func envInt(def int, key string, alts ...string) int {
val, err := envIntOrError(def, key, alts...)
Expand All @@ -141,16 +209,30 @@ func envFloatOrError(def float64, key string, alts ...string) (float64, error) {
return 0, fmt.Errorf("invalid float env %s=%q: %w", key, val, err)
}

if val := os.Getenv(key); val != "" {
return parse(key, val)
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return parse(alt, val)
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = key
}
}
return def, nil
if found == 0 {
return def, nil
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}
return parse(resultKey, result)
}
func envFloat(def float64, key string, alts ...string) float64 {
val, err := envFloatOrError(def, key, alts...)
Expand All @@ -171,16 +253,30 @@ func envDurationOrError(def time.Duration, key string, alts ...string) (time.Dur
return 0, fmt.Errorf("invalid duration env %s=%q: %w", key, val, err)
}

if val := os.Getenv(key); val != "" {
return parse(key, val)
found := 0
result := ""
resultKey := ""

if val, ok := os.LookupEnv(key); ok {
found++
result = val
resultKey = key
}
for _, alt := range alts {
if val := os.Getenv(alt); val != "" {
fmt.Fprintf(os.Stderr, "env $%s has been deprecated, use $%s instead\n", alt, key)
return parse(alt, val)
if val, ok := os.LookupEnv(alt); ok {
envWarnf("env $%s has been deprecated, use $%s instead\n", alt, key)
found++
result = val
resultKey = key
}
}
return def, nil
if found == 0 {
return def, nil
}
if found > 1 {
envWarnf("env $%s was overridden by $%s\n", key, resultKey)
}
return parse(resultKey, result)
}
func envDuration(def time.Duration, key string, alts ...string) time.Duration {
val, err := envDurationOrError(def, key, alts...)
Expand Down
Loading
Loading