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

Add option to configure fly by env #3789

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
extend functionality to string/duration type vars
fliepeltje committed Jul 29, 2024

Verified

This commit was signed with the committer’s verified signature.
nonrational Alan Norton
commit 9591ece72c5fe4af53608993efe46329c3802f4b
28 changes: 19 additions & 9 deletions internal/flag/context.go
Original file line number Diff line number Diff line change
@@ -97,20 +97,24 @@ func GetFloat64(ctx context.Context, name string) float64 {
// Preserves commas (unlike the following `GetStringSlice`): in `--flag x,y` the value is string[]{`x,y`}.
// This is useful to pass key-value pairs like environment variables or build arguments.
func GetStringArray(ctx context.Context, name string) []string {
if v, err := FromContext(ctx).GetStringArray(name); err != nil {
return []string{}
} else {
if v, err := FromContext(ctx).GetStringArray(name); err == nil {
return v
} else if v := FromEnv(name); v != "" {
return []string{v}
} else {
return []string{}
}
}

// GetStringSlice returns the values of the named string flag ctx carries.
// Can be comma separated or passed "by repeated flags": `--flag x,y` is equivalent to `--flag x --flag y`.
func GetStringSlice(ctx context.Context, name string) []string {
if v, err := FromContext(ctx).GetStringSlice(name); err != nil {
return []string{}
} else {
if v, err := FromContext(ctx).GetStringSlice(name); err == nil {
return v
} else if v := FromEnv(name); v != "" {
return strings.Split(v, ",")
} else {
return []string{}
}
}

@@ -131,10 +135,16 @@ func GetNonEmptyStringSlice(ctx context.Context, name string) []string {

// GetDuration returns the value of the named duration flag ctx carries.
func GetDuration(ctx context.Context, name string) time.Duration {
if v, err := FromContext(ctx).GetDuration(name); err != nil {
return 0
} else {
if v, err := FromContext(ctx).GetDuration(name); err == nil {
return v
} else if v := FromEnv(name); v != "" {
if d, err := time.ParseDuration(v); err == nil {
return d
} else {
return 0
}
} else {
return 0
}
}