GeezerVison is showing github.com — Add secrets · PastureStack/cli@9c34bf4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/inspect.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | ) | 9 | 9 |
| 10 | 10 | var ( | 11 | | - inspectTypes = []string{"service", "container", "host", "project", "stack", "volume"} | | 11 | + inspectTypes = []string{"service", "container", "host", "project", "stack", "volume", "secret"} | 12 | 12 | ) | 13 | 13 |
| 14 | 14 | func InspectCommand() cli.Command { | |
Collapse file Copy file name to clipboardExpand all lines: cmd/rm.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | ) | 10 | 10 |
| 11 | 11 | var ( | 12 | | - rmTypes = []string{"service", "container", "stack", "host", "volume"} | | 12 | + rmTypes = []string{"service", "container", "stack", "host", "volume", "secret"} | 13 | 13 | ) | 14 | 14 |
| 15 | 15 | func RmCommand() cli.Command { | |
Collapse file Copy file name to clipboard +130Lines changed: 130 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package cmd | | 2 | + | | 3 | +import ( | | 4 | + "encoding/base64" | | 5 | + "fmt" | | 6 | + "io" | | 7 | + "io/ioutil" | | 8 | + "os" | | 9 | + | | 10 | + "github.com/Sirupsen/logrus" | | 11 | + "github.com/rancher/go-rancher/v2" | | 12 | + "github.com/urfave/cli" | | 13 | +) | | 14 | + | | 15 | +func SecretCommand() cli.Command { | | 16 | + secretLsFlags := []cli.Flag{ | | 17 | + cli.BoolFlag{ | | 18 | + Name: "quiet,q", | | 19 | + Usage: "Only display IDs", | | 20 | + }, | | 21 | + cli.StringFlag{ | | 22 | + Name: "format", | | 23 | + Usage: "'json' or Custom format: {{.Id}} {{.Name}}", | | 24 | + }, | | 25 | + } | | 26 | + | | 27 | + return cli.Command{ | | 28 | + Name: "secrets", | | 29 | + ShortName: "secret", | | 30 | + Usage: "Operations on secrets", | | 31 | + Action: defaultAction(secretLs), | | 32 | + Flags: secretLsFlags, | | 33 | + Subcommands: []cli.Command{ | | 34 | + cli.Command{ | | 35 | + Name: "ls", | | 36 | + Usage: "List secrets", | | 37 | + Description: "\nLists all secrets in the current $RANCHER_ENVIRONMENT. Use `--env <envID>` or `--env <envName>` to select a different environment.\n\nExample:\n\t$ rancher secrets ls\n\t$ rancher --env 1a5 secrets ls\n", | | 38 | + ArgsUsage: "None", | | 39 | + Action: secretLs, | | 40 | + Flags: secretLsFlags, | | 41 | + }, | | 42 | + cli.Command{ | | 43 | + Name: "create", | | 44 | + Usage: "Create a secret", | | 45 | + Description: "\nCreate all secret in the current $RANCHER_ENVIRONMENT. Use `--env <envID>` or `--env <envName>` to select a different environment.\n\nExample:\n\t$ rancher secret create my-name file-with-secret\n", | | 46 | + ArgsUsage: "NAME [FILE|-]", | | 47 | + Action: secretCreate, | | 48 | + Flags: []cli.Flag{}, | | 49 | + }, | | 50 | + }, | | 51 | + } | | 52 | +} | | 53 | + | | 54 | +type SecretData struct { | | 55 | + ID string | | 56 | + Secret client.Secret | | 57 | +} | | 58 | + | | 59 | +func secretLs(ctx *cli.Context) error { | | 60 | + c, err := GetClient(ctx) | | 61 | + if err != nil { | | 62 | + return err | | 63 | + } | | 64 | + | | 65 | + collection, err := c.Secret.List(defaultListOpts(ctx)) | | 66 | + if err != nil { | | 67 | + return err | | 68 | + } | | 69 | + | | 70 | + writer := NewTableWriter([][]string{ | | 71 | + {"ID", "ID"}, | | 72 | + {"NAME", "Secret.Name"}, | | 73 | + {"CREATED", "Secret.Created"}, | | 74 | + }, ctx) | | 75 | + | | 76 | + defer writer.Close() | | 77 | + | | 78 | + for _, item := range collection.Data { | | 79 | + writer.Write(&SecretData{ | | 80 | + ID: item.Id, | | 81 | + Secret: item, | | 82 | + }) | | 83 | + } | | 84 | + | | 85 | + return writer.Err() | | 86 | +} | | 87 | + | | 88 | +func secretCreate(ctx *cli.Context) error { | | 89 | + c, err := GetClient(ctx) | | 90 | + if err != nil { | | 91 | + return err | | 92 | + } | | 93 | + | | 94 | + w, err := NewWaiter(ctx) | | 95 | + if err != nil { | | 96 | + return err | | 97 | + } | | 98 | + | | 99 | + if ctx.NArg() != 2 { | | 100 | + return fmt.Errorf("both NAME and FILE|- are required") | | 101 | + } | | 102 | + | | 103 | + name, file := ctx.Args()[0], ctx.Args()[1] | | 104 | + var input io.Reader | | 105 | + | | 106 | + if file == "-" { | | 107 | + input = os.Stdin | | 108 | + } else { | | 109 | + input, err = os.Open(file) | | 110 | + if os.IsNotExist(err) { | | 111 | + logrus.Errorf("Failed to find %s, argument must be a file or -", file) | | 112 | + } | | 113 | + } | | 114 | + | | 115 | + content, err := ioutil.ReadAll(input) | | 116 | + if err != nil { | | 117 | + return err | | 118 | + } | | 119 | + | | 120 | + secret, err := c.Secret.Create(&client.Secret{ | | 121 | + Name: name, | | 122 | + Value: base64.StdEncoding.EncodeToString(content), | | 123 | + }) | | 124 | + if err != nil { | | 125 | + return err | | 126 | + } | | 127 | + | | 128 | + w.Add(secret.Id) | | 129 | + return w.Wait() | | 130 | +} |
Collapse file Copy file name to clipboardExpand all lines: main.go +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,6 +136,7 @@ func mainErr() error { | 136 | 136 | cmd.RmCommand(), | 137 | 137 | cmd.RunCommand(), | 138 | 138 | cmd.ScaleCommand(), | | 139 | + cmd.SecretCommand(), | 139 | 140 | cmd.SSHCommand(), | 140 | 141 | cmd.StackCommand(), | 141 | 142 | cmd.StartCommand(), | |
|