Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit bff74c6

Browse files
Merge pull request #237 from secrethub/feature/check-strict-env
Add CheckStrictEnv func to cli.App
2 parents 24865af + 9868589 commit bff74c6

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

internals/cli/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ func (a *App) PrintEnv(w io.Writer, verbose bool) error {
148148
return nil
149149
}
150150

151+
// CheckStrictEnv checks that every environment variable that starts with the app name is recognized by the application.
152+
func (a *App) CheckStrictEnv() error {
153+
for _, envVar := range os.Environ() {
154+
key, _, match := splitVar(a.name, a.separator, envVar)
155+
if match {
156+
key = strings.ToUpper(key)
157+
_, isKnown := a.knownEnvVars[key]
158+
if !(isKnown || a.isExtraEnvVar(key)) {
159+
return fmt.Errorf("environment variable set, but not recognized: %s", key)
160+
}
161+
}
162+
}
163+
return nil
164+
}
165+
151166
// CommandClause represents a command clause in a command0-line application.
152167
type CommandClause struct {
153168
*kingpin.CmdClause

0 commit comments

Comments
 (0)