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 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
4 changes: 2 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,14 @@ 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, filePath []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 filePath {
if fileVar != "" {
if data, err := os.ReadFile(fileVar); err == nil {
return string(data), fmt.Sprintf("file %q", filePath), true
Expand Down
8 changes: 4 additions & 4 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
FilePath []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
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
FilePath []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
FilePath []string // file paths to load value from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be named to FilePaths then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats what I initially was about to do but thought to keep the name same since behavior is same. I can change it. @meatballhat any thoughts ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With many file paths, what will happen if first path does not exist? Will value from the second path overwrite the first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in favor of a rename to FilePaths 👍🏼 and to @abitrolly's point it'd be nice to have clearer documentation around the expected behavior 🙇🏼 (in a later PR!)

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