Skip to content

Commit

Permalink
Support env variables for config (#111) (#177)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nikonhub and nik authored Oct 22, 2024
1 parent e168780 commit 677d92e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 33 additions & 2 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -142,7 +159,7 @@ func getFlags() (validatorConfig, error) {
versionPtr,
outputPtr,
groupOutputPtr,
quietPrt,
quietPtr,
}

return config, nil
Expand All @@ -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 {
Expand Down

0 comments on commit 677d92e

Please sign in to comment.