Skip to content

Commit

Permalink
fix: environment variable handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Apr 11, 2022
1 parent 6b1be1b commit b91353c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,10 @@ Messages are written into standard error stream (`stderr`) to avoid polluting th
<br/>

## Features
- Coloured output by default with emojis
- Prints to Standard Error stream (`stderr`)
- Subprocess friendly: Tries to access the `tty` and print to its `stderr` (can be disabled)
- Message structure:
1. `emoji` (optional, pass empty string to ignore it)
2. `...args` (often just a single string)
- Coloured output by default with emojis
- Respectful of user environment and disables color output with emojis if one of below set:
- `NO_COLOR` (to something other than `false` or `0`)
- `<APP-NAME>_NO_COLOR` (to something other than `false` or `0`)
- `TERM=dumb`
- Allows user to control verbosity by configuring environment variables:
- `VERBOSE` (to something other than `false` or `0`)
- `<APP-NAME>_VERBOSE` (to something other than `false` or `0`)
- Respectful of user environment: User can disable colors/emojis, enable silent mode or verbose mode by setting various environment variables.

<br/>

Expand Down
14 changes: 6 additions & 8 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ func allowEmoji(appName string) bool {
// enableVerboseMode sets the initial verbosity setting by looking at the user
// environment for specific variables.
func enableVerboseMode(appName string) bool {
// Check if NO_COLOR set https://no-color.org/
if isEnvVarSetOrTruthy("VERBOSE") {
return true
}

// Check if app-specific _NO_COLOR set https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46
if isEnvVarSetOrTruthy(formatPrefixedEnvVar(appName, "VERBOSE")) {
return true
}
Expand All @@ -164,19 +162,19 @@ func enableVerboseMode(appName string) bool {
func isEnvVarSetOrTruthy(envVar string) bool {
value, set := os.LookupEnv(envVar)

// if it's not set at all
// check if not set at all
if !set {
return false
}

// if it has truthy value
// check if falsy value
lowerValue := strings.ToLower(value)
if lowerValue == "true" || lowerValue == "1" {
return true
if lowerValue == "false" || lowerValue == "0" {
return false
}

// for unset or "falsy value"
return false
// is set (or has truthy value)
return true
}

func formatPrefixedEnvVar(prefix string, envVar string) string {
Expand Down

0 comments on commit b91353c

Please sign in to comment.