github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/serviceauth/describe.go (about)

     1  package serviceauth
     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  	"github.com/fastly/cli/pkg/time"
    13  )
    14  
    15  // DescribeCommand calls the Fastly API to describe a service authorization.
    16  type DescribeCommand struct {
    17  	argparser.Base
    18  	argparser.JSONOutput
    19  
    20  	Input fastly.GetServiceAuthorizationInput
    21  }
    22  
    23  // NewDescribeCommand returns a usable command registered under the parent.
    24  func NewDescribeCommand(parent argparser.Registerer, g *global.Data) *DescribeCommand {
    25  	c := DescribeCommand{
    26  		Base: argparser.Base{
    27  			Globals: g,
    28  		},
    29  	}
    30  	c.CmdClause = parent.Command("describe", "Show service authorization").Alias("get")
    31  
    32  	// Required.
    33  	c.CmdClause.Flag("id", "ID of the service authorization to retrieve").Required().StringVar(&c.Input.ID)
    34  
    35  	// Optional.
    36  	c.RegisterFlagBool(c.JSONFlag()) // --json
    37  	return &c
    38  }
    39  
    40  // Exec invokes the application logic for the command.
    41  func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
    42  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    43  		return fsterr.ErrInvalidVerboseJSONCombo
    44  	}
    45  
    46  	o, err := c.Globals.APIClient.GetServiceAuthorization(&c.Input)
    47  	if err != nil {
    48  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    49  			"Service Authorization ID": c.Input.ID,
    50  		})
    51  		return err
    52  	}
    53  
    54  	if ok, err := c.WriteJSON(out, o); ok {
    55  		return err
    56  	}
    57  
    58  	return c.print(o, out)
    59  }
    60  
    61  func (c *DescribeCommand) print(s *fastly.ServiceAuthorization, out io.Writer) error {
    62  	fmt.Fprintf(out, "Auth ID: %s\n", s.ID)
    63  	fmt.Fprintf(out, "User ID: %s\n", s.User.ID)
    64  	fmt.Fprintf(out, "Service ID: %s\n", s.Service.ID)
    65  	fmt.Fprintf(out, "Permission: %s\n", s.Permission)
    66  
    67  	if s.CreatedAt != nil {
    68  		fmt.Fprintf(out, "Created (UTC): %s\n", s.CreatedAt.UTC().Format(time.Format))
    69  	}
    70  	if s.UpdatedAt != nil {
    71  		fmt.Fprintf(out, "Last edited (UTC): %s\n", s.UpdatedAt.UTC().Format(time.Format))
    72  	}
    73  	if s.DeletedAt != nil {
    74  		fmt.Fprintf(out, "Deleted (UTC): %s\n", s.DeletedAt.UTC().Format(time.Format))
    75  	}
    76  
    77  	return nil
    78  }