@@ -2,10 +2,13 @@ package cmd
22
33import (
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
4848The label can be set directly from a value or from an environment variable on
4949the 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
0 commit comments