github.com/kolanos/fargate@v0.2.3/cmd/lb_list.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"text/tabwriter"
     8  
     9  	"github.com/jpignata/fargate/console"
    10  	ELBV2 "github.com/jpignata/fargate/elbv2"
    11  	"github.com/jpignata/fargate/util"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var lbListCmd = &cobra.Command{
    16  	Use:   "list",
    17  	Short: "List load balancers",
    18  	Run: func(cmd *cobra.Command, args []string) {
    19  		listLoadBalancers()
    20  	},
    21  }
    22  
    23  func init() {
    24  	lbCmd.AddCommand(lbListCmd)
    25  }
    26  
    27  func listLoadBalancers() {
    28  	elbv2 := ELBV2.New(sess)
    29  	loadBalancers := elbv2.DescribeLoadBalancers(ELBV2.DescribeLoadBalancersInput{})
    30  
    31  	if len(loadBalancers) == 0 {
    32  		console.InfoExit("No load balancers found")
    33  	}
    34  
    35  	w := new(tabwriter.Writer)
    36  	w.Init(os.Stdout, 0, 8, 1, '\t', 0)
    37  	fmt.Fprintln(w, "NAME\tTYPE\tSTATUS\tDNS NAME\tPORTS")
    38  
    39  	for _, loadBalancer := range loadBalancers {
    40  		var listeners []string
    41  
    42  		for _, listener := range elbv2.GetListeners(loadBalancer.Arn) {
    43  			listeners = append(listeners, listener.String())
    44  		}
    45  
    46  		fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
    47  			loadBalancer.Name,
    48  			util.Humanize(loadBalancer.Type),
    49  			util.Humanize(loadBalancer.State),
    50  			loadBalancer.DNSName,
    51  			strings.Join(listeners, ", "),
    52  		)
    53  	}
    54  
    55  	w.Flush()
    56  }