Skip to content

Commit

Permalink
✨ feat(cmdr): support set workdir and env-map on run command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 2, 2023
1 parent fc98d06 commit 7460c0f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions comdef/consts.go
Expand Up @@ -23,3 +23,5 @@ const (

// NoIdx invalid index or length
const NoIdx = -1

// const VarPathReg = `(\w[\w-]*(?:\.[\w-]+)*)`
44 changes: 43 additions & 1 deletion sysutil/cmdr/cmd.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gookit/goutil"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/internal/comfunc"
)

Expand Down Expand Up @@ -37,6 +38,15 @@ func NewGitCmd(subCmd string, args ...string) *Cmd {
return NewCmd("git", subCmd).AddArgs(args)
}

// NewCmdline instance
//
// see exec.Command
func NewCmdline(line string) *Cmd {
bin, args := cmdline.NewParser(line).WithParseEnv().BinAndArgs()

return NewCmd(bin, args...)
}

// NewCmd instance
//
// see exec.Command
Expand Down Expand Up @@ -125,12 +135,39 @@ func (c *Cmd) WithWorkDir(dir string) *Cmd {

// WorkDirOnNE set workdir on input is not empty
func (c *Cmd) WorkDirOnNE(dir string) *Cmd {
if c.Dir == "" {
if dir == "" {
c.Dir = dir
}
return c
}

// WithEnvMap override set new ENV for run
func (c *Cmd) WithEnvMap(mp map[string]string) *Cmd {
if ln := len(mp); ln > 0 {
c.Env = make([]string, 0, ln)
for key, val := range mp {
c.Env = append(c.Env, key+"="+val)
}
}
return c
}

// AppendEnv to the os ENV for run command
func (c *Cmd) AppendEnv(mp map[string]string) *Cmd {
if len(mp) > 0 {
// init env data
if c.Env == nil {
c.Env = os.Environ()
}

for name, val := range mp {
c.Env = append(c.Env, name+"="+val)
}
}

return c
}

// OutputToOS output to OS stdout and error
func (c *Cmd) OutputToOS() *Cmd {
return c.ToOSStdoutStderr()
Expand Down Expand Up @@ -268,6 +305,11 @@ func (c *Cmd) ResetArgs() {
}
}

// Workdir of the command
func (c *Cmd) Workdir() string {
return c.Dir
}

// Cmdline to command line
func (c *Cmd) Cmdline() string {
return comfunc.Cmdline(c.Args)
Expand Down
6 changes: 5 additions & 1 deletion sysutil/cmdr/cmdr.go
Expand Up @@ -9,7 +9,11 @@ import (

// PrintCmdline on before exec
func PrintCmdline(c *Cmd) {
color.Yellowln(">", c.Cmdline())
if c.DryRun {
color.Yellowln("DRY-RUN>", c.Cmdline())
} else {
color.Yellowln(">", c.Cmdline())
}
}

// OutputLines split output to lines
Expand Down
18 changes: 15 additions & 3 deletions sysutil/cmdr/runner.go
Expand Up @@ -86,8 +86,11 @@ type Runner struct {
Errs errorx.ErrMap

// TODO Concurrent run
// common workdir
// wordDir string

// 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
Expand Down Expand Up @@ -211,14 +214,23 @@ func (r *Runner) Run() error {

// RunTask command
func (r *Runner) RunTask(task *Task) (goon bool) {
if len(r.EnvMap) > 0 {
task.Cmd.AppendEnv(r.EnvMap)
}

if r.OutToStd && !task.Cmd.HasStdout() {
task.Cmd.ToOSStdoutStderr()
}

// common workdir
if r.Workdir != "" && task.Cmd.Dir == "" {
task.Cmd.WithWorkDir(r.Workdir)
}

// do running
if err := task.Run(); err != nil {
r.Errs[task.ID] = err
color.Errorf("Task #%d run error: %s\n", task.Index()+1, err)
color.Errorf("Task#%d run error: %s\n", task.Index()+1, err)

// not ignore error, stop.
if !r.IgnoreErr {
Expand Down

0 comments on commit 7460c0f

Please sign in to comment.