diff --git a/basefn/extfunc.go b/basefn/extfunc.go index fde7dbc56..31efac180 100644 --- a/basefn/extfunc.go +++ b/basefn/extfunc.go @@ -2,7 +2,7 @@ package basefn import "fmt" -// DataSize format bytes number friendly. +// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB // // Usage: // diff --git a/mathutil/number.go b/mathutil/number.go index 64e9be873..5b1359463 100644 --- a/mathutil/number.go +++ b/mathutil/number.go @@ -25,7 +25,8 @@ func ElapsedTime(startTime time.Time) string { return fmt.Sprintf("%.3f", time.Since(startTime).Seconds()*1000) } -// DataSize format value. alias format.DataSize() +// DataSize format value to data size string. eg: 1024 => 1KB, 1024*1024 => 1MB +// alias format.DataSize() func DataSize(size uint64) string { return basefn.DataSize(size) } diff --git a/sysutil/cmdr/cmd.go b/sysutil/cmdr/cmd.go index 8ebda4415..f91a41184 100644 --- a/sysutil/cmdr/cmd.go +++ b/sysutil/cmdr/cmd.go @@ -21,6 +21,8 @@ type Cmd struct { Name string // DryRun if True, not real execute command DryRun bool + // Vars mapping + Vars map[string]string // BeforeRun hook BeforeRun func(c *Cmd) @@ -28,11 +30,6 @@ type Cmd struct { AfterRun func(c *Cmd, err error) } -// WrapGoCmd instance -func WrapGoCmd(cmd *exec.Cmd) *Cmd { - return &Cmd{Cmd: cmd} -} - // NewGitCmd instance func NewGitCmd(subCmd string, args ...string) *Cmd { return NewCmd("git", subCmd).AddArgs(args) @@ -43,7 +40,6 @@ func NewGitCmd(subCmd string, args ...string) *Cmd { // see exec.Command func NewCmdline(line string) *Cmd { bin, args := cmdline.NewParser(line).WithParseEnv().BinAndArgs() - return NewCmd(bin, args...) } @@ -51,17 +47,21 @@ func NewCmdline(line string) *Cmd { // // see exec.Command func NewCmd(bin string, args ...string) *Cmd { - return &Cmd{ - Cmd: exec.Command(bin, args...), - } + return WrapGoCmd(exec.Command(bin, args...)) } // CmdWithCtx create new instance with context. // // see exec.CommandContext func CmdWithCtx(ctx context.Context, bin string, args ...string) *Cmd { + return WrapGoCmd(exec.CommandContext(ctx, bin, args...)) +} + +// WrapGoCmd instance +func WrapGoCmd(cmd *exec.Cmd) *Cmd { return &Cmd{ - Cmd: exec.CommandContext(ctx, bin, args...), + Cmd: cmd, + Vars: make(map[string]string), } } @@ -260,6 +260,20 @@ func (c *Cmd) WithArgsIf(args []string, exprOk bool) *Cmd { return c } +// WithVars add vars and returns the current object +func (c *Cmd) WithVars(vs map[string]string) *Cmd { + if len(vs) > 0 { + c.Vars = vs + } + return c +} + +// SetVar add var and returns the current object +func (c *Cmd) SetVar(name, val string) *Cmd { + c.Vars[name] = val + return c +} + // ------------------------------------------------- // helper command // -------------------------------------------------