mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add endpoint group inspect command to inspect an endpoint group
This commit is contained in:
parent
b4f6dd7476
commit
6939361670
@ -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.
|
||||
|
@ -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="" \
|
||||
|
77
cmd/endpointGroupInspect.go
Normal file
77
cmd/endpointGroupInspect.go
Normal 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{}))
|
||||
}
|
Loading…
Reference in New Issue
Block a user