cmdr

package
v0.6.17 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 16 Imported by: 4

Documentation

Overview

Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstLine

func FirstLine(output string) string

FirstLine from command output

func OutputLines

func OutputLines(output string) []string

OutputLines split output to lines

func PrintCmdline added in v0.6.0

func PrintCmdline(c *Cmd)

PrintCmdline on before exec

Types

type Cmd

type Cmd struct {
	*exec.Cmd
	// Name of the command
	Name string
	// DryRun if True, not real execute command
	DryRun bool
	// Vars mapping
	Vars map[string]string

	// BeforeRun hook
	BeforeRun func(c *Cmd)
	// AfterRun hook
	AfterRun func(c *Cmd, err error)
}

Cmd struct

func CmdWithCtx added in v0.6.0

func CmdWithCtx(ctx context.Context, bin string, args ...string) *Cmd

CmdWithCtx create new instance with context.

see exec.CommandContext

func NewCmd

func NewCmd(bin string, args ...string) *Cmd

NewCmd instance

see exec.Command

func NewCmdline added in v0.6.7

func NewCmdline(line string) *Cmd

NewCmdline instance

see exec.Command

func NewGitCmd added in v0.6.0

func NewGitCmd(subCmd string, args ...string) *Cmd

NewGitCmd instance

func WrapGoCmd

func WrapGoCmd(cmd *exec.Cmd) *Cmd

WrapGoCmd instance

func (*Cmd) AddArg

func (c *Cmd) AddArg(args ...string) *Cmd

AddArg add args and returns the current object

func (*Cmd) AddArgf

func (c *Cmd) AddArgf(format string, args ...any) *Cmd

AddArgf add args and returns the current object. alias of the WithArgf()

func (*Cmd) AddArgs

func (c *Cmd) AddArgs(args []string) *Cmd

AddArgs for the git. alias of WithArgs()

func (*Cmd) AppendEnv added in v0.6.7

func (c *Cmd) AppendEnv(mp map[string]string) *Cmd

AppendEnv to the os ENV for run command

func (*Cmd) ArgIf

func (c *Cmd) ArgIf(arg string, exprOk bool) *Cmd

ArgIf add arg and returns the current object

func (*Cmd) BinName added in v0.6.0

func (c *Cmd) BinName() string

BinName of the command

func (*Cmd) BinOrPath added in v0.6.0

func (c *Cmd) BinOrPath() string

BinOrPath of the command

func (*Cmd) Cmdline

func (c *Cmd) Cmdline() string

Cmdline to command line

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() (string, error)

CombinedOutput run and return output, will combine stderr and stdout output

func (*Cmd) Config added in v0.6.0

func (c *Cmd) Config(fn func(c *Cmd)) *Cmd

Config the command

func (*Cmd) Copy added in v0.6.0

func (c *Cmd) Copy(args ...string) *Cmd

Copy new instance from current command, with new args.

func (*Cmd) FlushRun

func (c *Cmd) FlushRun() error

FlushRun runs command and flush output to stdout

func (*Cmd) GoCmd

func (c *Cmd) GoCmd() *exec.Cmd

GoCmd get exec.Cmd

func (*Cmd) HasStdout added in v0.6.2

func (c *Cmd) HasStdout() bool

HasStdout output setting.

func (*Cmd) IDString

func (c *Cmd) IDString() string

IDString of the command

func (*Cmd) MustRun

func (c *Cmd) MustRun()

MustRun a command. will panic on error

func (*Cmd) OnAfter

func (c *Cmd) OnAfter(fn func(c *Cmd, err error)) *Cmd

OnAfter exec add hook

func (*Cmd) OnBefore

func (c *Cmd) OnBefore(fn func(c *Cmd)) *Cmd

OnBefore exec add hook

func (*Cmd) OnlyArgs added in v0.6.0

func (c *Cmd) OnlyArgs() (ss []string)

OnlyArgs of the command, not contains bin name.

func (*Cmd) Output

func (c *Cmd) Output() (string, error)

Output run and return output

func (*Cmd) OutputLines

func (c *Cmd) OutputLines() ([]string, error)

OutputLines run and return output as lines

func (*Cmd) OutputToOS added in v0.6.2

func (c *Cmd) OutputToOS() *Cmd

OutputToOS output to OS stdout and error

func (*Cmd) PrintCmdline added in v0.6.6

func (c *Cmd) PrintCmdline() *Cmd

PrintCmdline on exec command

func (*Cmd) ResetArgs

func (c *Cmd) ResetArgs()

ResetArgs for command, but will keep bin name.

func (*Cmd) Run

func (c *Cmd) Run() error

Run runs command

func (*Cmd) SafeLines

func (c *Cmd) SafeLines() []string

SafeLines run and return output as lines

func (*Cmd) SafeOutput

func (c *Cmd) SafeOutput() string

SafeOutput run and return output

func (*Cmd) SetVar added in v0.6.9

func (c *Cmd) SetVar(name, val string) *Cmd

SetVar add var and returns the current object

func (*Cmd) Success

func (c *Cmd) Success() bool

Success run and return whether success

func (*Cmd) ToOSStdout added in v0.6.2

func (c *Cmd) ToOSStdout() *Cmd

ToOSStdout output to OS stdout

func (*Cmd) ToOSStdoutStderr added in v0.6.2

func (c *Cmd) ToOSStdoutStderr() *Cmd

ToOSStdoutStderr output to OS stdout and error

func (*Cmd) WithAnyArgs added in v0.6.0

func (c *Cmd) WithAnyArgs(args ...any) *Cmd

WithAnyArgs add args and returns the current object.

func (*Cmd) WithArg

func (c *Cmd) WithArg(args ...string) *Cmd

WithArg add args and returns the current object. alias of the WithArg()

func (*Cmd) WithArgIf

func (c *Cmd) WithArgIf(arg string, exprOk bool) *Cmd

WithArgIf add arg and returns the current object

func (*Cmd) WithArgf

func (c *Cmd) WithArgf(format string, args ...any) *Cmd

WithArgf add arg and returns the current object

func (*Cmd) WithArgs

func (c *Cmd) WithArgs(args []string) *Cmd

WithArgs for the git

func (*Cmd) WithArgsIf

func (c *Cmd) WithArgsIf(args []string, exprOk bool) *Cmd

WithArgsIf add arg and returns the current object

func (*Cmd) WithBin added in v0.6.0

func (c *Cmd) WithBin(name string) *Cmd

WithBin name returns the current object

func (*Cmd) WithDryRun added in v0.6.6

func (c *Cmd) WithDryRun(dryRun bool) *Cmd

WithDryRun on exec command

func (*Cmd) WithEnvMap added in v0.6.7

func (c *Cmd) WithEnvMap(mp map[string]string) *Cmd

WithEnvMap override set new ENV for run

func (*Cmd) WithGoCmd added in v0.6.0

func (c *Cmd) WithGoCmd(ec *exec.Cmd) *Cmd

WithGoCmd and returns the current instance.

func (*Cmd) WithOutput

func (c *Cmd) WithOutput(out, errOut io.Writer) *Cmd

WithOutput returns the current instance

func (*Cmd) WithStdin

func (c *Cmd) WithStdin(in io.Reader) *Cmd

WithStdin returns the current argument

func (*Cmd) WithVars added in v0.6.9

func (c *Cmd) WithVars(vs map[string]string) *Cmd

WithVars add vars and returns the current object

func (*Cmd) WithWorkDir

func (c *Cmd) WithWorkDir(dir string) *Cmd

WithWorkDir returns the current object

func (*Cmd) WorkDirOnNE added in v0.6.6

func (c *Cmd) WorkDirOnNE(dir string) *Cmd

WorkDirOnNE set workdir on input is not empty

func (*Cmd) Workdir added in v0.6.7

func (c *Cmd) Workdir() string

Workdir of the command

type Runner

type Runner struct {

	// Errs on run tasks, key is Task.ID
	Errs errorx.ErrMap

	// Workdir common workdir
	Workdir string
	// EnvMap will append to task.Cmd on run
	EnvMap map[string]string

	// Params for add custom params
	Params maputil.Map

	// DryRun dry run all commands
	DryRun bool
	// OutToStd stdout and stderr
	OutToStd bool
	// IgnoreErr continue on error
	IgnoreErr bool
	// BeforeRun hooks on each task. return false to skip current task.
	BeforeRun func(r *Runner, t *Task) bool
	// AfterRun hook on each task. return false to stop running.
	AfterRun func(r *Runner, t *Task) bool
	// contains filtered or unexported fields
}

Runner use for batch run multi task commands

func NewRunner

func NewRunner(fns ...func(rr *Runner)) *Runner

NewRunner instance with config func

func (*Runner) Add

func (r *Runner) Add(tasks ...*Task) *Runner

Add multitask at once

func (*Runner) AddCmd

func (r *Runner) AddCmd(cmds ...*Cmd) *Runner

AddCmd commands

func (*Runner) AddCmdline added in v0.6.6

func (r *Runner) AddCmdline(line string) *Runner

AddCmdline as a command task

func (*Runner) AddTask

func (r *Runner) AddTask(task *Task) *Runner

AddTask add one task

func (*Runner) CmdWithAnys added in v0.6.0

func (r *Runner) CmdWithAnys(cmdName string, args ...any) *Runner

CmdWithAnys a command task

func (*Runner) CmdWithArgs added in v0.6.0

func (r *Runner) CmdWithArgs(cmdName string, args ...string) *Runner

CmdWithArgs a command task

func (*Runner) GitCmd added in v0.6.0

func (r *Runner) GitCmd(subCmd string, args ...string) *Runner

GitCmd quick a git command task

func (*Runner) Len added in v0.6.0

func (r *Runner) Len() int

Len of tasks

func (*Runner) Prev

func (r *Runner) Prev() *Task

Prev task instance after running

func (*Runner) Reset added in v0.6.9

func (r *Runner) Reset() *Runner

Reset instance

func (*Runner) Run

func (r *Runner) Run() error

Run all tasks

func (*Runner) RunTask added in v0.6.0

func (r *Runner) RunTask(task *Task) (goon bool)

RunTask command

func (*Runner) StepRun added in v0.6.8

func (r *Runner) StepRun() error

StepRun one command

func (*Runner) Task

func (r *Runner) Task(id string) (*Task, error)

Task get by id name

func (*Runner) TaskIDs

func (r *Runner) TaskIDs() []string

TaskIDs get

func (*Runner) WithOutToStd added in v0.6.8

func (r *Runner) WithOutToStd() *Runner

WithOutToStd set

type RunnerHookFn added in v0.6.8

type RunnerHookFn func(r *Runner, t *Task) bool

RunnerHookFn func

type Task

type Task struct {

	// ID for task
	ID  string
	Cmd *Cmd

	// BeforeRun hook
	BeforeRun func(t *Task)
	PrevCond  func(prev *Task) bool
	// contains filtered or unexported fields
}

Task struct

func NewTask added in v0.6.0

func NewTask(cmd *Cmd) *Task

NewTask instance

func (*Task) Cmdline added in v0.6.0

func (t *Task) Cmdline() string

Cmdline get

func (*Task) Err

func (t *Task) Err() error

Err get

func (*Task) Index added in v0.6.0

func (t *Task) Index() int

Index get

func (*Task) IsSuccess added in v0.6.0

func (t *Task) IsSuccess() bool

IsSuccess of task

func (*Task) Run added in v0.6.0

func (t *Task) Run() error

Run command

func (*Task) RunWith added in v0.6.8

func (t *Task) RunWith(ctx maputil.Data) error

RunWith command

Jump to

Keyboard shortcuts

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