From 677d92e1853316cf883511e40e265fd1cbe6fcd7 Mon Sep 17 00:00:00 2001 From: Nik <45662151+nikonhub@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:21:03 +0200 Subject: [PATCH] Support env variables for config (#111) (#177) * Support env variables for config (#111) * Check the return err of flag.Set * Typo var name * Fix typo in env variables prefix * Add env doc to README * Convert env doc to a table --------- Co-authored-by: nik --- README.md | 14 ++++++++++++++ cmd/validator/validator.go | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 40ee7ce9..e18bbf90 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,20 @@ optional flags: Version prints the release version of validator ``` +### Environment Variables + +The config-file-validator supports setting options via environment variables. If both command-line flags and environment variables are set, the command-line flags will take precedence. The supported environment variables are as follows: + +| Environment Variable | Equivalent Flag | +|----------------------|-----------------| +| `CFV_DEPTH` | `-depth` | +| `CFV_EXCLUDE_DIRS` | `-exclude-dirs` | +| `CFV_EXCLUDE_FILE_TYPES` | `-exclude-file-types` | +| `CFV_OUTPUT` | `-output` | +| `CFV_REPORTER` | `-reporter` | +| `CFV_GROUPBY` | `-groupby` | +| `CFV_QUIET` | `-quiet` | + ### Examples #### Standard Run If the search path is omitted it will search the current directory diff --git a/cmd/validator/validator.go b/cmd/validator/validator.go index 63fceebc..b59590ce 100644 --- a/cmd/validator/validator.go +++ b/cmd/validator/validator.go @@ -79,7 +79,24 @@ func getFlags() (validatorConfig, error) { reportTypePtr := flag.String("reporter", "standard", "Format of the printed report. Options are standard and json") versionPtr := flag.Bool("version", false, "Version prints the release version of validator") groupOutputPtr := flag.String("groupby", "", "Group output by filetype, directory, pass-fail. Supported for Standard and JSON reports") - quietPrt := flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.") + quietPtr := flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.") + + flagsEnvMap := map[string]string{ + "depth": "CFV_DEPTH", + "exclude-dirs": "CFV_EXCLUDE_DIRS", + "exclude-file-types": "CFV_EXCLUDE_FILE_TYPES", + "output": "CFV_OUTPUT", + "reporter": "CFV_REPORTER", + "groupby": "CFV_GROUPBY", + "quiet": "CFV_QUIET", + } + + for flagName, envVar := range flagsEnvMap { + if err := setFlagFromEnvIfNotSet(flagName, envVar); err != nil { + return validatorConfig{}, err + } + } + flag.Parse() searchPaths := make([]string, 0) @@ -142,7 +159,7 @@ func getFlags() (validatorConfig, error) { versionPtr, outputPtr, groupOutputPtr, - quietPrt, + quietPtr, } return config, nil @@ -161,6 +178,20 @@ func isFlagSet(flagName string) bool { return isSet } +func setFlagFromEnvIfNotSet(flagName string, envVar string) error { + if isFlagSet(flagName) { + return nil + } + + if envVarValue, ok := os.LookupEnv(envVar); ok { + if err := flag.Set(flagName, envVarValue); err != nil { + return err + } + } + + return nil +} + // Return the reporter associated with the // reportType string func getReporter(reportType, outputDest *string) reporter.Reporter {