genericclioptions

package
v0.1.0-devel.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: Unlicense Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsAny

func ContainsAny(str string, substrings ...string) bool

ContainsAny reports whether str contains any of substrings.

func ExecuteCommand

func ExecuteCommand(ctx context.Context, cmd CmdOptions, args ...string) error

ExecuteCommand executes the provided command options by first completing, then validating, and finally running the command.

func MarkAllFlagsHidden

func MarkAllFlagsHidden(target *cobra.Command, excluded ...string)

MarkAllFlagsHidden hides all flags from the target's help output.

func MarkFlagsHidden

func MarkFlagsHidden(target *cobra.Command, hidden ...string)

MarkFlagsHidden hides the given flags from the target's help output.

func NewMockFileInfo

func NewMockFileInfo(name string, size int64, mode os.FileMode, isDir bool, t time.Time) os.FileInfo

func NewTestIOStreams

func NewTestIOStreams(r *TestFdReader) (iostream *IOStreams, in *TestFdReader, out *bytes.Buffer, errOut *bytes.Buffer)

NewTestIOStreamsWithMockInput returns IOStreams with mock input, a TestFdReader and out and error buffers for unit tests.

func ParseLevel

func ParseLevel(s string) (slog.Level, error)

func RejectDisallowedFlags

func RejectDisallowedFlags(cmd *cobra.Command, disallowed ...string) error

func RemoveLinesContaining

func RemoveLinesContaining(s string, substrings ...string) string

RemoveLinesContaining removes any lines that contain any of substrings.

Types

type BaseOptions

type BaseOptions interface {
	Complete() error // Complete prepares the options for the command by setting required values.
	Validate() error // Validate checks that the options are valid before running the command.
}

BaseOptions defines the interface for shared setup and validation logic.

type CmdOptions

type CmdOptions interface {
	BaseOptions

	Run(ctx context.Context, args ...string) error
}

CmdOptions includes BaseOptions and adds the ability to run the command logic.

type FdReader

type FdReader interface {
	Fd() uintptr
	Stat() (os.FileInfo, error)

	io.Reader
}

FdReader defines the interface for file-like objects that can be read from, provide a file descriptor, and return file information.

type IOStreams

type IOStreams struct {
	In     FdReader
	Out    io.Writer
	ErrOut io.Writer
	// contains filtered or unexported fields
}

func NewDefaultIOStreams

func NewDefaultIOStreams() *IOStreams

NewDefaultIOStreams returns the default IOStreams (using os.Stdin, os.Stdout, os.Stderr).

func NewTestIOStreamsDiscard

func NewTestIOStreamsDiscard(in *TestFdReader) *IOStreams

NewTestIOStreamsDiscard returns IOStreams with mocked input, and discards both output and error output.

func (*IOStreams) Debugf

func (io *IOStreams) Debugf(format string, args ...any)

Debugf writes formatted debug output to the error stream. if Verbose is enabled.

func (*IOStreams) Errorf

func (io *IOStreams) Errorf(format string, args ...any)

Errorf writes a formatted message to the error stream.

func (*IOStreams) Infof

func (io *IOStreams) Infof(format string, args ...any)

Infof writes a formatted message to the standard output stream.

func (*IOStreams) Print

func (io *IOStreams) Print(s string)

Print writes a general, unprefixed message to the standard output stream.

func (*IOStreams) Printf

func (io *IOStreams) Printf(format string, args ...any)

Printf writes a general, unprefixed formatted message to the standard output stream.

func (*IOStreams) SetLevel

func (io *IOStreams) SetLevel(l slog.Level)

func (*IOStreams) Warnf

func (io *IOStreams) Warnf(format string, args ...any)

Warnf writes a formatted message to the standard output stream.

type StdioOption

type StdioOption func(*StdioOptions)

func WithErr

func WithErr(w io.Writer) StdioOption

func WithIn

func WithIn(r FdReader) StdioOption

func WithLevel

func WithLevel(l slog.Level) StdioOption

func WithLogger

func WithLogger(logger *slog.Logger) StdioOption

func WithOut

func WithOut(w io.Writer) StdioOption

type StdioOptions

type StdioOptions struct {
	*IOStreams

	Logger *slog.Logger
	Piped  bool
	// contains filtered or unexported fields
}

StdioOptions provides stdin-related CLI helpers, intended to be embedded in option structs.

func NewStdioOptions

func NewStdioOptions() *StdioOptions

NewStdioOptions creates a new StdioOptions with default streams and logger.

func (*StdioOptions) Complete

func (o *StdioOptions) Complete() error

Complete sets default values, e.g., enabling Stdin if piped input is detected.

func (*StdioOptions) Opts

func (o *StdioOptions) Opts(opts ...StdioOption)

func (*StdioOptions) Validate

func (o *StdioOptions) Validate() error

Validate ensures the input mode (Stdin or interactive) is used appropriately.

type TestFdReader

type TestFdReader struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

func NewTestFdReader

func NewTestFdReader(b *bytes.Buffer, fd uintptr, fi os.FileInfo) *TestFdReader

func (*TestFdReader) Fd

func (r *TestFdReader) Fd() uintptr

func (*TestFdReader) Stat

func (r *TestFdReader) Stat() (os.FileInfo, error)

Jump to

Keyboard shortcuts

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