Skip to content

Commit 84d2ad0

Browse files
authored
feat: add noninteractive flow to labels config (knative#2886)
1 parent 1b83f0b commit 84d2ad0

File tree

7 files changed

+115
-29
lines changed

7 files changed

+115
-29
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ test-integration: ## Run integration tests using an available cluster.
219219
go test -tags integration -timeout 30m --coverprofile=coverage.txt ./... -v
220220

221221
.PHONY: func-instrumented
222-
func-instrumented: ## Func binary that is instrumented for e2e tests
222+
func-instrumented: # func binary instrumented with coverage reporting
223223
env CGO_ENABLED=1 go build -cover -o func ./cmd/$(BIN)
224224

225225
.PHONY: test-e2e

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) (err error) {
139139
case "Environment variables":
140140
err = listEnvs(function, cmd.OutOrStdout(), Human)
141141
case "Labels":
142-
listLabels(function)
142+
err = listLabels(function, cmd.OutOrStdout(), Human)
143143
case "Git":
144144
err = runConfigGitCmd(cmd, NewClient)
145145
}

cmd/config_labels.go

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package cmd
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"io"
68
"os"
79

810
"github.com/AlecAivazis/survey/v2"
11+
"github.com/ory/viper"
912
"github.com/spf13/cobra"
1013

1114
"knative.dev/func/pkg/config"
@@ -24,16 +27,14 @@ the current directory or from the directory specified with --path.
2427
`,
2528
Aliases: []string{"label"},
2629
SuggestFor: []string{"albels", "abels"},
27-
PreRunE: bindEnv("path", "verbose"),
30+
PreRunE: bindEnv("path", "output", "verbose"),
2831
RunE: func(cmd *cobra.Command, args []string) (err error) {
2932
function, err := initConfigCommand(loaderSaver)
3033
if err != nil {
3134
return
3235
}
3336

34-
listLabels(function)
35-
36-
return
37+
return listLabels(function, cmd.OutOrStdout(), Format(viper.GetString("output")))
3738
},
3839
}
3940

@@ -42,20 +43,54 @@ the current directory or from the directory specified with --path.
4243
Short: "Add labels to the function configuration",
4344
Long: `Add labels to the function configuration
4445
45-
Interactive prompt to add labels to the function project in the current
46-
directory or from the directory specified with --path.
46+
If label is not set explicitly by flag, interactive prompt is used.
4747
4848
The label can be set directly from a value or from an environment variable on
4949
the local machine.
5050
`,
51+
Example: `# set label directly
52+
{{rootCmdUse}} config labels add --name=Foo --value=Bar
53+
54+
# set label from local env $FOO
55+
{{rootCmdUse}} config labels add --name=Foo --value='{{"{{"}} env:FOO {{"}}"}}'`,
5156
SuggestFor: []string{"ad", "create", "insert", "append"},
52-
PreRunE: bindEnv("path", "verbose"),
57+
PreRunE: bindEnv("path", "name", "value", "verbose"),
5358
RunE: func(cmd *cobra.Command, args []string) (err error) {
5459
function, err := initConfigCommand(loaderSaver)
5560
if err != nil {
5661
return
5762
}
5863

64+
var np *string
65+
var vp *string
66+
67+
if cmd.Flags().Changed("name") {
68+
s, e := cmd.Flags().GetString("name")
69+
if e != nil {
70+
return e
71+
}
72+
np = &s
73+
}
74+
if cmd.Flags().Changed("value") {
75+
s, e := cmd.Flags().GetString("value")
76+
if e != nil {
77+
return e
78+
}
79+
vp = &s
80+
}
81+
82+
if np != nil && vp != nil {
83+
if err := utils.ValidateLabelKey(*np); err != nil {
84+
return err
85+
}
86+
if err := utils.ValidateLabelValue(*vp); err != nil {
87+
return err
88+
}
89+
90+
function.Deploy.Labels = append(function.Deploy.Labels, fn.Label{Key: np, Value: vp})
91+
return loaderSaver.Save(function)
92+
}
93+
5994
return runAddLabelsPrompt(cmd.Context(), function, loaderSaver)
6095
},
6196
}
@@ -70,13 +105,33 @@ directory or from the directory specified with --path.
70105
`,
71106
Aliases: []string{"rm"},
72107
SuggestFor: []string{"del", "delete", "rmeove"},
73-
PreRunE: bindEnv("path", "verbose"),
108+
PreRunE: bindEnv("path", "name", "verbose"),
74109
RunE: func(cmd *cobra.Command, args []string) (err error) {
75110
function, err := initConfigCommand(loaderSaver)
76111
if err != nil {
77112
return
78113
}
79114

115+
var name string
116+
if cmd.Flags().Changed("name") {
117+
s, e := cmd.Flags().GetString("name")
118+
if e != nil {
119+
return e
120+
}
121+
name = s
122+
}
123+
124+
if name != "" {
125+
labels := []fn.Label{}
126+
for _, v := range function.Deploy.Labels {
127+
if v.Key == nil || *v.Key != name {
128+
labels = append(labels, v)
129+
}
130+
}
131+
function.Deploy.Labels = labels
132+
return loaderSaver.Save(function)
133+
}
134+
80135
return runRemoveLabelsPrompt(function, loaderSaver)
81136
},
82137
}
@@ -86,6 +141,12 @@ directory or from the directory specified with --path.
86141
fmt.Fprintf(configLabelsCmd.OutOrStdout(), "error loading config at '%v'. %v\n", config.File(), err)
87142
}
88143

144+
// Add flags
145+
configLabelsCmd.Flags().StringP("output", "o", "human", "Output format (human|json)")
146+
configLabelsAddCmd.Flags().StringP("name", "", "", "Name of the label.")
147+
configLabelsAddCmd.Flags().StringP("value", "", "", "Value of the label.")
148+
configLabelsRemoveCmd.Flags().StringP("name", "", "", "Name of the label.")
149+
89150
addPathFlag(configLabelsCmd)
90151
addPathFlag(configLabelsAddCmd)
91152
addPathFlag(configLabelsRemoveCmd)
@@ -99,15 +160,27 @@ directory or from the directory specified with --path.
99160
return configLabelsCmd
100161
}
101162

102-
func listLabels(f fn.Function) {
103-
if len(f.Deploy.Labels) == 0 {
104-
fmt.Println("There aren't any configured labels")
105-
return
106-
}
163+
func listLabels(f fn.Function, w io.Writer, outputFormat Format) error {
164+
switch outputFormat {
165+
case Human:
166+
if len(f.Deploy.Labels) == 0 {
167+
_, err := fmt.Fprintln(w, "No labels defined")
168+
return err
169+
}
107170

108-
fmt.Println("Configured labels:")
109-
for _, e := range f.Deploy.Labels {
110-
fmt.Println(" - ", e.String())
171+
fmt.Fprintln(w, "Labels:")
172+
for _, e := range f.Deploy.Labels {
173+
_, err := fmt.Fprintln(w, " - ", e.String())
174+
if err != nil {
175+
return err
176+
}
177+
}
178+
return nil
179+
case JSON:
180+
enc := json.NewEncoder(w)
181+
return enc.Encode(f.Deploy.Labels)
182+
default:
183+
return fmt.Errorf("invalid format: %v", outputFormat)
111184
}
112185
}
113186

cmd/config_labels_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ func TestListLabels(t *testing.T) {
172172
}()
173173

174174
expected := []string{
175-
`Configured labels:`,
176-
`- Label with key "a" and value "b"`,
177-
`- Label with key "c" and value "d"`,
175+
`Labels:`,
176+
` - Label with key "a" and value "b"`,
177+
` - Label with key "c" and value "d"`,
178178
}
179179

180180
// prevents the ExpectString() function from waiting indefinitely

docs/reference/func_config_labels.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ func config labels
1717
### Options
1818

1919
```
20-
-h, --help help for labels
21-
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
22-
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
20+
-h, --help help for labels
21+
-o, --output string Output format (human|json) (default "human")
22+
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
23+
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
2324
```
2425

2526
### SEE ALSO

docs/reference/func_config_labels_add.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Add labels to the function configuration
66

77
Add labels to the function configuration
88

9-
Interactive prompt to add labels to the function project in the current
10-
directory or from the directory specified with --path.
9+
If label is not set explicitly by flag, interactive prompt is used.
1110

1211
The label can be set directly from a value or from an environment variable on
1312
the local machine.
@@ -17,12 +16,24 @@ the local machine.
1716
func config labels add
1817
```
1918

19+
### Examples
20+
21+
```
22+
# set label directly
23+
func config labels add --name=Foo --value=Bar
24+
25+
# set label from local env $FOO
26+
func config labels add --name=Foo --value='{{ env:FOO }}'
27+
```
28+
2029
### Options
2130

2231
```
23-
-h, --help help for add
24-
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
25-
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
32+
-h, --help help for add
33+
--name string Name of the label.
34+
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
35+
--value string Value of the label.
36+
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
2637
```
2738

2839
### SEE ALSO

docs/reference/func_config_labels_remove.md

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

1919
```
2020
-h, --help help for remove
21+
--name string Name of the label.
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)