From 1ab13335401688626ff550b3926903da61549f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Fri, 9 Aug 2019 13:29:16 -0400 Subject: [PATCH] Add command to list endpoint groups --- cmd/endpointGroup.go | 15 ++++++++++ cmd/endpointGroupList.go | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 cmd/endpointGroup.go create mode 100644 cmd/endpointGroupList.go diff --git a/cmd/endpointGroup.go b/cmd/endpointGroup.go new file mode 100644 index 0000000..72b8036 --- /dev/null +++ b/cmd/endpointGroup.go @@ -0,0 +1,15 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// endpointGroupCmd represents the endpoint group command +var endpointGroupCmd = &cobra.Command{ + Use: "group", + Short: "Manage endpoint groups", +} + +func init() { + endpointCmd.AddCommand(endpointGroupCmd) +} diff --git a/cmd/endpointGroupList.go b/cmd/endpointGroupList.go new file mode 100644 index 0000000..874821a --- /dev/null +++ b/cmd/endpointGroupList.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + "os" + "text/template" + + "github.com/greenled/portainer-stack-utils/common" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + + "github.com/spf13/cobra" +) + +// endpointGroupListCmd represents the endpoint group list command +var endpointGroupListCmd = &cobra.Command{ + Use: "list", + Short: "List endpoint groups", + Aliases: []string{"ls"}, + Run: func(cmd *cobra.Command, args []string) { + client, err := common.GetClient() + common.CheckError(err) + + logrus.Debug("Getting endpoint groups") + endpointGroups, err := client.GetEndpointGroups() + common.CheckError(err) + + if viper.GetString("endpoint.group.list.format") != "" { + // Print endpoint group fields formatted + template, templateParsingErr := template.New("endpointGroupTpl").Parse(viper.GetString("endpoint.group.list.format")) + common.CheckError(templateParsingErr) + for _, g := range endpointGroups { + templateExecutionErr := template.Execute(os.Stdout, g) + common.CheckError(templateExecutionErr) + fmt.Println() + } + } else { + // Print all endpoint group fields as a table + writer, err := common.NewTabWriter([]string{ + "ENDPOINT GROUP ID", + "NAME", + "DESCRIPTION", + }) + common.CheckError(err) + for _, g := range endpointGroups { + _, err := fmt.Fprintln(writer, fmt.Sprintf( + "%v\t%s\t%s", + g.Id, + g.Name, + g.Description, + )) + common.CheckError(err) + } + flushErr := writer.Flush() + common.CheckError(flushErr) + } + }, +} + +func init() { + endpointGroupCmd.AddCommand(endpointGroupListCmd) + + endpointGroupListCmd.Flags().String("format", "", "Format output using a Go template.") + viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format")) +}