github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/digitalocean/list.go (about)

     1  package digitalocean
     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/text"
    13  )
    14  
    15  // ListCommand calls the Fastly API to list DigitalOcean Spaces logging endpoints.
    16  type ListCommand struct {
    17  	argparser.Base
    18  	argparser.JSONOutput
    19  
    20  	Input          fastly.ListDigitalOceansInput
    21  	serviceName    argparser.OptionalServiceNameID
    22  	serviceVersion argparser.OptionalServiceVersion
    23  }
    24  
    25  // NewListCommand returns a usable command registered under the parent.
    26  func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
    27  	c := ListCommand{
    28  		Base: argparser.Base{
    29  			Globals: g,
    30  		},
    31  	}
    32  	c.CmdClause = parent.Command("list", "List DigitalOcean Spaces logging endpoints on a Fastly service version")
    33  
    34  	// Required.
    35  	c.RegisterFlag(argparser.StringFlagOpts{
    36  		Name:        argparser.FlagVersionName,
    37  		Description: argparser.FlagVersionDesc,
    38  		Dst:         &c.serviceVersion.Value,
    39  		Required:    true,
    40  	})
    41  
    42  	// Optional.
    43  	c.RegisterFlagBool(c.JSONFlag()) // --json
    44  	c.RegisterFlag(argparser.StringFlagOpts{
    45  		Name:        argparser.FlagServiceIDName,
    46  		Description: argparser.FlagServiceIDDesc,
    47  		Dst:         &g.Manifest.Flag.ServiceID,
    48  		Short:       's',
    49  	})
    50  	c.RegisterFlag(argparser.StringFlagOpts{
    51  		Action:      c.serviceName.Set,
    52  		Name:        argparser.FlagServiceName,
    53  		Description: argparser.FlagServiceDesc,
    54  		Dst:         &c.serviceName.Value,
    55  	})
    56  	return &c
    57  }
    58  
    59  // Exec invokes the application logic for the command.
    60  func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
    61  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    62  		return fsterr.ErrInvalidVerboseJSONCombo
    63  	}
    64  
    65  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
    66  		AllowActiveLocked:  true,
    67  		APIClient:          c.Globals.APIClient,
    68  		Manifest:           *c.Globals.Manifest,
    69  		Out:                out,
    70  		ServiceNameFlag:    c.serviceName,
    71  		ServiceVersionFlag: c.serviceVersion,
    72  		VerboseMode:        c.Globals.Flags.Verbose,
    73  	})
    74  	if err != nil {
    75  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    76  			"Service ID":      serviceID,
    77  			"Service Version": fsterr.ServiceVersion(serviceVersion),
    78  		})
    79  		return err
    80  	}
    81  
    82  	c.Input.ServiceID = serviceID
    83  	c.Input.ServiceVersion = fastly.ToValue(serviceVersion.Number)
    84  
    85  	o, err := c.Globals.APIClient.ListDigitalOceans(&c.Input)
    86  	if err != nil {
    87  		c.Globals.ErrLog.Add(err)
    88  		return err
    89  	}
    90  
    91  	if ok, err := c.WriteJSON(out, o); ok {
    92  		return err
    93  	}
    94  
    95  	if !c.Globals.Verbose() {
    96  		tw := text.NewTable(out)
    97  		tw.AddHeader("SERVICE", "VERSION", "NAME")
    98  		for _, digitalocean := range o {
    99  			tw.AddLine(
   100  				fastly.ToValue(digitalocean.ServiceID),
   101  				fastly.ToValue(digitalocean.ServiceVersion),
   102  				fastly.ToValue(digitalocean.Name),
   103  			)
   104  		}
   105  		tw.Print()
   106  		return nil
   107  	}
   108  
   109  	fmt.Fprintf(out, "Version: %d\n", c.Input.ServiceVersion)
   110  	for i, digitalocean := range o {
   111  		fmt.Fprintf(out, "\tDigitalOcean %d/%d\n", i+1, len(o))
   112  		fmt.Fprintf(out, "\t\tService ID: %s\n", fastly.ToValue(digitalocean.ServiceID))
   113  		fmt.Fprintf(out, "\t\tVersion: %d\n", fastly.ToValue(digitalocean.ServiceVersion))
   114  		fmt.Fprintf(out, "\t\tName: %s\n", fastly.ToValue(digitalocean.Name))
   115  		fmt.Fprintf(out, "\t\tBucket: %s\n", fastly.ToValue(digitalocean.BucketName))
   116  		fmt.Fprintf(out, "\t\tDomain: %s\n", fastly.ToValue(digitalocean.Domain))
   117  		fmt.Fprintf(out, "\t\tAccess key: %s\n", fastly.ToValue(digitalocean.AccessKey))
   118  		fmt.Fprintf(out, "\t\tSecret key: %s\n", fastly.ToValue(digitalocean.SecretKey))
   119  		fmt.Fprintf(out, "\t\tPath: %s\n", fastly.ToValue(digitalocean.Path))
   120  		fmt.Fprintf(out, "\t\tPeriod: %d\n", fastly.ToValue(digitalocean.Period))
   121  		fmt.Fprintf(out, "\t\tGZip level: %d\n", fastly.ToValue(digitalocean.GzipLevel))
   122  		fmt.Fprintf(out, "\t\tFormat: %s\n", fastly.ToValue(digitalocean.Format))
   123  		fmt.Fprintf(out, "\t\tFormat version: %d\n", fastly.ToValue(digitalocean.FormatVersion))
   124  		fmt.Fprintf(out, "\t\tResponse condition: %s\n", fastly.ToValue(digitalocean.ResponseCondition))
   125  		fmt.Fprintf(out, "\t\tMessage type: %s\n", fastly.ToValue(digitalocean.MessageType))
   126  		fmt.Fprintf(out, "\t\tTimestamp format: %s\n", fastly.ToValue(digitalocean.TimestampFormat))
   127  		fmt.Fprintf(out, "\t\tPlacement: %s\n", fastly.ToValue(digitalocean.Placement))
   128  		fmt.Fprintf(out, "\t\tPublic key: %s\n", fastly.ToValue(digitalocean.PublicKey))
   129  	}
   130  	fmt.Fprintln(out)
   131  
   132  	return nil
   133  }