EnvVarNames offers completion for environment-variable names. This completion can be used for "--env" and "--build-arg" flags, which allow obtaining the value of the given environment-variable if present in the local environment, so we only should complete the names of the environment variables, and
()
| 156 | // docker run --rm --env MY_VAR alpine printenv MY_VAR |
| 157 | // hello |
| 158 | func EnvVarNames() cobra.CompletionFunc { |
| 159 | return Unique(func(_ *cobra.Command, _ []string, _ string) (names []string, _ cobra.ShellCompDirective) { |
| 160 | envs := os.Environ() |
| 161 | names = make([]string, 0, len(envs)) |
| 162 | for _, env := range envs { |
| 163 | name, _, _ := strings.Cut(env, "=") |
| 164 | names = append(names, name) |
| 165 | } |
| 166 | return names, cobra.ShellCompDirectiveNoFileComp |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | // FromList offers completion for the given list of options. |
| 171 | func FromList(options ...string) cobra.CompletionFunc { |
searching dependent graphs…