github.com/section/sectionctl@v1.12.3/commands/domains.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/olekukonko/tablewriter"
     9  	"github.com/section/sectionctl/api"
    10  )
    11  
    12  // DomainsCmd manages domains on Section
    13  type DomainsCmd struct {
    14  	List DomainsListCmd `cmd help:"List domains on Section." default:"1"`
    15  }
    16  
    17  // DomainsListCmd handles listing domains on Section
    18  type DomainsListCmd struct {
    19  	AccountID int `short:"a" help:"ID of account to list domains under"`
    20  }
    21  
    22  // Run executes the command
    23  func (c *DomainsListCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
    24  	var aids []int
    25  	if c.AccountID == 0 {
    26  		s := NewSpinner("Looking up accounts",logWriters)
    27  		s.Start()
    28  
    29  		as, err := api.Accounts()
    30  		if err != nil {
    31  			return fmt.Errorf("unable to look up accounts: %w", err)
    32  		}
    33  		for _, a := range as {
    34  			aids = append(aids, a.ID)
    35  		}
    36  
    37  		s.Stop()
    38  	} else {
    39  		aids = append(aids, c.AccountID)
    40  	}
    41  
    42  	s := NewSpinner("Looking up domains",logWriters)
    43  	s.Start()
    44  	domains := make(map[int][]api.DomainsResponse)
    45  	for _, id := range aids {
    46  		ds, err := api.Domains(id)
    47  		if err != nil {
    48  			return fmt.Errorf("unable to look up domains: %w", err)
    49  		}
    50  		domains[id] = ds
    51  	}
    52  	s.Stop()
    53  
    54  	table := NewTable(cli, os.Stdout)
    55  	table.SetHeader([]string{"Account ID", "Domain", "Engaged"})
    56  	table.SetHeaderColor(tablewriter.Colors{tablewriter.Normal,tablewriter.FgWhiteColor},tablewriter.Colors{tablewriter.Normal, tablewriter.FgWhiteColor},tablewriter.Colors{tablewriter.Normal, tablewriter.FgWhiteColor})
    57  	table.SetColumnColor(tablewriter.Colors{tablewriter.Normal,tablewriter.FgWhiteColor},tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiCyanColor},tablewriter.Colors{tablewriter.Normal,tablewriter.FgWhiteColor})
    58  	for id, ds := range domains {
    59  		for _, d := range ds {
    60  			r := []string{strconv.Itoa(id), d.DomainName, fmt.Sprintf("%t", d.Engaged)}
    61  			table.Append(r)
    62  		}
    63  	}
    64  
    65  	table.Render()
    66  	return err
    67  }