github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/aclentry/describe.go (about) 1 package aclentry 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 c := DescribeCommand{ 17 Base: argparser.Base{ 18 Globals: g, 19 }, 20 } 21 c.CmdClause = parent.Command("describe", "Retrieve a single ACL entry").Alias("get") 22 23 // Required. 24 c.CmdClause.Flag("acl-id", "Alphanumeric string identifying a ACL").Required().StringVar(&c.aclID) 25 c.CmdClause.Flag("id", "Alphanumeric string identifying an ACL Entry").Required().StringVar(&c.id) 26 27 // Optional. 28 c.RegisterFlagBool(c.JSONFlag()) // --json 29 c.RegisterFlag(argparser.StringFlagOpts{ 30 Name: argparser.FlagServiceIDName, 31 Description: argparser.FlagServiceIDDesc, 32 Dst: &g.Manifest.Flag.ServiceID, 33 Short: 's', 34 }) 35 c.RegisterFlag(argparser.StringFlagOpts{ 36 Action: c.serviceName.Set, 37 Name: argparser.FlagServiceName, 38 Description: argparser.FlagServiceDesc, 39 Dst: &c.serviceName.Value, 40 }) 41 42 return &c 43 } 44 45 // DescribeCommand calls the Fastly API to describe an appropriate resource. 46 type DescribeCommand struct { 47 argparser.Base 48 argparser.JSONOutput 49 50 aclID string 51 id string 52 serviceName argparser.OptionalServiceNameID 53 } 54 55 // Exec invokes the application logic for the command. 56 func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error { 57 if c.Globals.Verbose() && c.JSONOutput.Enabled { 58 return fsterr.ErrInvalidVerboseJSONCombo 59 } 60 61 serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) 62 if err != nil { 63 return err 64 } 65 if c.Globals.Verbose() { 66 argparser.DisplayServiceID(serviceID, flag, source, out) 67 } 68 69 input := c.constructInput(serviceID) 70 71 o, err := c.Globals.APIClient.GetACLEntry(input) 72 if err != nil { 73 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 74 "Service ID": serviceID, 75 }) 76 return err 77 } 78 79 if ok, err := c.WriteJSON(out, o); ok { 80 return err 81 } 82 83 return c.print(out, o) 84 } 85 86 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 87 func (c *DescribeCommand) constructInput(serviceID string) *fastly.GetACLEntryInput { 88 var input fastly.GetACLEntryInput 89 90 input.ACLID = c.aclID 91 input.EntryID = c.id 92 input.ServiceID = serviceID 93 94 return &input 95 } 96 97 // print displays the information returned from the API. 98 func (c *DescribeCommand) print(out io.Writer, a *fastly.ACLEntry) error { 99 if !c.Globals.Verbose() { 100 fmt.Fprintf(out, "\nService ID: %s\n", fastly.ToValue(a.ServiceID)) 101 } 102 fmt.Fprintf(out, "ACL ID: %s\n", fastly.ToValue(a.ACLID)) 103 fmt.Fprintf(out, "ID: %s\n", fastly.ToValue(a.EntryID)) 104 fmt.Fprintf(out, "IP: %s\n", fastly.ToValue(a.IP)) 105 fmt.Fprintf(out, "Subnet: %d\n", fastly.ToValue(a.Subnet)) 106 fmt.Fprintf(out, "Negated: %t\n", fastly.ToValue(a.Negated)) 107 fmt.Fprintf(out, "Comment: %s\n\n", fastly.ToValue(a.Comment)) 108 109 if a.CreatedAt != nil { 110 fmt.Fprintf(out, "Created at: %s\n", a.CreatedAt) 111 } 112 if a.UpdatedAt != nil { 113 fmt.Fprintf(out, "Updated at: %s\n", a.UpdatedAt) 114 } 115 if a.DeletedAt != nil { 116 fmt.Fprintf(out, "Deleted at: %s\n", a.DeletedAt) 117 } 118 return nil 119 }