mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Merge branch 'enhaced-format-help'
This commit is contained in:
commit
933f73896b
@ -92,16 +92,7 @@ func init() {
|
||||
configListCmd.Flags().String("format", "", "Format output using a Go template.")
|
||||
viper.BindPFlag("config.list.format", configListCmd.Flags().Lookup("format"))
|
||||
|
||||
configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + `
|
||||
Format:
|
||||
The --format flag accepts a Go template, which is executed in an object with this structure:
|
||||
|
||||
{
|
||||
Key string
|
||||
EnvironmentVariable string
|
||||
CurrentValue string
|
||||
}
|
||||
`)
|
||||
configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + common.GetFormatHelp(config{}))
|
||||
}
|
||||
|
||||
type config struct {
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
@ -62,4 +64,6 @@ func init() {
|
||||
|
||||
endpointGroupListCmd.Flags().String("format", "", "Format output using a Go template.")
|
||||
viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format"))
|
||||
|
||||
endpointGroupListCmd.SetUsageTemplate(endpointGroupListCmd.UsageTemplate() + common.GetFormatHelp(portainer.EndpointGroup{}))
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
@ -90,4 +92,6 @@ func init() {
|
||||
|
||||
endpointListCmd.Flags().String("format", "", "Format output using a Go template.")
|
||||
viper.BindPFlag("endpoint.list.format", endpointListCmd.Flags().Lookup("format"))
|
||||
|
||||
endpointListCmd.SetUsageTemplate(endpointListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Endpoint{}))
|
||||
}
|
||||
|
@ -98,4 +98,6 @@ func init() {
|
||||
stackListCmd.Flags().String("format", "", "Format output using a Go template.")
|
||||
viper.BindPFlag("stack.list.endpoint", stackListCmd.Flags().Lookup("endpoint"))
|
||||
viper.BindPFlag("stack.list.format", stackListCmd.Flags().Lookup("format"))
|
||||
|
||||
stackListCmd.SetUsageTemplate(stackListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Stack{}))
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@ -58,4 +60,6 @@ func init() {
|
||||
|
||||
statusCmd.Flags().String("format", "", "Format output using a Go template.")
|
||||
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
|
||||
|
||||
statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{}))
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package common
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -94,6 +95,34 @@ func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{}
|
||||
}
|
||||
}
|
||||
|
||||
func GetFormatHelp(v interface{}) (r string) {
|
||||
typeOfV := reflect.TypeOf(v)
|
||||
r = fmt.Sprintf(`
|
||||
Format:
|
||||
The --format flag accepts a Go template, which is passed a %s.%s object:
|
||||
|
||||
%s
|
||||
`, typeOfV.PkgPath(), typeOfV.Name(), fmt.Sprintf("%s%s", " ", repr(typeOfV, " ", " ")))
|
||||
return
|
||||
}
|
||||
|
||||
func repr(t reflect.Type, margin, beforeMargin string) (r string) {
|
||||
switch t.Kind() {
|
||||
case reflect.Struct:
|
||||
r = fmt.Sprintln("{")
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
tField := t.Field(i)
|
||||
r += fmt.Sprintln(fmt.Sprintf("%s%s%s %s", beforeMargin, margin, tField.Name, repr(tField.Type, margin, beforeMargin+margin)))
|
||||
}
|
||||
r += fmt.Sprintf("%s}", beforeMargin)
|
||||
case reflect.Array, reflect.Slice:
|
||||
r = fmt.Sprintf("[]%s", repr(t.Elem(), margin, beforeMargin))
|
||||
default:
|
||||
r = fmt.Sprintf("%s", t.Name())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Custom customerrors
|
||||
type StackNotFoundError struct {
|
||||
StackName string
|
||||
|
Loading…
Reference in New Issue
Block a user