Skip to content

Commit

Permalink
👔 up(sys): update and add some cmdr build methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 23, 2023
1 parent e500a04 commit 0cd4fda
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
4 changes: 4 additions & 0 deletions fsutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"

"github.com/gookit/goutil/internal/comfunc"
)
Expand Down Expand Up @@ -46,6 +47,9 @@ func ExpandPath(pathStr string) string {

// Realpath returns the shortest path name equivalent to path by purely lexical processing.
func Realpath(pathStr string) string {
if runtime.GOOS == "windows" {
return filepath.Clean(pathStr)
}
return path.Clean(pathStr)
}

Expand Down
52 changes: 35 additions & 17 deletions sysutil/cmdr/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os/exec"
"path/filepath"

"github.com/gookit/color"
"github.com/gookit/goutil"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/internal/comfunc"
Expand All @@ -19,7 +18,8 @@ type Cmd struct {
*exec.Cmd
// Name of the command
Name string
// inited bool
// DryRun if True, not real execute command
DryRun bool

// BeforeRun hook
BeforeRun func(c *Cmd)
Expand Down Expand Up @@ -55,11 +55,6 @@ func CmdWithCtx(ctx context.Context, bin string, args ...string) *Cmd {
}
}

// PrintCmdline on before exec
func PrintCmdline(c *Cmd) {
color.Yellowln(">", c.Cmdline())
}

// -------------------------------------------------
// config the command
// -------------------------------------------------
Expand All @@ -70,6 +65,18 @@ func (c *Cmd) Config(fn func(c *Cmd)) *Cmd {
return c
}

// WithDryRun on exec command
func (c *Cmd) WithDryRun(dryRun bool) *Cmd {
c.DryRun = dryRun
return c
}

// PrintCmdline on exec command
func (c *Cmd) PrintCmdline() *Cmd {
c.BeforeRun = PrintCmdline
return c
}

// OnBefore exec add hook
func (c *Cmd) OnBefore(fn func(c *Cmd)) *Cmd {
c.BeforeRun = fn
Expand Down Expand Up @@ -99,7 +106,7 @@ func (c *Cmd) lookPath(name string) {
c.Path = lp
}
if err != nil {
goutil.Panicf("look %q path error: %s", name, err.Error())
goutil.Panicf("cmdr: look %q path error: %v", name, err)
}
}
}
Expand All @@ -116,8 +123,8 @@ func (c *Cmd) WithWorkDir(dir string) *Cmd {
return c
}

// WorkDirOnNot set, returns the current object
func (c *Cmd) WorkDirOnNot(dir string) *Cmd {
// WorkDirOnNE set workdir on input is not empty
func (c *Cmd) WorkDirOnNE(dir string) *Cmd {
if c.Dir == "" {
c.Dir = dir
}
Expand Down Expand Up @@ -327,6 +334,10 @@ func (c *Cmd) Output() (string, error) {
c.BeforeRun(c)
}

if c.DryRun {
return "DRY-RUN: ok", nil
}

output, err := c.Cmd.Output()

if c.AfterRun != nil {
Expand All @@ -341,6 +352,10 @@ func (c *Cmd) CombinedOutput() (string, error) {
c.BeforeRun(c)
}

if c.DryRun {
return "DRY-RUN: ok", nil
}

output, err := c.Cmd.CombinedOutput()

if c.AfterRun != nil {
Expand All @@ -358,8 +373,7 @@ func (c *Cmd) MustRun() {

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

// Run runs command
Expand All @@ -368,20 +382,24 @@ func (c *Cmd) Run() error {
c.BeforeRun(c)
}

if c.DryRun {
return nil
}

// do running
err := c.Cmd.Run()

if c.AfterRun != nil {
c.AfterRun(c, err)
}
return err

// if IsWindows() {
// return c.Spawn()
// }
// return c.Exec()
}

// if IsWindows() {
// return c.Spawn()
// }
// return c.Exec()

// Spawn runs command with spawn(3)
// func (c *Cmd) Spawn() error {
// return c.Cmd.Run()
Expand Down
11 changes: 10 additions & 1 deletion sysutil/cmdr/cmdr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks
package cmdr

import "strings"
import (
"strings"

"github.com/gookit/color"
)

// PrintCmdline on before exec
func PrintCmdline(c *Cmd) {
color.Yellowln(">", c.Cmdline())
}

// OutputLines split output to lines
func OutputLines(output string) []string {
Expand Down
9 changes: 9 additions & 0 deletions sysutil/sysenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ func HasExecutable(binName string) bool {
return err == nil
}

// Getenv get ENV value by key name, can with default value
func Getenv(name string, def ...string) string {
val := os.Getenv(name)
if val == "" && len(def) > 0 {
val = def[0]
}
return val
}

// Environ like os.Environ, but will returns key-value map[string]string data.
func Environ() map[string]string {
return comfunc.Environ()
Expand Down

0 comments on commit 0cd4fda

Please sign in to comment.