Skip to content

Commit

Permalink
✨ feat: cmdr - add new option for output result to OS
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 1, 2023
1 parent 767ed80 commit 6aaea78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 20 additions & 3 deletions sysutil/cmdr/cmd.go
Expand Up @@ -124,13 +124,25 @@ func (c *Cmd) WorkDirOnNot(dir string) *Cmd {
return c
}

// OutputToStd output to OS stdout and error
func (c *Cmd) OutputToStd() *Cmd {
// OutputToOS output to OS stdout and error
func (c *Cmd) OutputToOS() *Cmd {
return c.ToOSStdoutStderr()
}

// ToOSStdoutStderr output to OS stdout and error
func (c *Cmd) ToOSStdoutStderr() *Cmd {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c
}

// ToOSStdout output to OS stdout
func (c *Cmd) ToOSStdout() *Cmd {
c.Stdout = os.Stdout
c.Stderr = os.Stdout
return c
}

// WithStdin returns the current argument
func (c *Cmd) WithStdin(in io.Reader) *Cmd {
c.Stdin = in
Expand Down Expand Up @@ -280,6 +292,11 @@ func (c *Cmd) Success() bool {
return c.Run() == nil
}

// HasStdout output setting.
func (c *Cmd) HasStdout() bool {
return c.Stdout != nil
}

// SafeLines run and return output as lines
func (c *Cmd) SafeLines() []string {
ss, _ := c.OutputLines()
Expand Down Expand Up @@ -341,7 +358,7 @@ func (c *Cmd) MustRun() {

// FlushRun runs command and flush output to stdout
func (c *Cmd) FlushRun() error {
c.OutputToStd()
c.ToOSStdoutStderr()
return c.Run()
}

Expand Down
6 changes: 6 additions & 0 deletions sysutil/cmdr/runner.go
Expand Up @@ -93,6 +93,8 @@ type Runner struct {

// 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.
Expand Down Expand Up @@ -201,6 +203,10 @@ func (r *Runner) Run() error {

// RunTask command
func (r *Runner) RunTask(task *Task) (goon bool) {
if r.OutToStd && !task.Cmd.HasStdout() {
task.Cmd.ToOSStdoutStderr()
}

// do running
if err := task.Run(); err != nil {
r.Errs[task.ID] = err
Expand Down

0 comments on commit 6aaea78

Please sign in to comment.