From 1a4192b5c94a2145f8ac6504f30643d9a838bff0 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 7 Jun 2024 22:42:57 +0200 Subject: feat(gonf): support a GONF_HOST environment variable --- cmd/gonf/cmd_build.go | 2 +- cmd/gonf/cmd_deploy.go | 2 +- cmd/gonf/hostflag.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'cmd') diff --git a/cmd/gonf/cmd_build.go b/cmd/gonf/cmd_build.go index 566edfd..0bc1fa0 100644 --- a/cmd/gonf/cmd_build.go +++ b/cmd/gonf/cmd_build.go @@ -24,7 +24,7 @@ where FLAG can be one or more of`, flag.ContinueOnError) f.SetOutput(stdout) f.Usage() } - hostDir, err := hostFlagToHostDir(hostFlag) + hostDir, err := hostFlagToHostDir(hostFlag, getenv) if err != nil { f.Usage() return err diff --git a/cmd/gonf/cmd_deploy.go b/cmd/gonf/cmd_deploy.go index 0d48b2b..2f0d68f 100644 --- a/cmd/gonf/cmd_deploy.go +++ b/cmd/gonf/cmd_deploy.go @@ -23,7 +23,7 @@ where FLAG can be one or more of`, flag.ContinueOnError) f.SetOutput(stdout) f.Usage() } - hostDir, err := hostFlagToHostDir(hostFlag) + hostDir, err := hostFlagToHostDir(hostFlag, getenv) if err != nil { f.Usage() return err diff --git a/cmd/gonf/hostflag.go b/cmd/gonf/hostflag.go index 2c2c877..e936f45 100644 --- a/cmd/gonf/hostflag.go +++ b/cmd/gonf/hostflag.go @@ -8,12 +8,17 @@ import ( ) func addHostFlag(f *flag.FlagSet) *string { - return f.String("host", "", "(REQUIRED) a valid $GONF_CONFIG/hosts/ subdirectory") + return f.String("host", "", "(REQUIRED) a valid $GONF_CONFIG/hosts/ subdirectory (overrides the GONF_HOST environment variable)") } -func hostFlagToHostDir(hostFlag *string) (string, error) { +func hostFlagToHostDir(hostFlag *string, + getenv func(string) string, +) (string, error) { if *hostFlag == "" { - return "", fmt.Errorf("required -host FLAG is missing") + *hostFlag = getenv("GONF_HOST") + if *hostFlag == "" { + return "", fmt.Errorf("the GONF_HOST environment variable is unset and the -host FLAG is missing. Please use one or the other") + } } hostDir := filepath.Join(configDir, "hosts", *hostFlag) if info, err := os.Stat(hostDir); err != nil { -- cgit v1.2.3