cmdr

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 14 Imported by: 3

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

	// 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 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) 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) 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) OutputToStd

func (c *Cmd) OutputToStd() *Cmd

OutputToStd output to OS stdout and error

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) Success

func (c *Cmd) Success() bool

Success run and return whether success

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) 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) WithWorkDir

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

WithWorkDir returns the current object

func (*Cmd) WorkDirOnNot added in v0.6.0

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

WorkDirOnNot set, returns the current object

type Runner

type Runner struct {

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

	// Params for add custom params
	Params maputil.Map

	// DryRun dry run all commands
	DryRun 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) 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) 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) Task

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

Task get by id name

func (*Runner) TaskIDs

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

TaskIDs get

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

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

Jump to

Keyboard shortcuts

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