From 19f5ed4e0c4c7183504975fe799003986095f48d Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 10 Apr 2024 23:36:31 +0200 Subject: feat(gonf): implement config directory flag and env variable --- cmd/gonf/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go index 882636a..1e28368 100644 --- a/cmd/gonf/main.go +++ b/cmd/gonf/main.go @@ -12,13 +12,14 @@ import ( var ( batchMode bool + configDir string helpMode bool ) func main() { if err := run(context.Background(), os.Args, - //os.Getenv, + os.Getenv, //os.Getwd, //os.Stdin, os.Stdout, @@ -31,7 +32,7 @@ func main() { func run(ctx context.Context, args []string, - //getenv func(string) string, + getenv func(string) string, //getwd func() (string, error), //stdin io.Reader, stdout, stderr io.Writer, @@ -46,6 +47,7 @@ where COMMAND is one of: where FLAG can be one or more of`, flag.ContinueOnError) f.BoolVar(&batchMode, "batch", false, "skips all questions and confirmations, using the default (safe) choices each time") f.BoolVar(&helpMode, "help", false, "show contextual help") + f.StringVar(&configDir, "config", "", "(REQUIRED for most commands) path to a gonf configurations repository (overrides the GONF_CONFIG environment variable)") f.SetOutput(stderr) f.Parse(args[1:]) @@ -61,8 +63,18 @@ where FLAG can be one or more of`, flag.ContinueOnError) case "version": cmdVersion() default: - f.Usage() - return fmt.Errorf("Invalid command: %s", cmd) + if configDir == "" { + configDir = getenv("GONF_CONFIG") + if configDir == "" { + f.Usage() + return errors.New("The GONF_CONFIG environment variable is unset and the -config FLAG is missing. Please use one or the other.") + } + } + switch cmd { + default: + f.Usage() + return fmt.Errorf("Invalid command: %s", cmd) + } } return nil } -- cgit v1.2.3