|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "cdr.dev/coder-cli/internal/entclient" |
| 8 | + "cdr.dev/coder-cli/internal/xcli" |
| 9 | + "github.com/spf13/pflag" |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "go.coder.com/flog" |
| 13 | + |
| 14 | + "go.coder.com/cli" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + _ cli.FlaggedCommand = secretsCmd{} |
| 19 | + _ cli.ParentCommand = secretsCmd{} |
| 20 | + |
| 21 | + _ cli.FlaggedCommand = &listSecretsCmd{} |
| 22 | + _ cli.FlaggedCommand = &addSecretCmd{} |
| 23 | +) |
| 24 | + |
| 25 | +type secretsCmd struct { |
| 26 | +} |
| 27 | + |
| 28 | +func (cmd secretsCmd) Spec() cli.CommandSpec { |
| 29 | + return cli.CommandSpec{ |
| 30 | + Name: "secrets", |
| 31 | + Desc: "interact with secrets owned by the authenticated user", |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (cmd secretsCmd) Run(fl *pflag.FlagSet) { |
| 36 | + exitUsage(fl) |
| 37 | +} |
| 38 | + |
| 39 | +func (cmd secretsCmd) RegisterFlags(fl *pflag.FlagSet) {} |
| 40 | + |
| 41 | +func (cmd secretsCmd) Subcommands() []cli.Command { |
| 42 | + return []cli.Command{ |
| 43 | + &listSecretsCmd{}, |
| 44 | + &viewSecretsCmd{}, |
| 45 | + &addSecretCmd{}, |
| 46 | + &deleteSecretsCmd{}, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +type listSecretsCmd struct{} |
| 51 | + |
| 52 | +func (cmd listSecretsCmd) Spec() cli.CommandSpec { |
| 53 | + return cli.CommandSpec{ |
| 54 | + Name: "ls", |
| 55 | + Desc: "list all secrets owned by the authenticated user", |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (cmd listSecretsCmd) Run(fl *pflag.FlagSet) { |
| 60 | + client := requireAuth() |
| 61 | + |
| 62 | + secrets, err := client.Secrets() |
| 63 | + xcli.RequireSuccess(err, "failed to get secrets: %v", err) |
| 64 | + |
| 65 | + w := xcli.HumanReadableWriter() |
| 66 | + if len(secrets) > 0 { |
| 67 | + _, err := fmt.Fprintln(w, xcli.TabDelimitedStructHeaders(secrets[0])) |
| 68 | + xcli.RequireSuccess(err, "failed to write: %v", err) |
| 69 | + } |
| 70 | + for _, s := range secrets { |
| 71 | + s.Value = "******" // value is omitted from bulk responses |
| 72 | + |
| 73 | + _, err = fmt.Fprintln(w, xcli.TabDelimitedStructValues(s)) |
| 74 | + xcli.RequireSuccess(err, "failed to write: %v", err) |
| 75 | + } |
| 76 | + err = w.Flush() |
| 77 | + xcli.RequireSuccess(err, "failed to flush writer: %v", err) |
| 78 | +} |
| 79 | + |
| 80 | +func (cmd *listSecretsCmd) RegisterFlags(fl *pflag.FlagSet) {} |
| 81 | + |
| 82 | +type viewSecretsCmd struct{} |
| 83 | + |
| 84 | +func (cmd viewSecretsCmd) Spec() cli.CommandSpec { |
| 85 | + return cli.CommandSpec{ |
| 86 | + Name: "view", |
| 87 | + Usage: "[secret_name]", |
| 88 | + Desc: "view a secret owned by the authenticated user", |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (cmd viewSecretsCmd) Run(fl *pflag.FlagSet) { |
| 93 | + var ( |
| 94 | + client = requireAuth() |
| 95 | + name = fl.Arg(0) |
| 96 | + ) |
| 97 | + |
| 98 | + secret, err := client.SecretByName(name) |
| 99 | + xcli.RequireSuccess(err, "failed to get secret by name: %v", err) |
| 100 | + |
| 101 | + _, err = fmt.Fprintln(os.Stdout, secret.Value) |
| 102 | + xcli.RequireSuccess(err, "failed to write: %v", err) |
| 103 | +} |
| 104 | + |
| 105 | +type addSecretCmd struct { |
| 106 | + name, value, description string |
| 107 | +} |
| 108 | + |
| 109 | +func (cmd *addSecretCmd) Validate() (e []error) { |
| 110 | + if cmd.name == "" { |
| 111 | + e = append(e, xerrors.New("--name is a required flag")) |
| 112 | + } |
| 113 | + if cmd.value == "" { |
| 114 | + e = append(e, xerrors.New("--value is a required flag")) |
| 115 | + } |
| 116 | + return e |
| 117 | +} |
| 118 | + |
| 119 | +func (cmd *addSecretCmd) Spec() cli.CommandSpec { |
| 120 | + return cli.CommandSpec{ |
| 121 | + Name: "add", |
| 122 | + Usage: `--name MYSQL_KEY --value 123456 --description "MySQL credential for database access"`, |
| 123 | + Desc: "insert a new secret", |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (cmd *addSecretCmd) Run(fl *pflag.FlagSet) { |
| 128 | + var ( |
| 129 | + client = requireAuth() |
| 130 | + ) |
| 131 | + xcli.Validate(cmd) |
| 132 | + |
| 133 | + err := client.InsertSecret(entclient.InsertSecretReq{ |
| 134 | + Name: cmd.name, |
| 135 | + Value: cmd.value, |
| 136 | + Description: cmd.description, |
| 137 | + }) |
| 138 | + xcli.RequireSuccess(err, "failed to insert secret: %v", err) |
| 139 | +} |
| 140 | + |
| 141 | +func (cmd *addSecretCmd) RegisterFlags(fl *pflag.FlagSet) { |
| 142 | + fl.StringVar(&cmd.name, "name", "", "the name of the secret") |
| 143 | + fl.StringVar(&cmd.value, "value", "", "the value of the secret") |
| 144 | + fl.StringVar(&cmd.description, "description", "", "a description of the secret") |
| 145 | +} |
| 146 | + |
| 147 | +type deleteSecretsCmd struct{} |
| 148 | + |
| 149 | +func (cmd *deleteSecretsCmd) Spec() cli.CommandSpec { |
| 150 | + return cli.CommandSpec{ |
| 151 | + Name: "rm", |
| 152 | + Usage: "[secret_name]", |
| 153 | + Desc: "remove a secret by name", |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func (cmd *deleteSecretsCmd) Run(fl *pflag.FlagSet) { |
| 158 | + var ( |
| 159 | + client = requireAuth() |
| 160 | + name = fl.Arg(0) |
| 161 | + ) |
| 162 | + |
| 163 | + err := client.DeleteSecretByName(name) |
| 164 | + xcli.RequireSuccess(err, "failed to delete secret: %v", err) |
| 165 | + |
| 166 | + flog.Info("Successfully deleted secret %q", name) |
| 167 | +} |
0 commit comments