github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/newrelic/describe.go (about) 1 package newrelic 2 3 import ( 4 "io" 5 6 "github.com/fastly/go-fastly/v9/fastly" 7 8 "github.com/fastly/cli/pkg/argparser" 9 fsterr "github.com/fastly/cli/pkg/errors" 10 "github.com/fastly/cli/pkg/global" 11 "github.com/fastly/cli/pkg/text" 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", "Get the details of a New Relic Logs logging object for a particular service and version").Alias("get") 22 23 // Required. 24 c.CmdClause.Flag("name", "The name for the real-time logging configuration").Required().StringVar(&c.name) 25 c.RegisterFlag(argparser.StringFlagOpts{ 26 Name: argparser.FlagVersionName, 27 Description: argparser.FlagVersionDesc, 28 Dst: &c.serviceVersion.Value, 29 Required: true, 30 }) 31 32 // Optional. 33 c.RegisterFlagBool(c.JSONFlag()) // --json 34 c.RegisterFlag(argparser.StringFlagOpts{ 35 Name: argparser.FlagServiceIDName, 36 Description: argparser.FlagServiceIDDesc, 37 Dst: &g.Manifest.Flag.ServiceID, 38 Short: 's', 39 }) 40 c.RegisterFlag(argparser.StringFlagOpts{ 41 Action: c.serviceName.Set, 42 Name: argparser.FlagServiceName, 43 Description: argparser.FlagServiceDesc, 44 Dst: &c.serviceName.Value, 45 }) 46 47 return &c 48 } 49 50 // DescribeCommand calls the Fastly API to describe an appropriate resource. 51 type DescribeCommand struct { 52 argparser.Base 53 argparser.JSONOutput 54 55 name string 56 serviceName argparser.OptionalServiceNameID 57 serviceVersion argparser.OptionalServiceVersion 58 } 59 60 // Exec invokes the application logic for the command. 61 func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error { 62 if c.Globals.Verbose() && c.JSONOutput.Enabled { 63 return fsterr.ErrInvalidVerboseJSONCombo 64 } 65 66 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 67 AllowActiveLocked: true, 68 APIClient: c.Globals.APIClient, 69 Manifest: *c.Globals.Manifest, 70 Out: out, 71 ServiceNameFlag: c.serviceName, 72 ServiceVersionFlag: c.serviceVersion, 73 VerboseMode: c.Globals.Flags.Verbose, 74 }) 75 if err != nil { 76 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 77 "Service ID": serviceID, 78 "Service Version": fsterr.ServiceVersion(serviceVersion), 79 }) 80 return err 81 } 82 83 input := c.constructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 84 85 o, err := c.Globals.APIClient.GetNewRelic(input) 86 if err != nil { 87 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 88 "Service ID": serviceID, 89 "Service Version": fastly.ToValue(serviceVersion.Number), 90 }) 91 return err 92 } 93 94 if ok, err := c.WriteJSON(out, o); ok { 95 return err 96 } 97 98 return c.print(out, o) 99 } 100 101 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 102 func (c *DescribeCommand) constructInput(serviceID string, serviceVersion int) *fastly.GetNewRelicInput { 103 var input fastly.GetNewRelicInput 104 105 input.Name = c.name 106 input.ServiceID = serviceID 107 input.ServiceVersion = serviceVersion 108 109 return &input 110 } 111 112 // print displays the information returned from the API. 113 func (c *DescribeCommand) print(out io.Writer, nr *fastly.NewRelic) error { 114 lines := text.Lines{ 115 "Format Version": fastly.ToValue(nr.FormatVersion), 116 "Format": fastly.ToValue(nr.Format), 117 "Name": fastly.ToValue(nr.Name), 118 "Placement": fastly.ToValue(nr.Placement), 119 "Region": fastly.ToValue(nr.Region), 120 "Response Condition": fastly.ToValue(nr.ResponseCondition), 121 "Service Version": fastly.ToValue(nr.ServiceVersion), 122 "Token": fastly.ToValue(nr.Token), 123 } 124 if nr.CreatedAt != nil { 125 lines["Created at"] = nr.CreatedAt 126 } 127 if nr.UpdatedAt != nil { 128 lines["Updated at"] = nr.UpdatedAt 129 } 130 if nr.DeletedAt != nil { 131 lines["Deleted at"] = nr.DeletedAt 132 } 133 134 if !c.Globals.Verbose() { 135 lines["Service ID"] = fastly.ToValue(nr.ServiceID) 136 } 137 text.PrintLines(out, lines) 138 139 return nil 140 }