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

Feat: Convert filepath in Flag to string slice #1601

Merged
merged 2 commits into from
Nov 29, 2022
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
6 changes: 3 additions & 3 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,17 @@ func hasFlag(flags []Flag, fl Flag) bool {
// Return the first value from a list of environment variables and files
// (which may or may not exist), a description of where the value was found,
// and a boolean which is true if a value was found.
func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhere string, found bool) {
func flagFromEnvOrFile(envVars []string, filePaths []string) (value string, fromWhere string, found bool) {
for _, envVar := range envVars {
envVar = strings.TrimSpace(envVar)
if value, found := syscall.Getenv(envVar); found {
return value, fmt.Sprintf("environment variable %q", envVar), true
}
}
for _, fileVar := range strings.Split(filePath, ",") {
for _, fileVar := range filePaths {
if fileVar != "" {
if data, err := os.ReadFile(fileVar); err == nil {
return string(data), fmt.Sprintf("file %q", filePath), true
return string(data), fmt.Sprintf("file %q", filePaths), true
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type NoConfig struct{}
type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string // name of the flag

Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePath string // file path to load value from
Usage string // usage string for help output
Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePaths []string // file paths to load value from
Usage string // usage string for help output

Required bool // whether the flag is required or not
Hidden bool // whether to hide the flag in help output
Expand Down Expand Up @@ -84,7 +84,7 @@ func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
if !f.applied || !f.Persistent {
newVal := f.Value

if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePaths); found {
tmpVal := f.creator.Create(f.Value, new(T), f.Config)
if val != "" || reflect.TypeOf(f.Value).Kind() == reflect.String {
if err := tmpVal.Set(val); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2565,7 +2565,7 @@ func TestFlagFromFile(t *testing.T) {
}

for _, filePathTest := range filePathTests {
got, _, _ := flagFromEnvOrFile(filePathTest.name, filePathTest.path)
got, _, _ := flagFromEnvOrFile(filePathTest.name, []string{filePathTest.path})
if want := filePathTest.expected; got != want {
t.Errorf("Did not expect %v - Want %v", got, want)
}
Expand Down
8 changes: 4 additions & 4 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,10 @@ var VersionFlag Flag = &BoolFlag{
type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string // name of the flag

Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePath string // file path to load value from
Usage string // usage string for help output
Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePaths []string // file paths to load value from
Usage string // usage string for help output

Required bool // whether the flag is required or not
Hidden bool // whether to hide the flag in help output
Expand Down
8 changes: 4 additions & 4 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,10 @@ var VersionFlag Flag = &BoolFlag{
type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string // name of the flag

Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePath string // file path to load value from
Usage string // usage string for help output
Category string // category of the flag, if any
DefaultText string // default text of the flag for usage purposes
FilePaths []string // file paths to load value from
Usage string // usage string for help output

Required bool // whether the flag is required or not
Hidden bool // whether to hide the flag in help output
Expand Down