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

     1  package stats
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/fastly/cli/pkg/argparser"
     8  	"github.com/fastly/cli/pkg/global"
     9  	"github.com/fastly/cli/pkg/text"
    10  )
    11  
    12  // RegionsCommand exposes the Stats Regions API.
    13  type RegionsCommand struct {
    14  	argparser.Base
    15  }
    16  
    17  // NewRegionsCommand returns a new command registered under parent.
    18  func NewRegionsCommand(parent argparser.Registerer, g *global.Data) *RegionsCommand {
    19  	var c RegionsCommand
    20  	c.Globals = g
    21  	c.CmdClause = parent.Command("regions", "List stats regions")
    22  	return &c
    23  }
    24  
    25  // Exec implements the command interface.
    26  func (c *RegionsCommand) Exec(_ io.Reader, out io.Writer) error {
    27  	resp, err := c.Globals.APIClient.GetRegions()
    28  	if err != nil {
    29  		c.Globals.ErrLog.Add(err)
    30  		return fmt.Errorf("fetching regions: %w", err)
    31  	}
    32  
    33  	for _, region := range resp.Data {
    34  		text.Output(out, "%s", region)
    35  	}
    36  
    37  	return nil
    38  }