Skip to content
Myr

Replace all non-alphanumerics in active help env var program prefix (#1940)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Replace all non-alphanumerics in active help env var program prefix There are other characters besides the dash that are fine in program names, but are problematic in environment variable names. These include (but are not limited to) period, space, and non-ASCII letters. * Another change in docs to mention non-ASCII-alphanumeric instead of just dash

authored by Ville Skyttä · committed by GitHub · parent 00b68a1 · Browse files · 2 files+9 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+9 added−5 removed

  • other:+9 added−5 removed

2 files+9 added−5 removed

modified:site/content/active_help.md+2 added-2 removed
modified:active_help.go+7 added-3 removed
15 unmodified lines
1617import (18 "fmt"19 "os"20 "regexp"21 "strings"22)2324const (
4 unmodified lines
29 activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP"30 activeHelpGlobalDisable = "0"31)3233var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)3435// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.36// Such strings will be processed by the completion script and will be shown as ActiveHelp37// to the user.38// The array parameter should be the array that will contain the completions.
5 unmodified lines
44}4546// GetActiveHelpConfig returns the value of the ActiveHelp environment variable47// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper45// case, with all - replaced by _.48// case, with all non-ASCII-alphanumeric characters replaced by `_`.49// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP50// is set to "0".51func GetActiveHelpConfig(cmd *Command) string {52 activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
4 unmodified lines
57}5859// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment60// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the58// root command in upper case, with all - replaced by _.61// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.62func activeHelpEnvVar(name string) string {63 // This format should not be changed: users will be using it explicitly.64 activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))62 return strings.ReplaceAll(activeHelpEnvVar, "-", "_")65 activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")66 return activeHelpEnvVar67}