cli

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cli provides the CLI config component of go-micro.

Package cli provides cli.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Plugins contains source/cli subplugins, for example urfave, pflag, cobra.
	Plugins = container.NewMap[string, ParseFunc]()
	// Flags is the global flag container where you have to register flags with.
	Flags = container.NewList[*Flag]()
)
View Source
var (
	// DefaultCLIPlugin holds the default CLI plugin.
	DefaultCLIPlugin = "urfave" //nolint:gochecknoglobals
)
View Source
var (
	// ErrFlagExists is returned when the given element exists in the flag container.
	ErrFlagExists = errors.New("element exists already")
)

Functions

func FlagValue

func FlagValue[T any](f *Flag) (T, error)

FlagValue gets a value back from a Flag and enforces types.

func New

func New() source.Source

New creates a new cli source.

Types

type Config

type Config struct {
	Name    string `json:"name"    yaml:"name"`
	Version string `json:"version" yaml:"version"`
}

Config is the base config for this component.

func NewConfig

func NewConfig() Config

NewConfig returns the cli config.

type Flag

type Flag struct {
	Name    string
	EnvVars []string
	Usage   string

	// The path in map(\[string\])+any
	ConfigPath []string

	Default any
	Value   any
}

Flag is a Cli Flag and maybe environment variable.

func NewFlag

func NewFlag[T any](
	name string,
	defaultValue T,
	opts ...FlagOption,
) *Flag

NewFlag creates a new CLI flag.

func (*Flag) String

func (f *Flag) String() string

type FlagOption

type FlagOption func(*Flag)

FlagOption is an option for NewFlag.

func ConfigPath

func ConfigPath(n string) FlagOption

ConfigPath sets the ConfigPath for the flag.

func ConfigPathSlice

func ConfigPathSlice(n []string) FlagOption

ConfigPathSlice is the same as ConfigPath but it accepts a slice.

func Default

func Default[T any](n T) FlagOption

Default sets the flags default.

func EnvVars

func EnvVars(n ...string) FlagOption

EnvVars set's environment variables for the flag.

func Usage

func Usage(n string) FlagOption

Usage set's the usage string for the flag.

type ParseFunc

type ParseFunc func(config *Config, flags []*Flag, args []string) error

ParseFunc is the subplugin of source/cli.

type Source

type Source struct{}

Source cli reads flags and environment variables into a config struct.

func (*Source) PrependSections

func (s *Source) PrependSections() bool

PrependSections indicates whether this needs sections to be prepended, which is true in this case.

func (*Source) Read

func (s *Source) Read(u *url.URL) source.Data

Read creates the subplugin for the given url, creates its config after and then executes it.

func (*Source) Schemes

func (s *Source) Schemes() []string

Schemes returns the supported schemes by this plugin.

func (*Source) String

func (s *Source) String() string

String returns the name of this plugin.

Jump to

Keyboard shortcuts

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