@@ -2,10 +2,13 @@ package cmd
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
7
+ "io"
6
8
"os"
7
9
8
10
"github.com/AlecAivazis/survey/v2"
11
+ "github.com/ory/viper"
9
12
"github.com/spf13/cobra"
10
13
11
14
"knative.dev/func/pkg/config"
@@ -24,16 +27,14 @@ the current directory or from the directory specified with --path.
24
27
` ,
25
28
Aliases : []string {"label" },
26
29
SuggestFor : []string {"albels" , "abels" },
27
- PreRunE : bindEnv ("path" , "verbose" ),
30
+ PreRunE : bindEnv ("path" , "output" , " verbose" ),
28
31
RunE : func (cmd * cobra.Command , args []string ) (err error ) {
29
32
function , err := initConfigCommand (loaderSaver )
30
33
if err != nil {
31
34
return
32
35
}
33
36
34
- listLabels (function )
35
-
36
- return
37
+ return listLabels (function , cmd .OutOrStdout (), Format (viper .GetString ("output" )))
37
38
},
38
39
}
39
40
@@ -42,20 +43,54 @@ the current directory or from the directory specified with --path.
42
43
Short : "Add labels to the function configuration" ,
43
44
Long : `Add labels to the function configuration
44
45
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.
47
47
48
48
The label can be set directly from a value or from an environment variable on
49
49
the local machine.
50
50
` ,
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 {{"}}"}}'` ,
51
56
SuggestFor : []string {"ad" , "create" , "insert" , "append" },
52
- PreRunE : bindEnv ("path" , "verbose" ),
57
+ PreRunE : bindEnv ("path" , "name" , "value" , " verbose" ),
53
58
RunE : func (cmd * cobra.Command , args []string ) (err error ) {
54
59
function , err := initConfigCommand (loaderSaver )
55
60
if err != nil {
56
61
return
57
62
}
58
63
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
+
59
94
return runAddLabelsPrompt (cmd .Context (), function , loaderSaver )
60
95
},
61
96
}
@@ -70,13 +105,33 @@ directory or from the directory specified with --path.
70
105
` ,
71
106
Aliases : []string {"rm" },
72
107
SuggestFor : []string {"del" , "delete" , "rmeove" },
73
- PreRunE : bindEnv ("path" , "verbose" ),
108
+ PreRunE : bindEnv ("path" , "name" , " verbose" ),
74
109
RunE : func (cmd * cobra.Command , args []string ) (err error ) {
75
110
function , err := initConfigCommand (loaderSaver )
76
111
if err != nil {
77
112
return
78
113
}
79
114
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
+
80
135
return runRemoveLabelsPrompt (function , loaderSaver )
81
136
},
82
137
}
@@ -86,6 +141,12 @@ directory or from the directory specified with --path.
86
141
fmt .Fprintf (configLabelsCmd .OutOrStdout (), "error loading config at '%v'. %v\n " , config .File (), err )
87
142
}
88
143
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
+
89
150
addPathFlag (configLabelsCmd )
90
151
addPathFlag (configLabelsAddCmd )
91
152
addPathFlag (configLabelsRemoveCmd )
@@ -99,15 +160,27 @@ directory or from the directory specified with --path.
99
160
return configLabelsCmd
100
161
}
101
162
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
+ }
107
170
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 )
111
184
}
112
185
}
113
186
0 commit comments