cli

package
v1.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutputFormatYaml        = "yaml"
	OutputFormatJson        = "json"
	OutputFormatJsonCompact = "json-compact"
	OutputFormatTable       = "table"
)

Variables

View Source
var Data string
View Source
var Datafile string
View Source
var Debug bool
View Source
var HideTags string

HideTags is a comma separated list of tag names that are matched against the protocmd.hidetag field option. Fields that match will be zeroed-out on output objects, effectively hiding them when outputing json or yaml. How this is set is up to the user, but typically a global flag can be used.

View Source
var Interactive bool
View Source
var NoFlags func(flagSet *pflag.FlagSet) = nil
View Source
var OutputFormat = OutputFormatYaml
View Source
var OutputStream bool
View Source
var Parsable bool
View Source
var SilenceUsage bool
View Source
var Tty bool

Functions

func AddDebugFlag

func AddDebugFlag(flagSet *pflag.FlagSet)

func AddHideTagsFormatFlag

func AddHideTagsFormatFlag(flagSet *pflag.FlagSet)

func AddInputFlags

func AddInputFlags(flagSet *pflag.FlagSet)

func AddOutputFlags

func AddOutputFlags(flagSet *pflag.FlagSet)

func AddTtyFlags

func AddTtyFlags(flagSet *pflag.FlagSet)

func ConvertDecodeErr

func ConvertDecodeErr(err error, reals map[string]string) error

func DecodeHook

func DecodeHook(from, to reflect.Type, data interface{}) (interface{}, error)

func FindField

func FindField(t reflect.Type, name string, ns FieldNamespace) (reflect.StructField, bool)

func FindHierField

func FindHierField(t reflect.Type, hierName string, ns FieldNamespace) (reflect.StructField, bool)

func GenGroup

func GenGroup(use, short string, cmds []*Command) *cobra.Command

func GetFieldTaggedName

func GetFieldTaggedName(sf reflect.StructField, ns FieldNamespace) string

Get the field name based on the namespace

func GetGenericObj

func GetGenericObj(dataMap interface{}) (map[string]interface{}, error)

func GetGenericObjFromList

func GetGenericObjFromList(dataMap interface{}, idx int) (map[string]interface{}, error)

func GetParseHelp

func GetParseHelp(t reflect.Type) (string, string, bool)

GetParseHelp gets end-user specific messages for error messages without any golang-specific language. It returns a non-golang specific type name, a help message with valid values, and a bool that represents whether the original error should be suppressed because it is too golang-specific.

func GetParseHelpKind

func GetParseHelpKind(k reflect.Kind) string

func GetSpecialArgs

func GetSpecialArgs(obj interface{}) map[string]string

func GetSpecifiedFields

func GetSpecifiedFields(data *MapData, obj interface{}) []string

Get a list of fields specified as their hierarchical id space. I.e. a field will be "1", or "2.2", based on the protobuf id and hierarchy. Data contains the specified data hierarchically arranged. Obj is the protobuf object with protobuf tags on fields that corresponds to the data.

func GroupRunE

func GroupRunE(cmd *cobra.Command, args []string) error

For group commands, if no subcommand is specified, we want to return an error (unless help was specified).

func LocalFlagsUsageNoNewline

func LocalFlagsUsageNoNewline(cmd *cobra.Command) string

For usage, the cobra code converts the entire usage to a string and then calls Println on it. This ends up introducing an extra unsightly newline in the output. Flags should be last, so this gets the flags output without the final new line.

func MapJsonNamesT

func MapJsonNamesT(dat, js map[string]interface{}, t reflect.Type, inputNS FieldNamespace) error

func MapToArgs

func MapToArgs(prefix []string, dat map[string]interface{}, ignore map[string]struct{}, specialArgs map[string]string, aliases map[string]string) []string

func MarshalArgs

func MarshalArgs(obj interface{}, ignore []string, aliases []string, queryParams map[string]string) ([]string, error)

MarshalArgs generates a name=val arg list from the object. Arg names that should be ignore can be specified. Names are the same format as arg names, lowercase of field names, joined by '.' Aliases are of the form alias=hiername.

func WeakDecode

func WeakDecode(input, output interface{}, hook mapstructure.DecodeHookFunc) ([]string, error)

Use mapstructure to convert an args map (map[string]interface{}) to fill in an object in output.

Types

type Command

type Command struct {
	Use                  string
	Short                string
	RequiredArgs         string
	OptionalArgs         string
	AliasArgs            string
	SpecialArgs          *map[string]string
	Comments             map[string]string
	QueryParams          string
	QueryComments        map[string]string
	ReqData              interface{}
	ReplyData            interface{}
	PasswordArg          string
	CurrentPasswordArg   string
	VerifyPassword       bool
	DataFlagOnly         bool
	StreamOut            bool
	StreamOutIncremental bool
	CobraCmd             *cobra.Command
	Run                  func(c *Command, args []string) error
	UsageIsHelp          bool
	Annotations          map[string]string
	AddFlagsFunc         func(*pflag.FlagSet)
	// contains filtered or unexported fields
}

func (*Command) GenCmd

func (c *Command) GenCmd() *cobra.Command

func (*Command) ParseInput

func (c *Command) ParseInput(args []string) (*MapData, error)

ParseInput converts args to generic map. Input can come in 3 flavors, arg=value lists, yaml data, or json data. The output is generic map[string]interface{} holding the data, but normally each input format would have slightly different values for the map keys (field names) due to json or yaml tags being different from the go struct field name and each other. So we settle on the output map using json field names for consistency.

func (*Command) WriteOutput

func (c *Command) WriteOutput(out io.Writer, objs interface{}, format string) error

note slightly different function in cmdsup.WriteOutputGeneric, perhaps we can consolidate them.

type FieldNamespace

type FieldNamespace int

FieldNamespace describes the format of field names in generic map[string]interface{} data, whether they correspond to the go struct's field names, yaml tag names, json tag names, or arg names. Note that arg names are just lower-cased field names. It does not specify what format the values are in. Values may be type-specific (if extracted from an object with type info), or may be generic values (if unmarshaling yaml/json into map[string]interface{} where type info is not present).

const (
	StructNamespace FieldNamespace = iota
	YamlNamespace
	JsonNamespace
	ArgsNamespace
)

func (FieldNamespace) String

func (s FieldNamespace) String() string

type GetStructMapOp

type GetStructMapOp func(opts *GetStructMapOptions)

func WithStructMapFieldFlags

func WithStructMapFieldFlags(fieldFlags []string) GetStructMapOp

Only include fields specified by the field flags, which are based on the protobuf id tag.

func WithStructMapOmitEmpty

func WithStructMapOmitEmpty() GetStructMapOp

Omit empty fields.

type GetStructMapOptions

type GetStructMapOptions struct {
	// contains filtered or unexported fields
}

type GroupCommand

type GroupCommand struct {
	UsageIsHelp bool
}

type Input

type Input struct {
	// Required argument names
	RequiredArgs []string
	// Alias argument names, format is alias=real
	AliasArgs []string
	// Special argument names, format is arg=argType
	SpecialArgs *map[string]string
	// Password arg will prompt for password if not in args list
	PasswordArg string
	// Current password arg will prompt for current password if not in args list
	CurrentPasswordArg string
	// API key arg will replace password and avoid prompt for password
	ApiKeyArg string
	// Verify password if prompting
	VerifyPassword bool
	// Mapstructure DecodeHook functions
	DecodeHook mapstructure.DecodeHookFunc
	// Allow extra args that were not mapped to target object.
	AllowUnused bool
	// Args that are query params and not part of the object
	QueryParams []string
}

func (*Input) ParseArgs

func (s *Input) ParseArgs(args []string, obj interface{}) (*MapData, error)

Args are format name=val, where name could be a hierarchical name separated by ., i.e. appdata.key.name. Arg names should be all lowercase, matching the struct field names. This returns a generic map of values set by the args, again based on lower case field names, ignoring any json/yaml tags. It also fills in obj if specified. NOTE: arrays and maps not supported yet.

type MapData

type MapData struct {
	Namespace   FieldNamespace
	Data        map[string]interface{}
	QueryParams map[string]string
}

MapData associates generic imported mapped data with the namespace that the keys are in.

func GetStructMap

func GetStructMap(obj interface{}, ops ...GetStructMapOp) (*MapData, error)

GetStructMap converts the object to a StructMap.

func JsonMap

func JsonMap(mdat *MapData, obj interface{}) (*MapData, error)

This converts the key namespace from whatever is specified into JSON key names, based on json tags on the object. The values are not changed. It will squash hierarchy if the "inline" tag is found.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL