7
7
"context"
8
8
"fmt"
9
9
"io"
10
+ "net/http"
10
11
"os"
11
12
"regexp"
12
13
"strings"
20
21
literalSecret string
21
22
secretFile string
22
23
trimSecret bool
24
+ replaceSecret bool
23
25
)
24
26
25
27
// secretCreateCmd represents the secretCreate command
@@ -32,10 +34,23 @@ var secretCreateCmd = &cobra.Command{
32
34
[--tls-no-verify]` ,
33
35
Short : "Create a new secret" ,
34
36
Long : `The create command creates a new secret from file, literal or STDIN` ,
35
- Example : `faas-cli secret create secret-name --from-literal=secret-value
36
- faas-cli secret create secret-name --from-literal=secret-value --gateway=http://127.0.0.1:8080
37
- faas-cli secret create secret-name --from-file=/path/to/secret/file --gateway=http://127.0.0.1:8080
38
- cat /path/to/secret/file | faas-cli secret create secret-name` ,
37
+ Example : ` # Create a secret from a literal value in the default namespace
38
+ faas-cli secret create NAME --from-literal=VALUE
39
+
40
+ # Create a secret in a specific namespace
41
+ faas-cli secret create NAME --from-literal=VALUE \
42
+ --namespace=NS
43
+
44
+ # Create the secret from a file with a given gateway
45
+ faas-cli secret create NAME --from-file=PATH \
46
+ --gateway=http://127.0.0.1:8080
47
+
48
+ # Create the secret from a STDIN pipe
49
+ cat ./secret.txt | faas-cli secret create NAME
50
+
51
+ # Force an update if the secret already exists
52
+ faas-cli secret create NAME --from-file PATH --update
53
+ ` ,
39
54
RunE : runSecretCreate ,
40
55
PreRunE : preRunSecretCreate ,
41
56
}
@@ -48,6 +63,7 @@ func init() {
48
63
secretCreateCmd .Flags ().StringVarP (& gateway , "gateway" , "g" , defaultGateway , "Gateway URL starting with http(s)://" )
49
64
secretCreateCmd .Flags ().StringVarP (& token , "token" , "k" , "" , "Pass a JWT token to use instead of basic auth" )
50
65
secretCreateCmd .Flags ().StringVarP (& functionNamespace , "namespace" , "n" , "" , "Namespace of the function" )
66
+ secretCreateCmd .Flags ().BoolVar (& replaceSecret , "replace" , false , "Replace the secret if it already exists using an update" )
51
67
52
68
secretCmd .AddCommand (secretCreateCmd )
53
69
}
@@ -130,8 +146,12 @@ func runSecretCreate(cmd *cobra.Command, args []string) error {
130
146
}
131
147
132
148
fmt .Printf ("Creating secret: %s.%s\n " , secret .Name , functionNamespace )
133
- _ , output := client .CreateSecret (context .Background (), secret )
134
- fmt .Printf (output )
149
+ status , output := client .CreateSecret (context .Background (), secret )
150
+ if status == http .StatusConflict && replaceSecret {
151
+ fmt .Printf ("Secret %s already exists, updating...\n " , secret .Name )
152
+ _ , output = client .UpdateSecret (context .Background (), secret )
153
+ }
154
+ fmt .Print (output )
135
155
136
156
return nil
137
157
}
0 commit comments