cli

package
v0.0.0-...-b5b6610 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ConfigFileParam = configparam.String("config", "Configuration file").WithShortName("c")

Functions

func Execute

func Execute()

Execute function is the main entrypoint for the CLI tool.

Example
package main

import (
	"github.com/SAP/xp-clifford/cli"
)

func main() {
	cli.Configuration.ShortName = "ts"
	cli.Configuration.ObservedSystem = "test system"
	cli.Execute()
}

func RegisterSubCommand

func RegisterSubCommand(command SubCommand)

RegisterSubCommand registers a subcommand to the CLI application.

Example
package main

import (
	"context"
	"fmt"

	"github.com/SAP/xp-clifford/cli"
	"github.com/SAP/xp-clifford/cli/configparam"
)

func main() {
	cli.Configuration.ShortName = "ts"
	cli.Configuration.ObservedSystem = "test system"
	subcommand := &cli.BasicSubCommand{
		Name:             "login",
		Short:            "login to test system",
		Long:             "perform a login to test system",
		ConfigParams:     []configparam.ConfigParam{},
		IgnoreConfigFile: false,
		Run: func(_ context.Context) error {
			fmt.Println("login subcommand invoked")
			return nil
		},
	}
	cli.RegisterSubCommand(subcommand)
	cli.Execute()
}

Types

type BasicSubCommand

type BasicSubCommand struct {
	Name             string
	Short            string
	Long             string
	ConfigParams     configparam.ParamList
	IgnoreConfigFile bool
	Run              func(context.Context) error
}

BasicSubCommand implements the SubCommand interface.

func (*BasicSubCommand) GetConfigParams

func (s *BasicSubCommand) GetConfigParams() configparam.ParamList

func (*BasicSubCommand) GetLong

func (s *BasicSubCommand) GetLong() string

func (*BasicSubCommand) GetName

func (s *BasicSubCommand) GetName() string

func (*BasicSubCommand) GetRun

func (s *BasicSubCommand) GetRun() func(context.Context) error

func (*BasicSubCommand) GetShort

func (s *BasicSubCommand) GetShort() string

func (*BasicSubCommand) MustIgnoreConfigFile

func (s *BasicSubCommand) MustIgnoreConfigFile() bool

type CLIConfiguration

type CLIConfiguration struct {
	ConfiguratorCLI

	// ShortName is the abbreviated name of the observed system
	// that does not contain spaces, like "cf" for CloudFoundry
	// provider
	ShortName string

	// ObservedSystem is the full name of the external system that
	// may contain spaces, like "Cloud Foundry"
	ObservedSystem string

	// HasVerboseFlag indicates whether the CLI tool shall support
	// the --verbose/-v flag.
	HasVerboseFlag bool
}

type ConfigFileSettings

type ConfigFileSettings map[string]any

func (ConfigFileSettings) Set

func (cfs ConfigFileSettings) Set(key string, value any)

func (ConfigFileSettings) StoreConfig

func (cfs ConfigFileSettings) StoreConfig(configFile string) error

type ConfigSchema

type ConfigSchema struct {
	CLIConfiguration
}
var (
	Configuration *ConfigSchema = &ConfigSchema{}
)

type ConfiguratorCLI

type ConfiguratorCLI interface {
	// CommandUse method returns the one-line usage message.
	CommandUse(config *ConfigSchema) string
	// CommandShort method returns the short description shown in the 'help' output.
	CommandShort(config *ConfigSchema) string
	// CommandLong method returns the long message shown in the 'help <this-command>' output.
	CommandLong(config *ConfigSchema) string
}

ConfiguratorCLI defines the methods that a value has to support to provider lazy CLI configuration parameters.

type DefaultConfiguratorCLI

type DefaultConfiguratorCLI struct{}

DefaultConfiguratorCLI type satisfies the ConfiguratorCLI interface. It provides a basic method implementations based on the values set in the ConfigSchema.

func (DefaultConfiguratorCLI) CommandLong

func (c DefaultConfiguratorCLI) CommandLong(config *ConfigSchema) string

func (DefaultConfiguratorCLI) CommandShort

func (c DefaultConfiguratorCLI) CommandShort(config *ConfigSchema) string

func (DefaultConfiguratorCLI) CommandUse

func (c DefaultConfiguratorCLI) CommandUse(config *ConfigSchema) string

type SubCommand

type SubCommand interface {
	// GetName returns the name of the subcommand, such as 'login' or 'cleanup'.
	GetName() string
	// GetShort returns the short description shown in the 'help' output.
	GetShort() string
	// GetLong returns the long message shown in the 'help <this-command>' output.
	GetLong() string
	// GetConfigParams returns the configuration parameters that can be applied for the subcommand.
	GetConfigParams() configparam.ParamList
	// MustIgnoreConfigFile returns true if the subcommand shall ignore the parameters set in the configuration file.
	MustIgnoreConfigFile() bool
	// GetRun returns the function that is to be executed when the subcommand is invoked.
	GetRun() func(context.Context) error
}

SubCommand interface defines the methods that the types which implement a subcommand must implement.

Directories

Path Synopsis
Package configparam defines the configuration parameters that a CLI tool can use.
Package configparam defines the configuration parameters that a CLI tool can use.
Package export defines the export subcommand.
Package export defines the export subcommand.
Package widget supplies interactive CLI widgets.
Package widget supplies interactive CLI widgets.

Jump to

Keyboard shortcuts

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