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
[m3query] Allow for optional override config file. #1934
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package config | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var _ flag.Value = &FlagStringSlice{} | ||
|
||
// FlagStringSlice represents a slice of strings. When used as a flag variable, | ||
// it allows for multiple string values. For example, it can be used like this: | ||
// var configFiles FlagStringSlice | ||
// flag.Var(&configFiles, "f", "configuration file(s)") | ||
// Then it can be invoked like this: | ||
// ./app -f file1.yaml -f file2.yaml -f valueN.yaml | ||
// Finally, when the flags are parsed, the variable contains all the values. | ||
type FlagStringSlice []string | ||
|
||
// String() returns a string implmentation of the slice. | ||
func (i *FlagStringSlice) String() string { | ||
if i == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%v", ([]string)(*i)) | ||
} | ||
|
||
// Set appends a string value to the slice. | ||
func (i *FlagStringSlice) Set(value string) error { | ||
*i = append(*i, value) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package config_test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. meganit: we typically don't use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was intentional because I am not using any private members of the package. If anything internal changes, the tests remain the same - only test the public API of a package. There should be very few reasons not to use |
||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"github.com/m3db/m3/src/x/config" | ||
|
||
"github.com/stretchr/testify/assert" | ||
pavelnikolov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
func TestFlagArray(t *testing.T) { | ||
tests := []struct { | ||
args []string | ||
name string | ||
want string | ||
}{ | ||
{ | ||
name: "single value", | ||
args: []string{"-f", "./some/file/path/here.yaml"}, | ||
want: "[./some/file/path/here.yaml]", | ||
}, | ||
{ | ||
name: "single empty value", | ||
args: []string{"-f", ""}, | ||
want: "[]", | ||
}, | ||
{ | ||
name: "two value", | ||
args: []string{"-f", "file1.yaml", "-f", "file2.yaml"}, | ||
want: "[file1.yaml file2.yaml]", | ||
}, | ||
{ | ||
name: "two value one of which empty", | ||
args: []string{"-f", "", "-f", "file2.yaml"}, | ||
want: "[ file2.yaml]", | ||
}, | ||
{ | ||
name: "three values", | ||
args: []string{"-f", "file1.yaml", "-f", "file2.yaml", "-f", "file3.yaml"}, | ||
want: "[file1.yaml file2.yaml file3.yaml]", | ||
}, | ||
pavelnikolov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var values config.FlagStringSlice | ||
fs := flag.NewFlagSet(tt.name, flag.PanicOnError) | ||
fs.Var(&values, "f", "config files") | ||
err := fs.Parse(tt.args) | ||
assert.NoError(t, err, "error parsing flags") | ||
assert.Equal(t, tt.want, values.String(), "unexpected output") | ||
}) | ||
} | ||
} | ||
|
||
func TestFlagStringSliceNilFlag(t *testing.T) { | ||
var s *config.FlagStringSlice | ||
assert.Equal(t, "", s.String(), "nil string slice representation") | ||
} | ||
|
||
func TestFlagStringSliceWithOtherFlags(t *testing.T) { | ||
var values config.FlagStringSlice | ||
var x string | ||
fs := flag.NewFlagSet("app", flag.PanicOnError) | ||
fs.StringVar(&x, "x", "", "some random var") | ||
fs.Var(&values, "f", "config files") | ||
fs.Parse([]string{"-f", "file1.yaml", "-x", "file2.yaml", "-f", "file3.yaml"}) | ||
assert.Equal(t, "[file1.yaml file3.yaml]", values.String(), "flag string slice representation") | ||
assert.Equal(t, "file2.yaml", x, "x value") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about pulling all this configFiles stuff into
xconfig.StringSlice
, something likexconfig.ParseConfigFilenames(flag string, description string) ([]string, bool)
That could handle the validation and make it a little easier to use? Don't mind either option much though, so up to you here 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd personally leave it like this for now.