ghshell

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLineBufferOverflow = errors.New("line buffer overflow")

	ErrAlreadyFinished      = errors.New("already finished")
	ErrNotFoundCommand      = errors.New("command not found")
	ErrNotExecutePermission = errors.New("not execute permission")
	ErrInvalidArgs          = errors.New("Invalid argument to exit")
	ErrProcessTimeout       = errors.New("throw process timeout")
	ErrProcessCancel        = errors.New("active cancel process")

	DefaultExitCode = 2
)
View Source
var (
	TES = ""
	NL  = "\n"
	TAB = "\t"
)
View Source
var (
	GoBot = goBot{

		DefaultGitCommitMessage: "",
		GobotDevAutosaveMessage: defaultDevAutosaveMessage,
		GobotAutoSaveTimer:      defaultAutoSaveInterval,
		// contains filtered or unexported fields
	}

	GoBotCliWriter = cliWriter{
		Writer:  nil,
		Verbose: true,
		Out:     os.Stdout,
		Debug:   true,
		Err:     os.Stderr,
	}
)
View Source
var (
	Err = log.Err
)

Functions

func AppArgs

func AppArgs(usage string, in ...string) (appMessage string, args []string, err error)

AppArgs parses the input arguments into a set of app, args strings variables designed for use with exec.Command.

Arguments are split on any unicode whitespace and the first arg is returned as the 'app' while the remaining are returned as a slice of strings.

Any error is also returned as a standard error.

As a courtesy, if a usage string is provided, it will be used instead of the default empty string 'app' when no arguments are provided.

func CheckPnameRunning

func CheckPnameRunning(pname string) bool

CheckPnameRunning checks to see if a program is running.

func Command

func Command(args string) (out string, errno int, err error)

Command executes a command and returns CombinedOutput, exitcode, and err. If Stderr contains a message, the Go error is wrapped in a Stderr message.

func CommandContainsAll

func CommandContainsAll(args string, subs ...string) bool

CommandContainsAll executes a command then searches for matches to any substrings in the output string. If ANY of the substrings are not found, the command returns false.

func CommandFormat

func CommandFormat(format string, vals ...interface{}) (string, int, error)

Command executes a formatted command and returns CombinedOutput, exitcode, and err

func CommandScript

func CommandScript(script []byte) (string, int, error)

CommandScript writes a script to a temp file and executes that script.

func CommandWithChan

func CommandWithChan(cmd string, queue chan string) error

CommandWithChan executes the command and returns results in a channel

func CommandWithOutErr

func CommandWithOutErr(cmd string) (string, string, int, error)

CommandWithOutErr runs command and return separate output: string(stdout), string(stderr), exitcode, err

func Echo

func Echo(args ...string) (out string, err error)

Echo processes the shell command and and returns the result and any error encountered. In addition, it prints the result to stdout and any error message to stderr.

Any shell error values (errno of type int) are wrapped into the Go error message.

The io.Writers for stdout and stderr can be redirected in the configuration and all stdout and stderr output can be enabled or disabled by using the config settings Verbose and Debug, respectively.

func ErrControl

func ErrControl() error

func Example

func Example()

func Exists

func Exists(path string) bool

func GetOutput

func GetOutput(args ...string) (out string, err error)

GetOutput returns the contents of the Stdout. Any error is returned wrapped with a Stderr message and any errno returned from the shell.

func GitAddAll

func GitAddAll() error

func GitCommit

func GitCommit(message string) error

func GitCommitAll

func GitCommitAll(message string) error

GitCommitAll processes the following commands in sequence:

git add -all
git commit -m "message" (or default)
git push origin main

func GitCurrentBranch

func GitCurrentBranch() (out string, err error)

func GitFetch

func GitFetch() error

func GitPush

func GitPush() error

func GitPushSetUpstreamOrigin

func GitPushSetUpstreamOrigin() error

func Gitit

func Gitit(message string) error

Gitit is a quick and dirty git repo autosave and push.

Gitit runs the following commands in order:

go mod tidy
go doc >|go.doc
git add --all
git commit -m $message
git push

func Home

func Home() string

func IsDir

func IsDir(path string) bool

func LookPath

func LookPath(cmd string) bool

LookPath checks if a command is in the PATH

func Message

func Message(msg string) string

func NewErrorControl

func NewErrorControl() *errorControl

func NewShellError

func NewShellError(errno *int, err error, msg *string, command *string) *shellError

func NormalizeWhitespace

func NormalizeWhitespace(s string, saveNewLines bool) string

NormalizeWhitespace removes any unicode whitespace characters from s and returns a string with all words separate by single spaces (0x20). As a convenience, there is an option to ignore newlines (0xA) and leave them intact.

Whitespace is a space character as defined by Unicode's White Space property; in the Latin-1 space this is

'\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP)

Other definitions of spacing characters are set by category Z and property Pattern_White_Space.

func ToFields

func ToFields(in ...string) []string

ToFields converts any number of string arguments into a slice of fields, as defined by

strings.Fields()

from the standard library, and returns a slice of substrings of s or an empty slice if s contains only white space.

func WithExecMode

func WithExecMode(b bool) optionFunc

WithExecMode set exec mode, example:

["curl", "-i", "-v", "xiaorui.cc"]

func WithSetDir

func WithSetDir(dir string) optionFunc

WithSetDir sets working directory

func WithSetEnv

func WithSetEnv(env []string) optionFunc

WithSetEnv sets environment variables

func WithShellMode

func WithShellMode() optionFunc

WithShellMode sets shell mode for command

func WithTimeout

func WithTimeout(td int) optionFunc

WithTimeout sets command timeout in seconds

Types

type Cmd

type Cmd struct {
	sync.Mutex

	Bash      string
	ShellMode bool
	Status    Status
	Env       []string
	Dir       string
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(bash string, options ...optionFunc) *Cmd

func (*Cmd) Clone

func (c *Cmd) Clone() *Cmd

Clone new Cmd with current config

func (*Cmd) Cost

func (c *Cmd) Cost() time.Duration

Cost returns the time cost for the process

func (*Cmd) Kill

func (c *Cmd) Kill(sig syscall.Signal)

Kill sends a custom signal to the process

func (*Cmd) Run

func (c *Cmd) Run() error

Run starts and waits for process exit

func (*Cmd) Start

func (c *Cmd) Start() error

Start async execute command

func (*Cmd) Stop

func (c *Cmd) Stop()

Stop kill -9 pid

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait waits for command to finish

type OutputBuffer

type OutputBuffer struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

func NewOutputBuffer

func NewOutputBuffer() *OutputBuffer

func (*OutputBuffer) Lines

func (rw *OutputBuffer) Lines() []string

func (*OutputBuffer) Write

func (rw *OutputBuffer) Write(p []byte) (n int, err error)

type OutputStream

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

func NewOutputStream

func NewOutputStream(streamChan chan string) *OutputStream

NewOutputStream creates a new streaming output on the given channel.

func (*OutputStream) Lines

func (rw *OutputStream) Lines() <-chan string

func (*OutputStream) SetLineBufferSize

func (rw *OutputStream) SetLineBufferSize(n int)

func (*OutputStream) Write

func (rw *OutputStream) Write(p []byte) (n int, err error)

Write makes OutputStream implement the io.Writer interface.

type Sheller

type Sheller interface {
	Out(args ...string) (result string, err error)
	Combined(args ...string) (result string, err error)
	Quick(args ...string) string
}

type Status

type Status struct {
	PID      int
	Finish   bool
	ExitCode int
	Error    error
	CostTime time.Duration

	Output string // stdout + stderr
	Stdout string
	Stderr string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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