package cmd import ( "fmt" "os" "text/tabwriter" "xcecli/common/request" "xcecli/config" "github.com/spf13/cobra" ) // NewListCmd create the list command func NewListCmd(configManager *config.ConfigManager) *cobra.Command { return &cobra.Command{ Use: "list", GroupID: "api", Short: "List the API Servers", Long: "List all the configured API Servers and the default API Server", RunE: func(cmd *cobra.Command, args []string) error { return runList(configManager) }, } } func runList(cm *config.ConfigManager) error { cfg, err := cm.GetConfig() if err != nil { errMsg := request.ExtractErrorMessage(err) return fmt.Errorf("failed to get the config: [%s]", errMsg) } // use tabwriter to format the output w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) fmt.Fprintln(w, "Default\tVersion\tURL") for _, server := range cfg.APIServerInfo { isDefault := " " if server.Token == "" { continue } fmt.Fprintf(w, "%s\t%s\n", isDefault, server.URL) } return w.Flush() }