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

Allow flag or file based configuration #620

Merged
merged 10 commits into from
May 26, 2017
35 changes: 19 additions & 16 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,17 @@ import (
"bitbucket.org/creachadair/shell"
)

// ParseFlagFile parses a set of flags from a file at the provided
// path. Re-calls flag.Parse() after parsing the flags in the file
// so that flags provided on the command line take precedence over
// flags provided in the file.
func ParseFlagFile(path string) error {
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}

// Expand any environment variables in the file
flagsString := os.ExpandEnv(string(file))

args, valid := shell.Split(flagsString)
func parseFlags(file string) error {
args, valid := shell.Split(file)
if !valid {
return errors.New("flag file contains unclosed quotations")
}
// Expand any environment variables in the args
for i := range args {
args[i] = os.ExpandEnv(args[i])
}

err = flag.CommandLine.Parse(args)
if err != nil {
if err := flag.CommandLine.Parse(args); err != nil {
return err
}

Expand All @@ -51,3 +42,15 @@ func ParseFlagFile(path string) error {
flag.Parse()
return nil
}

// ParseFlagFile parses a set of flags from a file at the provided
// path. Re-calls flag.Parse() after parsing the flags in the file
// so that flags provided on the command line take precedence over
// flags provided in the file.
func ParseFlagFile(path string) error {
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return parseFlags(string(file))
}
104 changes: 104 additions & 0 deletions cmd/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"flag"
"os"
"testing"
)

func TestParseFlags(t *testing.T) {
var a, b string
flag.StringVar(&a, "a", "", "")
flag.StringVar(&b, "b", "", "")

flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)

tests := []struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a name field and include it in all of the t.Errorf() calls so that it's easy to identify which test case failed.

contents string
env map[string]string
cliArgs []string
expectedErr string
expectedA string
expectedB string
}{
{
contents: "-a one -b two",
expectedA: "one",
expectedB: "two",
},
{
contents: "-a one\n-b two",
expectedA: "one",
expectedB: "two",
},
{
contents: "-a one \\\n-b two",
expectedA: "one",
expectedB: "two",
},
{
contents: "-a one",
cliArgs: []string{"-b", "two"},
expectedA: "one",
expectedB: "two",
},
{
contents: "-a one\n-b two",
cliArgs: []string{"-b", "three"},
expectedA: "one",
expectedB: "three",
},
{
contents: "-a one\n-b $TEST_VAR",
env: map[string]string{"TEST_VAR": "from env"},
expectedA: "one",
expectedB: "from env",
},
{
contents: "-a one -b two -c three",
expectedErr: "flag provided but not defined: -c",
},
}

initalArgs := os.Args[:]
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: s/initalArgs/initialArgs/

for _, tc := range tests {
a, b = "", ""
os.Args = initalArgs[:]
Copy link
Contributor

Choose a reason for hiding this comment

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

Lines 80-83 could be condensed into os.Args = append(initialArgs, tc.cliArgs...).

if len(tc.cliArgs) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for this check - os.Args = append(os.Args, tc.cliArgs...) will simply be a noop if len(tc.cliArgs) == 0.

os.Args = append(os.Args, tc.cliArgs...)
}
for k, v := range tc.env {
if err := os.Setenv(k, v); err != nil {
t.Errorf("os.SetEnv failed: %s", err)
}
}
err := parseFlags(tc.contents)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could combine the above two lines.

if err.Error() == tc.expectedErr {
continue
}
t.Errorf("parseFlags failed: wanted: %q, got: %q", tc.expectedErr, err)
continue
}
if tc.expectedA != a {
t.Errorf("flag 'a' not properly set: wanted: %q, got %q", tc.expectedA, a)
}
if tc.expectedB != b {
t.Errorf("flag 'b' not properly set: wanted: %q, got %q", tc.expectedB, b)
}
}
}