forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
75 lines (66 loc) · 1.79 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package secret
import (
"errors"
"fmt"
"io"
"os"
"github.com/moby/swarmkit/swarmd/cmd/swarmctl/common"
"github.com/moby/swarmkit/v2/api"
"github.com/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create <secret name>",
Short: "Create a secret",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New(
"create command takes a unique secret name as an argument, and accepts secret data via stdin or via a file")
}
flags := cmd.Flags()
var (
secretData []byte
err error
driver string
)
driver, err = flags.GetString("driver")
if err != nil {
return fmt.Errorf("Error reading secret driver %s", err.Error())
}
if flags.Changed("file") {
filename, err := flags.GetString("file")
if err != nil {
return err
}
secretData, err = os.ReadFile(filename)
if err != nil {
return fmt.Errorf("Error reading from file '%s': %s", filename, err.Error())
}
} else if driver == "" {
secretData, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Error reading content from STDIN: %s", err.Error())
}
}
client, err := common.Dial(cmd)
if err != nil {
return err
}
spec := &api.SecretSpec{
Annotations: api.Annotations{Name: args[0]},
Data: secretData,
}
if driver != "" {
spec.Driver = &api.Driver{Name: driver}
}
resp, err := client.CreateSecret(common.Context(cmd), &api.CreateSecretRequest{Spec: spec})
if err != nil {
return err
}
fmt.Println(resp.Secret.ID)
return nil
},
}
func init() {
createCmd.Flags().StringP("file", "f", "", "Rather than read the secret from STDIN, read from the given file")
createCmd.Flags().StringP("driver", "d", "", "Rather than read the secret from STDIN, read the value from an external secret driver")
}