base

package
v1.59.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExperimentalWrapper added in v1.52.0

func ExperimentalWrapper(product, maturity, url string) func(state.State, *cobra.Command) *cobra.Command

ExperimentalWrapper create a command wrapper that appends a notice to the command descriptions and logs a warning when it is used.

Usage:

var (
	ExperimentalProduct = ExperimentalWrapper("Product", "in beta", "https://docs.hetzner.cloud/changelog#new-product")
)

func (c) CobraCommand(s state.State) *cobra.Command {
	cmd := &cobra.Command{
		Use:     "command",
		Short:   "My experimental command",
		Long:    "This is an experimental command.",
		PreRunE: s.EnsureToken,
	}

	cmd.Run = func(cmd *cobra.Command, _ []string) {}

	return ExperimentalProduct(s, cmd)
}

Types

type ChangeProtectionCmds added in v1.58.0

type ChangeProtectionCmds[T, Opts any] struct {
	ResourceNameSingular    string // e.g. "Server"
	ShortEnableDescription  string
	ShortDisableDescription string
	NameSuggestions         func(client hcapi2.Client) func() []string
	AdditionalFlags         func(*cobra.Command)
	// Fetch is called to fetch the resource to describe.
	Fetch func(s state.State, cmd *cobra.Command, idOrName string) (T, *hcloud.Response, error)
	// Can be set in case the resource has more than a single identifier that is used in the positional arguments.
	// See [ChangeProtectionCmds.PositionalArgumentOverride].
	FetchWithArgs func(s state.State, cmd *cobra.Command, args []string) (T, *hcloud.Response, error)

	// In case the resource does not have a single identifier that matches [ChangeProtectionCmds.ResourceNameSingular], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>
	PositionalArgumentOverride []string

	// Can be set if the default [ChangeProtectionCmds.NameSuggestions] is not enough. This is usually the case when
	// [ChangeProtectionCmds.FetchWithArgs] and [ChangeProtectionCmds.PositionalArgumentOverride] is being used.
	ValidArgsFunction func(client hcapi2.Client) []cobra.CompletionFunc

	// ProtectionLevels maps all available protection levels to a function that sets the corresponding value in the Opts struct
	ProtectionLevels map[string]func(opts *Opts, value bool)

	// If ProtectionLevelsOptional is set, all protection levels will always be applied
	ProtectionLevelsOptional bool

	// ChangeProtectionFunction is used to change the protection on a specific resource given the Opts
	ChangeProtectionFunction func(s state.State, resource T, opts Opts) (*hcloud.Action, *hcloud.Response, error)

	// IDOrName is used to retrieve a string representation of the resource
	IDOrName func(resource T) string

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

ChangeProtectionCmds allows defining commands for changing a resource's protection.

func (*ChangeProtectionCmds[T, Opts]) ChangeProtection added in v1.58.0

func (cpc *ChangeProtectionCmds[T, Opts]) ChangeProtection(s state.State, cmd *cobra.Command,
	resource T, enable bool, opts Opts) error

func (*ChangeProtectionCmds[T, Opts]) DisableCobraCommand added in v1.58.0

func (cpc *ChangeProtectionCmds[T, Opts]) DisableCobraCommand(s state.State) *cobra.Command

DisableCobraCommand creates a disable-protection command that can be registered with cobra.

func (*ChangeProtectionCmds[T, Opts]) EnableCobraCommand added in v1.58.0

func (cpc *ChangeProtectionCmds[T, Opts]) EnableCobraCommand(s state.State) *cobra.Command

EnableCobraCommand creates an enable-protection command that can be registered with cobra.

func (*ChangeProtectionCmds[T, Opts]) GetChangeProtectionOpts added in v1.58.0

func (cpc *ChangeProtectionCmds[T, Opts]) GetChangeProtectionOpts(enable bool, levels []string) (Opts, error)

func (*ChangeProtectionCmds[T, Opts]) Run added in v1.58.0

func (cpc *ChangeProtectionCmds[T, Opts]) Run(s state.State, cmd *cobra.Command, args []string, enable bool) error

Run executes a describe command.

type Cmd

type Cmd struct {
	BaseCobraCommand func(hcapi2.Client) *cobra.Command
	Run              func(state.State, *cobra.Command, []string) error

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

Cmd allows defining commands for generic resource-based commands

func (*Cmd) CobraCommand

func (gc *Cmd) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

type CreateCmd added in v1.40.0

type CreateCmd[T any] struct {
	BaseCobraCommand func(hcapi2.Client) *cobra.Command
	// Run is the function that will be called when the command is executed.
	// It should return the created resource, the schema of the resource and an error.
	Run           func(state.State, *cobra.Command, []string) (T, any, error)
	PrintResource func(state.State, *cobra.Command, T)
	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

CreateCmd allows defining commands for resource creation

func (*CreateCmd[T]) CobraCommand added in v1.40.0

func (cc *CreateCmd[T]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

type DeleteCmd added in v1.25.0

type DeleteCmd[T any] struct {
	ResourceNameSingular string // e.g. "Server"
	ResourceNamePlural   string // e.g. "Servers"
	ShortDescription     string
	NameSuggestions      func(client hcapi2.Client) func() []string
	AdditionalFlags      func(*cobra.Command)
	Fetch                FetchFunc[T]
	Delete               func(s state.State, cmd *cobra.Command, resource T) (*hcloud.Action, error)

	// FetchFunc is a factory function that produces [DeleteCmd.Fetch]. Should be set in case the resource has
	// more than a single identifier that is used in the positional arguments.
	// See [DeleteCmd.PositionalArgumentOverride].
	FetchFunc func(s state.State, cmd *cobra.Command, args []string) (FetchFunc[T], error)

	// In case the resource does not have a single identifier that matches [DeleteCmd.ResourceNameSingular], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>...
	// Where c is are resources to be deleted.
	PositionalArgumentOverride []string

	// Can be set if the default [DeleteCmd.NameSuggestions] is not enough. This is usually the case when
	// [DeleteCmd.FetchWithArgs] and [DeleteCmd.PositionalArgumentOverride] is being used.
	ValidArgsFunction func(client hcapi2.Client) []cobra.CompletionFunc

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

DeleteCmd allows defining commands for deleting a resource.

func (*DeleteCmd[T]) CobraCommand added in v1.25.0

func (dc *DeleteCmd[T]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

func (*DeleteCmd[T]) Run added in v1.25.0

func (dc *DeleteCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) error

Run executes a delete command.

type DescribeCmd

type DescribeCmd[T any] struct {
	ResourceNameSingular string // e.g. "Server"
	ShortDescription     string
	NameSuggestions      func(client hcapi2.Client) func() []string
	AdditionalFlags      func(*cobra.Command)
	// Fetch is called to fetch the resource to describe.
	// The first returned interface is the resource itself as a hcloud struct, the second is the schema for the resource.
	Fetch func(s state.State, cmd *cobra.Command, idOrName string) (T, any, error)
	// Can be set in case the resource has more than a single identifier that is used in the positional arguments.
	// See [DescribeCmd.PositionalArgumentOverride].
	FetchWithArgs func(s state.State, cmd *cobra.Command, args []string) (T, any, error)

	PrintText   func(s state.State, cmd *cobra.Command, out io.Writer, resource T) error
	GetIDOrName func(resource T) string

	// In case the resource does not have a single identifier that matches [DescribeCmd.ResourceNameSingular], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>
	PositionalArgumentOverride []string

	// Can be set if the default [DescribeCmd.NameSuggestions] is not enough. This is usually the case when
	// [DescribeCmd.FetchWithArgs] and [DescribeCmd.PositionalArgumentOverride] is being used.
	ValidArgsFunction func(client hcapi2.Client) []cobra.CompletionFunc

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

DescribeCmd allows defining commands for describing a resource.

func (*DescribeCmd[T]) CobraCommand

func (dc *DescribeCmd[T]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

func (*DescribeCmd[T]) Run

func (dc *DescribeCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) error

Run executes a describe command.

type FetchFunc added in v1.53.0

type FetchFunc[T any] func(s state.State, cmd *cobra.Command, idOrName string) (T, *hcloud.Response, error)

type LabelCmds

type LabelCmds[T any] struct {
	ResourceNameSingular   string
	ShortDescriptionAdd    string
	ShortDescriptionRemove string
	NameSuggestions        func(client hcapi2.Client) func() []string
	LabelKeySuggestions    func(client hcapi2.Client) func(idOrName string) []string
	Fetch                  func(s state.State, idOrName string) (T, error)

	// Can be set in case the resource has more than a single identifier that is used in the positional arguments.
	// See [LabelCmds.PositionalArgumentOverride].
	FetchWithArgs func(s state.State, args []string) (T, error)
	SetLabels     func(s state.State, resource T, labels map[string]string) error
	GetLabels     func(resource T) map[string]string
	GetIDOrName   func(resource T) string

	// In case the resource does not have a single identifier that matches [LabelCmds.ResourceNameSingular], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>
	PositionalArgumentOverride []string

	// Can be set if the default [LabelCmds.NameSuggestions] is not enough. This is usually the case when
	// [LabelCmds.FetchWithArgs] and [LabelCmds.PositionalArgumentOverride] is being used.
	//
	// If this is being set [LabelCmds.LabelKeySuggestions] is ignored and its functionality must be
	// provided as part of the [LabelCmds.ValidArgsFunction].
	ValidArgsFunction func(client hcapi2.Client) []cobra.CompletionFunc

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

LabelCmds allows defining commands for adding labels to resources.

func (*LabelCmds[T]) AddCobraCommand

func (lc *LabelCmds[T]) AddCobraCommand(s state.State) *cobra.Command

AddCobraCommand creates a command that can be registered with cobra.

func (*LabelCmds[T]) RemoveCobraCommand

func (lc *LabelCmds[T]) RemoveCobraCommand(s state.State) *cobra.Command

RemoveCobraCommand creates a command that can be registered with cobra.

func (*LabelCmds[T]) RunAdd

func (lc *LabelCmds[T]) RunAdd(s state.State, cmd *cobra.Command, args []string) error

RunAdd executes an add label command

func (*LabelCmds[T]) RunRemove

func (lc *LabelCmds[T]) RunRemove(s state.State, cmd *cobra.Command, args []string) error

RunRemove executes a remove label command

type ListCmd

type ListCmd[T any, S any] struct {
	SortOption         *config.Option[[]string]
	ResourceNamePlural string // e.g. "Servers"
	JSONKeyGetByName   string // e.g. "Servers"
	DefaultColumns     []string
	Fetch              func(state.State, *pflag.FlagSet, hcloud.ListOpts, []string) ([]T, error)
	// Can be set in case the resource has more than a single identifier that is used in the positional arguments.
	// See [ListCmd.PositionalArgumentOverride].
	FetchWithArgs   func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string, args []string) ([]T, error)
	AdditionalFlags func(*cobra.Command)
	OutputTable     func(t *output.Table[T], client hcapi2.Client)
	Schema          func(T) S

	// In case the resource does not have a single identifier that matches [ListCmd.ResourceNamePlural], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>
	PositionalArgumentOverride []string

	// Can be set if auto-completion is needed (usually if [ListCmd.FetchWithArgs] is used)
	ValidArgsFunction func(client hcapi2.Client) cobra.CompletionFunc

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

ListCmd allows defining commands for listing resources T is the type of the resource that is listed, e.g. *hcloud.Server S is the type of the schema that is returned, e.g. schema.Server

func (*ListCmd[T, S]) CobraCommand

func (lc *ListCmd[T, S]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

func (*ListCmd[T, S]) FetchAny added in v1.52.0

func (lc *ListCmd[T, S]) FetchAny(s state.State, fs *pflag.FlagSet, opts hcloud.ListOpts, sorts []string) ([]any, error)

func (*ListCmd[T, S]) GetDefaultColumns added in v1.52.0

func (lc *ListCmd[T, S]) GetDefaultColumns() []string

func (*ListCmd[T, S]) GetJSONKeyGetByName added in v1.52.0

func (lc *ListCmd[T, S]) GetJSONKeyGetByName() string

func (*ListCmd[T, S]) GetResourceNamePlural added in v1.52.0

func (lc *ListCmd[T, S]) GetResourceNamePlural() string

func (*ListCmd[T, S]) NewOutputTable added in v1.56.0

func (lc *ListCmd[T, S]) NewOutputTable(out io.Writer, client hcapi2.Client) *output.Table[any]

func (*ListCmd[T, S]) Run

func (lc *ListCmd[T, S]) Run(s state.State, cmd *cobra.Command, args []string) error

Run executes a list command

func (*ListCmd[T, S]) SchemaAny added in v1.52.0

func (lc *ListCmd[T, S]) SchemaAny(resource any) any

type Listable added in v1.52.0

type Listable interface {
	GetResourceNamePlural() string
	GetJSONKeyGetByName() string
	GetDefaultColumns() []string
	NewOutputTable(io.Writer, hcapi2.Client) *output.Table[any]
	FetchAny(state.State, *pflag.FlagSet, hcloud.ListOpts, []string) ([]any, error)
	SchemaAny(any) any
}

Listable is an interface that defines the methods required for a resource to be listed. It is needed because ListCmd is a generic type, and we don't always know the concrete type of the resource. See [all.ListCmd]

type SetRdnsCmd added in v1.28.1

type SetRdnsCmd[T hcloud.RDNSSupporter] struct {
	ResourceNameSingular string // e.g. "Server"
	ShortDescription     string
	NameSuggestions      func(client hcapi2.Client) func() []string
	Fetch                func(s state.State, cmd *cobra.Command, idOrName string) (T, *hcloud.Response, error)
	GetDefaultIP         func(resource T) net.IP

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

SetRdnsCmd allows defining commands for setting the RDNS of a resource.

func (*SetRdnsCmd[T]) CobraCommand added in v1.28.1

func (rc *SetRdnsCmd[T]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

func (*SetRdnsCmd[T]) Run added in v1.28.1

func (rc *SetRdnsCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) error

Run executes a setRDNS command.

type UpdateCmd added in v1.25.0

type UpdateCmd[T any] struct {
	ResourceNameSingular string // e.g. "Server"
	ShortDescription     string
	NameSuggestions      func(client hcapi2.Client) func() []string
	DefineFlags          func(*cobra.Command)

	Fetch func(s state.State, cmd *cobra.Command, idOrName string) (T, *hcloud.Response, error)
	// Can be set in case the resource has more than a single identifier that is used in the positional arguments.
	// See [UpdateCmd.PositionalArgumentOverride].
	FetchWithArgs func(s state.State, cmd *cobra.Command, args []string) (T, *hcloud.Response, error)

	Update func(s state.State, cmd *cobra.Command, resource T, flags map[string]pflag.Value) error

	// In case the resource does not have a single identifier that matches [UpdateCmd.ResourceNameSingular], this field
	// can be set to define the list of positional arguments.
	// For example, passing:
	//     []string{"a", "b", "c"}
	// Would result in the usage string:
	//     <a> <b> <c>
	PositionalArgumentOverride []string

	// Can be set if the default [UpdateCmd.NameSuggestions] is not enough. This is usually the case when
	// [UpdateCmd.FetchWithArgs] and [UpdateCmd.PositionalArgumentOverride] is being used.
	ValidArgsFunction func(client hcapi2.Client) []cobra.CompletionFunc

	// Experimental is a function that will be used to mark the command as experimental.
	Experimental func(state.State, *cobra.Command) *cobra.Command
}

UpdateCmd allows defining commands for updating a resource.

func (*UpdateCmd[T]) CobraCommand added in v1.25.0

func (uc *UpdateCmd[T]) CobraCommand(s state.State) *cobra.Command

CobraCommand creates a command that can be registered with cobra.

func (*UpdateCmd[T]) Run added in v1.25.0

func (uc *UpdateCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) error

Run executes a update command.

Jump to

Keyboard shortcuts

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