Skip to content

Commit

Permalink
ff: add WithConfigFileVia option (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasf authored Nov 13, 2020
1 parent a2a0e27 commit 7f261de
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
25 changes: 19 additions & 6 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ func Parse(fs *flag.FlagSet, args []string, options ...Option) error {
provided[f.Name] = true
})

var configFile string
if c.configFileVia != nil {
configFile = *c.configFileVia
}

// Third priority: config file (host).
if c.configFile == "" && c.configFileFlagName != "" {
if configFile == "" && c.configFileFlagName != "" {
if f := fs.Lookup(c.configFileFlagName); f != nil {
c.configFile = f.Value.String()
configFile = f.Value.String()
}
}

if parseConfig := c.configFile != "" && c.configFileParser != nil; parseConfig {
f, err := os.Open(c.configFile)
if parseConfig := configFile != "" && c.configFileParser != nil; parseConfig {
f, err := os.Open(configFile)
switch {
case err == nil:
defer f.Close()
Expand Down Expand Up @@ -117,7 +122,7 @@ func Parse(fs *flag.FlagSet, args []string, options ...Option) error {

// Context contains private fields used during parsing.
type Context struct {
configFile string
configFileVia *string
configFileFlagName string
configFileParser ConfigFileParser
allowMissingConfigFile bool
Expand All @@ -135,8 +140,16 @@ type Option func(*Context)
// Because config files should generally be user-specifiable, this option
// should be rarely used. Prefer WithConfigFileFlag.
func WithConfigFile(filename string) Option {
return WithConfigFileVia(&filename)
}

// WithConfigFileVia tells Parse to read the provided filename as a config file.
// Requires WithConfigFileParser, and overrides WithConfigFileFlag.
// This is useful for sharing a single root level flag for config files among
// multiple ffcli subcommands.
func WithConfigFileVia(filename *string) Option {
return func(c *Context) {
c.configFile = filename
c.configFileVia = filename
}
}

Expand Down
59 changes: 59 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ff_test

import (
"context"
"flag"
"os"
"testing"
"time"

"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/peterbourgon/ff/v3/fftest"
)

Expand Down Expand Up @@ -254,3 +257,59 @@ func TestParseConfigFile(t *testing.T) {
})
}
}

func TestParseConfigFileVia(t *testing.T) {
t.Parallel()

var (
rootFS = flag.NewFlagSet("root", flag.ContinueOnError)
config = rootFS.String("config-file", "", "")
i = rootFS.Int("i", 0, "")
s = rootFS.String("s", "", "")
subFS = flag.NewFlagSet("subcommand", flag.ContinueOnError)
d = subFS.Duration("d", time.Second, "")
b = subFS.Bool("b", false, "")
)

subCommand := &ffcli.Command{
Name: "subcommand",
FlagSet: subFS,
Options: []ff.Option{
ff.WithConfigFileParser(ff.PlainParser),
ff.WithConfigFileVia(config),
ff.WithIgnoreUndefined(true),
},
Exec: func(ctx context.Context, args []string) error { return nil },
}

root := &ffcli.Command{
Name: "root",
FlagSet: rootFS,
Options: []ff.Option{
ff.WithConfigFileParser(ff.PlainParser),
ff.WithConfigFileFlag("config-file"),
ff.WithIgnoreUndefined(true),
},
Exec: func(ctx context.Context, args []string) error { return nil },
Subcommands: []*ffcli.Command{subCommand},
}

err := root.ParseAndRun(context.Background(), []string{"-config-file", "testdata/1.conf", "subcommand", "-b"})
if err != nil {
t.Fatal(err)
}

if want, have := time.Hour, *d; want != have {
t.Errorf("d: want %v, have %v", want, have)
}
if want, have := true, *b; want != have {
t.Errorf("b: want %v, have %v", want, have)
}
if want, have := "bar", *s; want != have {
t.Errorf("s: want %q, have %q", want, have)
}
if want, have := 99, *i; want != have {
t.Errorf("i: want %d, have %d", want, have)
}

}

0 comments on commit 7f261de

Please sign in to comment.