-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support a config file to use instead of commandline arguments
Signed-off-by: Dave Henderson <[email protected]>
- Loading branch information
1 parent
307e0fa
commit 3b8b572
Showing
18 changed files
with
1,910 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hairyhenderson/gomplate/v3/internal/config" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultConfigFile = ".gomplate.yaml" | ||
) | ||
|
||
var fs = afero.NewOsFs() | ||
|
||
// loadConfig is intended to be called before command execution. It: | ||
// - creates a config.Config from the cobra flags | ||
// - creates a config.Config from the config file (if present) | ||
// - merges the two (flags take precedence) | ||
// - validates the final config | ||
// - converts the config to a *gomplate.Config for further use (TODO: eliminate this part) | ||
func loadConfig(cmd *cobra.Command, args []string) (*config.Config, error) { | ||
flagConfig, err := cobraConfig(cmd, args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cfg, err := readConfigFile(cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if cfg == nil { | ||
cfg = flagConfig | ||
} else { | ||
cfg = cfg.MergeFrom(flagConfig) | ||
} | ||
|
||
// reset defaults before validation | ||
cfg.ApplyDefaults() | ||
|
||
err = cfg.Validate() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to validate merged config: %w\n%+v", err, cfg) | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func pickConfigFile(cmd *cobra.Command) (cfgFile string, required bool) { | ||
cfgFile = defaultConfigFile | ||
if cmd.Flags().Changed("config") && cmd.Flag("config").Value.String() != "" { | ||
// Use config file from the flag if specified | ||
cfgFile = cmd.Flag("config").Value.String() | ||
required = true | ||
} | ||
return cfgFile, required | ||
} | ||
|
||
func readConfigFile(cmd *cobra.Command) (cfg *config.Config, err error) { | ||
ctx := cmd.Context() | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
log := zerolog.Ctx(ctx) | ||
|
||
cfgFile, configRequired := pickConfigFile(cmd) | ||
|
||
f, err := fs.Open(cfgFile) | ||
if err != nil { | ||
if configRequired { | ||
return cfg, fmt.Errorf("config file requested, but couldn't be opened: %w", err) | ||
} | ||
return nil, nil | ||
} | ||
|
||
cfg, err = config.Parse(f) | ||
if err != nil && configRequired { | ||
return cfg, fmt.Errorf("config file requested, but couldn't be parsed: %w", err) | ||
} | ||
|
||
log.Debug().Str("cfgFile", cfgFile).Msg("using config file") | ||
|
||
return cfg, err | ||
} | ||
|
||
// cobraConfig - initialize a config from the commandline options | ||
func cobraConfig(cmd *cobra.Command, args []string) (cfg *config.Config, err error) { | ||
cfg = &config.Config{} | ||
cfg.InputFiles, err = getStringSlice(cmd, "file") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.Input, err = getString(cmd, "in") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.InputDir, err = getString(cmd, "input-dir") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cfg.ExcludeGlob, err = getStringSlice(cmd, "exclude") | ||
if err != nil { | ||
return nil, err | ||
} | ||
includesFlag, err := getStringSlice(cmd, "include") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// support --include | ||
cfg.ExcludeGlob = processIncludes(includesFlag, cfg.ExcludeGlob) | ||
|
||
cfg.OutputFiles, err = getStringSlice(cmd, "out") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.Templates, err = getStringSlice(cmd, "template") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.OutputDir, err = getString(cmd, "output-dir") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.OutputMap, err = getString(cmd, "output-map") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.OutMode, err = getString(cmd, "chmod") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(args) > 0 { | ||
cfg.PostExec = args | ||
} | ||
|
||
cfg.ExecPipe, err = getBool(cmd, "exec-pipe") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cfg.LDelim, err = getString(cmd, "left-delim") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.RDelim, err = getString(cmd, "right-delim") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ds, err := getStringSlice(cmd, "datasource") | ||
if err != nil { | ||
return nil, err | ||
} | ||
cx, err := getStringSlice(cmd, "context") | ||
if err != nil { | ||
return nil, err | ||
} | ||
hdr, err := getStringSlice(cmd, "datasource-header") | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = cfg.ParseDataSourceFlags(ds, cx, hdr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pl, err := getStringSlice(cmd, "plugin") | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = cfg.ParsePluginFlags(pl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func getStringSlice(cmd *cobra.Command, flag string) (s []string, err error) { | ||
if cmd.Flag(flag) != nil && cmd.Flag(flag).Changed { | ||
s, err = cmd.Flags().GetStringSlice(flag) | ||
} | ||
return s, err | ||
} | ||
|
||
func getString(cmd *cobra.Command, flag string) (s string, err error) { | ||
if cmd.Flag(flag) != nil && cmd.Flag(flag).Changed { | ||
s, err = cmd.Flags().GetString(flag) | ||
} | ||
return s, err | ||
} | ||
|
||
func getBool(cmd *cobra.Command, flag string) (b bool, err error) { | ||
if cmd.Flag(flag) != nil && cmd.Flag(flag).Changed { | ||
b, err = cmd.Flags().GetBool(flag) | ||
} | ||
return b, err | ||
} | ||
|
||
// process --include flags - these are analogous to specifying --exclude '*', | ||
// then the inverse of the --include options. | ||
func processIncludes(includes, excludes []string) []string { | ||
if len(includes) == 0 && len(excludes) == 0 { | ||
return nil | ||
} | ||
|
||
out := []string{} | ||
// if any --includes are set, we start by excluding everything | ||
if len(includes) > 0 { | ||
out = make([]string, 1+len(includes)) | ||
out[0] = "*" | ||
} | ||
for i, include := range includes { | ||
// includes are just the opposite of an exclude | ||
out[i+1] = "!" + include | ||
} | ||
out = append(out, excludes...) | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hairyhenderson/gomplate/v3/internal/config" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadConfigFile(t *testing.T) { | ||
fs = afero.NewMemMapFs() | ||
defer func() { fs = afero.NewOsFs() }() | ||
cmd := &cobra.Command{} | ||
|
||
_, err := readConfigFile(cmd) | ||
assert.NoError(t, err) | ||
|
||
cmd.Flags().String("config", defaultConfigFile, "foo") | ||
|
||
_, err = readConfigFile(cmd) | ||
assert.NoError(t, err) | ||
|
||
cmd.ParseFlags([]string{"--config", "config.file"}) | ||
|
||
_, err = readConfigFile(cmd) | ||
assert.Error(t, err) | ||
|
||
cmd = &cobra.Command{} | ||
cmd.Flags().String("config", defaultConfigFile, "foo") | ||
|
||
f, err := fs.Create(defaultConfigFile) | ||
assert.NoError(t, err) | ||
f.WriteString("") | ||
|
||
cfg, err := readConfigFile(cmd) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, &config.Config{}, cfg) | ||
|
||
cmd.ParseFlags([]string{"--config", "config.yaml"}) | ||
|
||
f, err = fs.Create("config.yaml") | ||
assert.NoError(t, err) | ||
f.WriteString("in: hello world\n") | ||
|
||
cfg, err = readConfigFile(cmd) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, &config.Config{Input: "hello world"}, cfg) | ||
|
||
f.WriteString("in: ") | ||
|
||
_, err = readConfigFile(cmd) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
fs = afero.NewMemMapFs() | ||
defer func() { fs = afero.NewOsFs() }() | ||
|
||
cmd := &cobra.Command{} | ||
cmd.Args = optionalExecArgs | ||
cmd.Flags().StringSlice("file", []string{"-"}, "...") | ||
cmd.Flags().StringSlice("out", []string{"-"}, "...") | ||
cmd.Flags().String("in", ".", "...") | ||
cmd.Flags().String("output-dir", ".", "...") | ||
cmd.Flags().String("left-delim", "{{", "...") | ||
cmd.Flags().String("right-delim", "}}", "...") | ||
cmd.Flags().Bool("exec-pipe", false, "...") | ||
cmd.ParseFlags(nil) | ||
|
||
out, err := loadConfig(cmd, cmd.Flags().Args()) | ||
expected := &config.Config{ | ||
InputFiles: []string{"-"}, | ||
OutputFiles: []string{"-"}, | ||
LDelim: "{{", | ||
RDelim: "}}", | ||
} | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, expected, out) | ||
|
||
cmd.ParseFlags([]string{"--in", "foo"}) | ||
out, err = loadConfig(cmd, cmd.Flags().Args()) | ||
expected = &config.Config{ | ||
Input: "foo", | ||
OutputFiles: []string{"-"}, | ||
LDelim: "{{", | ||
RDelim: "}}", | ||
} | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, expected, out) | ||
|
||
cmd.ParseFlags([]string{"--in", "foo", "--exec-pipe", "--", "tr", "[a-z]", "[A-Z]"}) | ||
out, err = loadConfig(cmd, cmd.Flags().Args()) | ||
expected = &config.Config{ | ||
Input: "foo", | ||
LDelim: "{{", | ||
RDelim: "}}", | ||
ExecPipe: true, | ||
PostExec: []string{"tr", "[a-z]", "[A-Z]"}, | ||
} | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, expected, out) | ||
} | ||
|
||
func TestCobraConfig(t *testing.T) { | ||
t.Parallel() | ||
cmd := &cobra.Command{} | ||
cmd.Flags().StringSlice("file", []string{"-"}, "...") | ||
cmd.Flags().StringSlice("out", []string{"-"}, "...") | ||
cmd.Flags().String("output-dir", ".", "...") | ||
cmd.Flags().String("left-delim", "{{", "...") | ||
cmd.Flags().String("right-delim", "}}", "...") | ||
cmd.ParseFlags(nil) | ||
|
||
cfg, err := cobraConfig(cmd, cmd.Flags().Args()) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, &config.Config{}, cfg) | ||
|
||
cmd.ParseFlags([]string{"--file", "in", "--", "echo", "foo"}) | ||
|
||
cfg, err = cobraConfig(cmd, cmd.Flags().Args()) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, &config.Config{ | ||
InputFiles: []string{"in"}, | ||
PostExec: []string{"echo", "foo"}, | ||
}, cfg) | ||
} | ||
|
||
func TestProcessIncludes(t *testing.T) { | ||
t.Parallel() | ||
data := []struct { | ||
inc, exc, expected []string | ||
}{ | ||
{nil, nil, nil}, | ||
{[]string{}, []string{}, nil}, | ||
{nil, []string{"*.foo"}, []string{"*.foo"}}, | ||
{[]string{"*.bar"}, []string{"a*.bar"}, []string{"*", "!*.bar", "a*.bar"}}, | ||
{[]string{"*.bar"}, nil, []string{"*", "!*.bar"}}, | ||
} | ||
|
||
for _, d := range data { | ||
assert.EqualValues(t, d.expected, processIncludes(d.inc, d.exc)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.