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

     1  package ip
     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  // RootCommand is the parent command for all subcommands in this package.
    13  // It should be installed under the primary root command.
    14  type RootCommand struct {
    15  	argparser.Base
    16  }
    17  
    18  // NewRootCommand returns a new command registered in the parent.
    19  func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand {
    20  	var c RootCommand
    21  	c.Globals = g
    22  	c.CmdClause = parent.Command("ip-list", "List Fastly's public IPs")
    23  	return &c
    24  }
    25  
    26  // Exec implements the command interface.
    27  func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error {
    28  	ipv4, ipv6, err := c.Globals.APIClient.AllIPs()
    29  	if err != nil {
    30  		c.Globals.ErrLog.Add(err)
    31  		return err
    32  	}
    33  
    34  	// TODO: Implement --json support.
    35  
    36  	text.Break(out)
    37  	fmt.Fprintf(out, "%s\n", text.Bold("IPv4"))
    38  	for _, ip := range ipv4 {
    39  		fmt.Fprintf(out, "\t%s\n", ip)
    40  	}
    41  	fmt.Fprintf(out, "\n%s\n", text.Bold("IPv6"))
    42  	for _, ip := range ipv6 {
    43  		fmt.Fprintf(out, "\t%s\n", ip)
    44  	}
    45  	return nil
    46  }