Skip to content

Commit

Permalink
👔 up(sys): update the Cmd, add cmd var support
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 7, 2023
1 parent 6cb3562 commit 97d73fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion basefn/extfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down
3 changes: 2 additions & 1 deletion mathutil/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 24 additions & 10 deletions sysutil/cmdr/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ 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)
// AfterRun hook
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)
Expand All @@ -43,25 +40,28 @@ 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...)
}

// NewCmd instance
//
// 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),
}
}

Expand Down Expand Up @@ -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
// -------------------------------------------------
Expand Down

0 comments on commit 97d73fa

Please sign in to comment.