cliutil

package
v0.2.28 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cliutil is common command line utils for Go.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidCmd = errors.New("invalid command")

ErrInvalidCmd represents a invalid command error.

Functions

func DefaultLoggingFunc added in v0.1.7

func DefaultLoggingFunc(_ []byte)

DefaultLoggingFunc do nothing

func ExecContext

func ExecContext(ctx context.Context, command string, args ...string) (output string, err error)

ExecContext executes the given command.

func ExecErrPipe added in v0.1.33

func ExecErrPipe(ctx context.Context, fn LoggingFunc, command string, args ...string) error

ExecErrPipe executes the given command with a pipe that will be connected to the command's stderr when the command starts.

func ExecPipe

func ExecPipe(ctx context.Context, fn LoggingFunc, command string, args ...string) error

ExecPipe executes the given command with a pipe that will be connected to the command's stdout when the command starts.

func IsHelpCmd added in v0.1.37

func IsHelpCmd(short ...string) bool

IsHelpCmd this function simply calls RetrieveFlagFromCLI("--help", "") Optionally, a single extra argument of type string can be provided to indicate the short name of the help flag.

func IsVersionCmd added in v0.1.37

func IsVersionCmd(short ...string) bool

IsVersionCmd this function simply calls RetrieveFlagFromCLI("--version", "") Optionally, a single extra argument of type string can be provided to indicate the short name of the version flag.

func RetrieveFlag added in v0.1.14

func RetrieveFlag(args []string, long, short string) (value string, ok bool)

RetrieveFlag returns value of the given flag from args.

func RetrieveFlagFromCLI added in v0.1.14

func RetrieveFlagFromCLI(long string, short string) (value string, ok bool)

RetrieveFlagFromCLI returns value of the given flag from os.Args.

func ShellExec

func ShellExec(command string) (output string, err error)

ShellExec executes the given command by shell, errors.g. "ls -al"

func ShellExecContext

func ShellExecContext(ctx context.Context, command string) (output string, err error)

ShellExecContext executes the given command by shell.

func ShellExecPipe

func ShellExecPipe(ctx context.Context, fn LoggingFunc, command string) error

ShellExecPipe executes the given command with a pipe that will be connected to the command's stdout and stderr when the command starts.

Types

type ErrExit

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

ErrExit error with stdout and stderr.

func (*ErrExit) Code

func (e *ErrExit) Code() int

Code returns exit code.

func (*ErrExit) Error

func (e *ErrExit) Error() string

Error implements error.

func (*ErrExit) Stderr

func (e *ErrExit) Stderr() string

Stderr returns stderr.

func (*ErrExit) Stdout

func (e *ErrExit) Stdout() string

Stdout returns stdout.

type LoggingFunc added in v0.1.7

type LoggingFunc func(line []byte)

LoggingFunc callback function for logging command output

Jump to

Keyboard shortcuts

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