github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/privatekey/describe.go (about) 1 package privatekey 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/fastly/go-fastly/v9/fastly" 8 9 "github.com/fastly/cli/pkg/argparser" 10 fsterr "github.com/fastly/cli/pkg/errors" 11 "github.com/fastly/cli/pkg/global" 12 ) 13 14 // NewDescribeCommand returns a usable command registered under the parent. 15 func NewDescribeCommand(parent argparser.Registerer, g *global.Data) *DescribeCommand { 16 var c DescribeCommand 17 c.CmdClause = parent.Command("describe", "Show a TLS private key").Alias("get") 18 c.Globals = g 19 20 // Required. 21 c.CmdClause.Flag("id", "Alphanumeric string identifying a private Key").Required().StringVar(&c.id) 22 23 // Optional. 24 c.RegisterFlagBool(c.JSONFlag()) // --json 25 26 return &c 27 } 28 29 // DescribeCommand calls the Fastly API to describe an appropriate resource. 30 type DescribeCommand struct { 31 argparser.Base 32 argparser.JSONOutput 33 34 id string 35 } 36 37 // Exec invokes the application logic for the command. 38 func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error { 39 if c.Globals.Verbose() && c.JSONOutput.Enabled { 40 return fsterr.ErrInvalidVerboseJSONCombo 41 } 42 43 input := c.constructInput() 44 45 o, err := c.Globals.APIClient.GetPrivateKey(input) 46 if err != nil { 47 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 48 "TLS Certificate ID": c.id, 49 }) 50 return err 51 } 52 53 if ok, err := c.WriteJSON(out, o); ok { 54 return err 55 } 56 57 return c.print(out, o) 58 } 59 60 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 61 func (c *DescribeCommand) constructInput() *fastly.GetPrivateKeyInput { 62 var input fastly.GetPrivateKeyInput 63 64 input.ID = c.id 65 66 return &input 67 } 68 69 // print displays the information returned from the API. 70 func (c *DescribeCommand) print(out io.Writer, r *fastly.PrivateKey) error { 71 fmt.Fprintf(out, "\nID: %s\n", r.ID) 72 fmt.Fprintf(out, "Name: %s\n", r.Name) 73 fmt.Fprintf(out, "Key Length: %d\n", r.KeyLength) 74 fmt.Fprintf(out, "Key Type: %s\n", r.KeyType) 75 fmt.Fprintf(out, "Public Key SHA1: %s\n", r.PublicKeySHA1) 76 77 if r.CreatedAt != nil { 78 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 79 } 80 81 fmt.Fprintf(out, "Replace: %t\n", r.Replace) 82 83 return nil 84 }