Skip to content

Commit ffd997c

Browse files
authored
feat: add noninteractive path to func config envs remove (knative#2879)
1 parent c990659 commit ffd997c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

cmd/config_envs.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ the current directory or from the directory specified with --path.
4848
cmd.Flags().StringP("output", "o", "human", "Output format (human|json) ($FUNC_OUTPUT)")
4949

5050
configEnvsAddCmd := NewConfigEnvsAddCmd(loadSaver)
51-
configEnvsRemoveCmd := NewConfigEnvsRemoveCmd()
51+
configEnvsRemoveCmd := NewConfigEnvsRemoveCmd(loadSaver)
5252

5353
addPathFlag(cmd)
5454
addPathFlag(configEnvsAddCmd)
@@ -139,8 +139,8 @@ set environment variable from a secret
139139
return cmd
140140
}
141141

142-
func NewConfigEnvsRemoveCmd() *cobra.Command {
143-
return &cobra.Command{
142+
func NewConfigEnvsRemoveCmd(loadSaver functionLoaderSaver) *cobra.Command {
143+
cmd := &cobra.Command{
144144
Use: "remove",
145145
Short: "Remove environment variable from the function configuration",
146146
Long: `Remove environment variable from the function configuration
@@ -150,17 +150,40 @@ in the current directory or from the directory specified with --path.
150150
`,
151151
Aliases: []string{"rm"},
152152
SuggestFor: []string{"del", "delete", "rmeove"},
153-
PreRunE: bindEnv("path", "verbose"),
153+
PreRunE: bindEnv("path", "name", "verbose"),
154154
RunE: func(cmd *cobra.Command, args []string) (err error) {
155-
function, err := initConfigCommand(defaultLoaderSaver)
155+
function, err := initConfigCommand(loadSaver)
156156
if err != nil {
157157
return
158158
}
159159

160+
var name string
161+
if cmd.Flags().Changed("name") {
162+
s, e := cmd.Flags().GetString("name")
163+
if e != nil {
164+
return e
165+
}
166+
name = s
167+
}
168+
169+
if name != "" {
170+
envs := []fn.Env{}
171+
for _, v := range function.Run.Envs {
172+
if *v.Name != name {
173+
envs = append(envs, v)
174+
}
175+
}
176+
function.Run.Envs = envs
177+
return loadSaver.Save(function)
178+
}
179+
160180
return runRemoveEnvsPrompt(function)
161181
},
162182
}
163183

184+
cmd.Flags().StringP("name", "", "", "Name of the environment variable.")
185+
return cmd
186+
164187
}
165188

166189
func listEnvs(f fn.Function, w io.Writer, outputFormat Format) error {

docs/reference/func_config_envs_remove.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func config envs remove
1818

1919
```
2020
-h, --help help for remove
21+
--name string Name of the environment variable.
2122
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
2223
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
2324
```

0 commit comments

Comments
 (0)