Add endpoint group inspect command to inspect an endpoint group

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-17 17:03:07 -04:00
parent b4f6dd7476
commit 6939361670
3 changed files with 80 additions and 0 deletions

View File

@ -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.

View File

@ -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="" \

View File

@ -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{}))
}