From 6939361670cc39e31f916c49c25acb46a62b756e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Sat, 17 Aug 2019 17:03:07 -0400 Subject: [PATCH] Add endpoint group inspect command to inspect an endpoint group --- CHANGELOG.md | 2 + Dockerfile | 1 + cmd/endpointGroupInspect.go | 77 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 cmd/endpointGroupInspect.go diff --git a/CHANGELOG.md b/CHANGELOG.md index fb41757..dced17b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `--format` flag to select output format from "table", "json" or a custom Go template. Defaults to "table". - `endpoint list|ls` command to print endpoints. - `--format` flag to select output format from "table", "json" or a custom Go template. Defaults to "table". +- `endpoint group inspect` command to print endpoint group info. + - `--format` flag to select output format from "table", "json" or a custom Go template. Defaults to "table". - `endpoint group list|ls` command to print endpoint groups. - `--format` flag to select output format from "table", "json" or a custom Go template. Defaults to "table". - `endpoint inspect` command to print endpoint info. diff --git a/Dockerfile b/Dockerfile index 8ae0291..7ed384f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ FROM alpine:3.10 ENV PSU_AUTH_TOKEN="" \ PSU_CONFIG="" \ PSU_CONFIG_LIST_FORMAT="" \ + PSU_ENDPOINT_GROUP_INSPECT_FORMAT="" \ PSU_ENDPOINT_GROUP_LIST_FORMAT="" \ PSU_ENDPOINT_INSPECT_FORMAT="" \ PSU_ENDPOINT_LIST_FORMAT="" \ diff --git a/cmd/endpointGroupInspect.go b/cmd/endpointGroupInspect.go new file mode 100644 index 0000000..d0de8ca --- /dev/null +++ b/cmd/endpointGroupInspect.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + "text/template" + + "github.com/greenled/portainer-stack-utils/common" + portainer "github.com/portainer/portainer/api" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// endpointGroupInspectCmd represents the endpoint group inspect command +var endpointGroupInspectCmd = &cobra.Command{ + Use: "inspect", + Short: "Inspect an endpoint group", + Example: "psu endpoint group inspect production", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + endpointGroupName := args[0] + endpointGroup, err := common.GetEndpointGroupByName(endpointGroupName) + if err == nil { + // The endpoint group exists + switch viper.GetString("endpoint.group.inspect.format") { + case "table": + // Print endpoint group in a table format + writer, err := common.NewTabWriter([]string{ + "ID", + "NAME", + "DESCRIPTION", + }) + common.CheckError(err) + _, err = fmt.Fprintln(writer, fmt.Sprintf( + "%v\t%s\t%s", + endpointGroup.ID, + endpointGroup.Name, + endpointGroup.Description, + )) + common.CheckError(err) + err = writer.Flush() + common.CheckError(err) + case "json": + // Print endpoint group in a json format + endpointJsonBytes, err := json.Marshal(endpointGroup) + common.CheckError(err) + fmt.Println(string(endpointJsonBytes)) + default: + // Print endpoint group in a custom format + template, err := template.New("endpointGroupTpl").Parse(viper.GetString("endpoint.group.inspect.format")) + common.CheckError(err) + err = template.Execute(os.Stdout, endpointGroup) + common.CheckError(err) + fmt.Println() + } + } else if err == common.ErrEndpointGroupNotFound { + // The endpoint group does not exist + logrus.WithFields(logrus.Fields{ + "endpointGroup": endpointGroupName, + }).Fatal("Endpoint group not found") + } else { + // Something else happened + common.CheckError(err) + } + }, +} + +func init() { + endpointGroupCmd.AddCommand(endpointGroupInspectCmd) + + endpointGroupInspectCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`) + viper.BindPFlag("endpoint.group.inspect.format", endpointGroupInspectCmd.Flags().Lookup("format")) + + endpointGroupInspectCmd.SetUsageTemplate(endpointGroupInspectCmd.UsageTemplate() + common.GetFormatHelp(portainer.EndpointGroup{})) +}