feat(influx): add jsonout and hide headers support to secret cmd

pull/17469/head
Johnny Steenbergen 2020-03-27 10:31:26 -07:00 committed by Johnny Steenbergen
parent ba57fa5a66
commit 08fbdbff1a
1 changed files with 101 additions and 44 deletions

View File

@ -24,9 +24,11 @@ type cmdSecretBuilder struct {
svcFn secretSVCsFn svcFn secretSVCsFn
key string json bool
value string hideHeaders bool
org organization key string
value string
org organization
} }
func newCmdSecretBuilder(svcsFn secretSVCsFn, opt genericCLIOpts) *cmdSecretBuilder { func newCmdSecretBuilder(svcsFn secretSVCsFn, opt genericCLIOpts) *cmdSecretBuilder {
@ -51,21 +53,12 @@ func (b *cmdSecretBuilder) cmd() *cobra.Command {
func (b *cmdSecretBuilder) cmdUpdate() *cobra.Command { func (b *cmdSecretBuilder) cmdUpdate() *cobra.Command {
cmd := b.newCmd("update", b.cmdUpdateRunEFn, true) cmd := b.newCmd("update", b.cmdUpdateRunEFn, true)
cmd.Short = "Update secret" cmd.Short = "Update secret"
cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)") cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)")
cmd.Flags().StringVarP(&b.value, "value", "v", "", "Optional secret value for scripting convenience, using this might expose the secret to your local history") cmd.Flags().StringVarP(&b.value, "value", "v", "", "Optional secret value for scripting convenience, using this might expose the secret to your local history")
cmd.MarkFlagRequired("key") cmd.MarkFlagRequired("key")
b.org.register(cmd, false) b.org.register(cmd, false)
b.registerPrintFlags(cmd)
return cmd
}
func (b *cmdSecretBuilder) cmdDelete() *cobra.Command {
cmd := b.newCmd("delete", b.cmdDeleteRunEFn, true)
cmd.Short = "Delete secret"
cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)")
cmd.MarkFlagRequired("key")
b.org.register(cmd, false)
return cmd return cmd
} }
@ -86,27 +79,38 @@ func (b *cmdSecretBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er
Writer: b.genericCLIOpts.w, Writer: b.genericCLIOpts.w,
Reader: b.genericCLIOpts.in, Reader: b.genericCLIOpts.in,
} }
var secret string var secretVal string
if b.value != "" { if b.value != "" {
secret = b.value secretVal = b.value
} else { } else {
secret = getSecretFn(ui) secretVal = getSecretFn(ui)
} }
if err := scrSVC.PatchSecrets(ctx, orgID, map[string]string{b.key: secret}); err != nil { err = scrSVC.PatchSecrets(ctx, orgID, map[string]string{
b.key: secretVal,
})
if err != nil {
return fmt.Errorf("failed to update secret with key %q: %v", b.key, err) return fmt.Errorf("failed to update secret with key %q: %v", b.key, err)
} }
w := b.newTabWriter() return b.printSecrets(secretPrintOpt{
w.WriteHeaders("Key", "OrgID", "Updated") secret: secret{
w.Write(map[string]interface{}{ key: b.key,
"Key": b.key, orgID: orgID,
"OrgID": orgID, },
"Updated": true,
}) })
w.Flush() }
return nil func (b *cmdSecretBuilder) cmdDelete() *cobra.Command {
cmd := b.newCmd("delete", b.cmdDeleteRunEFn, true)
cmd.Short = "Delete secret"
cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)")
cmd.MarkFlagRequired("key")
b.org.register(cmd, false)
b.registerPrintFlags(cmd)
return cmd
} }
func (b *cmdSecretBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) error { func (b *cmdSecretBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) error {
@ -124,29 +128,27 @@ func (b *cmdSecretBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
return fmt.Errorf("failed to delete secret with key %q: %v", b.key, err) return fmt.Errorf("failed to delete secret with key %q: %v", b.key, err)
} }
w := b.newTabWriter() return b.printSecrets(secretPrintOpt{
w.WriteHeaders("Key", "OrgID", "Deleted") deleted: true,
w.Write(map[string]interface{}{ secret: secret{
"Key": b.key, key: b.key,
"OrgID": orgID, orgID: orgID,
"Deleted": true, },
}) })
w.Flush()
return nil
} }
func (b *cmdSecretBuilder) cmdFind() *cobra.Command { func (b *cmdSecretBuilder) cmdFind() *cobra.Command {
cmd := b.newCmd("list", b.cmdFindRunEFn, true) cmd := b.newCmd("list", b.cmdFindRunEFn, true)
cmd.Short = "List secrets" cmd.Short = "List secrets"
cmd.Aliases = []string{"find", "ls"} cmd.Aliases = []string{"find", "ls"}
b.org.register(cmd, false) b.org.register(cmd, false)
b.registerPrintFlags(cmd)
return cmd return cmd
} }
func (b *cmdSecretBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) error { func (b *cmdSecretBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) error {
scrSVC, orgSVC, _, err := b.svcFn() scrSVC, orgSVC, _, err := b.svcFn()
if err != nil { if err != nil {
return err return err
@ -157,24 +159,79 @@ func (b *cmdSecretBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) erro
return err return err
} }
secrets, err := scrSVC.GetSecretKeys(context.Background(), orgID) platformSecrets, err := scrSVC.GetSecretKeys(context.Background(), orgID)
if err != nil { if err != nil {
return fmt.Errorf("failed to retrieve secret keys: %s", err) return fmt.Errorf("failed to retrieve secret keys: %s", err)
} }
w := b.newTabWriter() secrets := make([]secret, 0, len(platformSecrets))
w.WriteHeaders("Key", "OrganizationID") for _, key := range platformSecrets {
for _, s := range secrets { secrets = append(secrets, secret{
w.Write(map[string]interface{}{ key: key,
"Key": s, orgID: orgID,
"OrganizationID": orgID,
}) })
} }
w.Flush()
return b.printSecrets(secretPrintOpt{
secrets: secrets,
})
}
func (b *cmdSecretBuilder) registerPrintFlags(cmd *cobra.Command) {
registerPrintOptions(cmd, &b.hideHeaders, &b.json)
}
func (b *cmdSecretBuilder) printSecrets(opt secretPrintOpt) error {
if b.json {
var v interface{} = opt.secrets
if opt.secrets == nil {
v = opt.secret
}
return b.writeJSON(v)
}
w := b.newTabWriter()
defer w.Flush()
w.HideHeaders(b.hideHeaders)
headers := []string{"Key", "Organization ID"}
if opt.deleted {
headers = append(headers, "Deleted")
}
w.WriteHeaders(headers...)
if opt.secrets == nil {
opt.secrets = append(opt.secrets, opt.secret)
}
for _, s := range opt.secrets {
m := map[string]interface{}{
"Key": s.key,
"Organization ID": s.orgID.String(),
}
if opt.deleted {
m["Deleted"] = true
}
w.Write(m)
}
return nil return nil
} }
type (
secretPrintOpt struct {
deleted bool
secret secret
secrets []secret
}
secret struct {
key string
orgID influxdb.ID
}
)
func newSecretSVCs() (influxdb.SecretService, influxdb.OrganizationService, func(*input.UI) string, error) { func newSecretSVCs() (influxdb.SecretService, influxdb.OrganizationService, func(*input.UI) string, error) {
httpClient, err := newHTTPClient() httpClient, err := newHTTPClient()
if err != nil { if err != nil {