github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/secret-ids.go (about) 1 // Copyright 2022 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "github.com/juju/cmd/v3" 8 "github.com/juju/gnuflag" 9 10 jujucmd "github.com/juju/juju/cmd" 11 ) 12 13 type secretIdsCommand struct { 14 cmd.CommandBase 15 ctx Context 16 17 out cmd.Output 18 } 19 20 // NewSecretIdsCommand returns a command to list the IDs and labels of secrets. 21 // created by this app. 22 func NewSecretIdsCommand(ctx Context) (cmd.Command, error) { 23 return &secretIdsCommand{ctx: ctx}, nil 24 } 25 26 func (c *secretIdsCommand) SetFlags(f *gnuflag.FlagSet) { 27 c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters()) 28 } 29 30 // Info implements cmd.Command. 31 func (c *secretIdsCommand) Info() *cmd.Info { 32 doc := ` 33 Returns the secret ids for secrets owned by the application. 34 35 Examples: 36 secret-ids 37 ` 38 return jujucmd.Info(&cmd.Info{ 39 Name: "secret-ids", 40 Purpose: "print secret ids", 41 Doc: doc, 42 }) 43 } 44 45 // Init implements cmd.Command. 46 func (c *secretIdsCommand) Init(args []string) error { 47 return cmd.CheckEmpty(args) 48 } 49 50 // Run implements cmd.Command. 51 func (c *secretIdsCommand) Run(ctx *cmd.Context) error { 52 result, err := c.ctx.SecretMetadata() 53 if err != nil { 54 return err 55 } 56 var out []string 57 for id := range result { 58 out = append(out, id) 59 } 60 return c.out.Write(ctx, out) 61 }