...

Source file src/edge-infra.dev/pkg/edge/edgecli/commands/activationcode/list/list.go

Documentation: edge-infra.dev/pkg/edge/edgecli/commands/activationcode/list

     1  package list
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"edge-infra.dev/pkg/edge/edgecli"
     9  	"edge-infra.dev/pkg/edge/edgecli/constructors"
    10  	"edge-infra.dev/pkg/edge/edgecli/flagutil"
    11  	"edge-infra.dev/pkg/lib/cli/command"
    12  	"edge-infra.dev/pkg/lib/cli/rags"
    13  )
    14  
    15  func NewCmd(cfg *edgecli.Config) *command.Command {
    16  	var cmd *command.Command
    17  	flagsets := flagutil.GetConnectionFlags(cfg)
    18  	var storeName, bannerName string
    19  
    20  	cmd = &command.Command{
    21  		ShortUsage: "edge activationcode list",
    22  		ShortHelp:  "List a clusters' terminal activation codes",
    23  		Flags: append(
    24  			flagsets,
    25  			&rags.Rag{
    26  				Name:     flagutil.StoreFlag,
    27  				Usage:    "Name of the Store",
    28  				Value:    &rags.String{Var: &storeName},
    29  				Required: true,
    30  			},
    31  			&rags.Rag{
    32  				Name:     flagutil.BannerFlag,
    33  				Usage:    "Banner name",
    34  				Value:    &rags.String{Var: &bannerName},
    35  				Required: true,
    36  			},
    37  		),
    38  		Exec: func(_ context.Context, _ []string) error {
    39  			if err := flagutil.ValidateRequiredFlags(cmd.Rags); err != nil {
    40  				return err
    41  			}
    42  
    43  			registrar, err := constructors.BuildRegistrar(cmd.Rags)
    44  			if err != nil {
    45  				return err
    46  			}
    47  
    48  			fmt.Println("flags validated")
    49  
    50  			reqCtx, cancelReq := context.WithTimeout(context.Background(), time.Duration(30)*time.Second)
    51  			defer cancelReq()
    52  
    53  			cluster, err := registrar.GetCluster(reqCtx, storeName, bannerName)
    54  			if err != nil {
    55  				fmt.Println("an error occurred whilst modifying the cluster network service")
    56  				return err
    57  			}
    58  
    59  			terminals, err := registrar.GetTerminals(reqCtx, cluster.ClusterEdgeID)
    60  			if err != nil {
    61  				return err
    62  			}
    63  
    64  			fmt.Println("-----------------------------------")
    65  			fmt.Println("List of Cluster's Activation Codes")
    66  			fmt.Println("-----------------------------------")
    67  			for _, terminal := range terminals {
    68  				if terminal.ActivationCode == nil || *terminal.ActivationCode == "" {
    69  					fmt.Printf("%s: <CONSUMED>\n", terminal.Hostname)
    70  					continue
    71  				}
    72  				fmt.Printf("%s: %s\n", terminal.Hostname, *terminal.ActivationCode)
    73  			}
    74  			return nil
    75  		},
    76  	}
    77  	return cmd
    78  }
    79  

View as plain text