github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/serviceauth/list.go (about) 1 package serviceauth 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/fastly/cli/pkg/argparser" 8 fsterr "github.com/fastly/cli/pkg/errors" 9 "github.com/fastly/cli/pkg/global" 10 "github.com/fastly/cli/pkg/text" 11 "github.com/fastly/cli/pkg/time" 12 "github.com/fastly/go-fastly/v9/fastly" 13 ) 14 15 // ListCommand calls the Fastly API to list service authorizations. 16 type ListCommand struct { 17 argparser.Base 18 argparser.JSONOutput 19 20 input fastly.ListServiceAuthorizationsInput 21 } 22 23 // NewListCommand returns a usable command registered under the parent. 24 func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand { 25 var c ListCommand 26 c.Globals = g 27 c.CmdClause = parent.Command("list", "List service authorizations") 28 29 // Optional. 30 c.RegisterFlagBool(c.JSONFlag()) // --json 31 c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.input.PageNumber) 32 c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.input.PageSize) 33 return &c 34 } 35 36 // Exec invokes the application logic for the command. 37 func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { 38 if c.Globals.Verbose() && c.JSONOutput.Enabled { 39 return fsterr.ErrInvalidVerboseJSONCombo 40 } 41 42 o, err := c.Globals.APIClient.ListServiceAuthorizations(&c.input) 43 if err != nil { 44 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 45 "Page Number": c.input.PageNumber, 46 "Page Size": c.input.PageSize, 47 }) 48 return err 49 } 50 51 if ok, err := c.WriteJSON(out, o); ok { 52 return err 53 } 54 55 if !c.Globals.Verbose() { 56 if len(o.Items) > 0 { 57 tw := text.NewTable(out) 58 tw.AddHeader("AUTH ID", "USER ID", "SERVICE ID", "PERMISSION") 59 60 for _, s := range o.Items { 61 tw.AddLine(s.ID, s.User.ID, s.Service.ID, s.Permission) 62 } 63 tw.Print() 64 65 return nil 66 } 67 } 68 69 for _, s := range o.Items { 70 fmt.Fprintf(out, "Auth ID: %s\n", s.ID) 71 fmt.Fprintf(out, "User ID: %s\n", s.User.ID) 72 fmt.Fprintf(out, "Service ID: %s\n", s.Service.ID) 73 fmt.Fprintf(out, "Permission: %s\n", s.Permission) 74 75 if s.CreatedAt != nil { 76 fmt.Fprintf(out, "Created (UTC): %s\n", s.CreatedAt.UTC().Format(time.Format)) 77 } 78 if s.UpdatedAt != nil { 79 fmt.Fprintf(out, "Last edited (UTC): %s\n", s.UpdatedAt.UTC().Format(time.Format)) 80 } 81 if s.DeletedAt != nil { 82 fmt.Fprintf(out, "Deleted (UTC): %s\n", s.DeletedAt.UTC().Format(time.Format)) 83 } 84 } 85 86 return nil 87 }