Skip to content

Commit

Permalink
reformat cmdline.CommandRealtimeStdout
Browse files Browse the repository at this point in the history
  • Loading branch information
qwenode committed Apr 11, 2024
1 parent 7c7c9ba commit f6a3684
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
41 changes: 37 additions & 4 deletions cmdline/cmd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cmdline

import (
"bufio"
"github.com/qwenode/gogo/sanitize"
"io/ioutil"
"os/exec"
)

//windows command: cmdline.exe /c "command"
//linux command: /bin/bash -c "command"
//TODO add CommandRealtimeStdoutTimeout run with timeout
//TODO add CommandFnTimeout run with timeout
// windows command: cmdline.exe /c "command"
// linux command: /bin/bash -c "command"
// TODO add CommandRealtimeStdoutTimeout run with timeout
// TODO add CommandFnTimeout run with timeout
// commandFunc call by CommandFn
type commandFunc func(output string, errCode int) bool

Expand All @@ -20,3 +22,34 @@ func CommandFn(fn commandFunc, name string, arg ...string) bool {
}
return fn(string(output), 0)
}

// commandStdoutFunc call by CommandRealtimeStdout
type commandStdoutFunc func(output string, errCode int, done bool) bool

// CommandRealtimeStdout run exec.command(name,arg...) and realtime output
func CommandRealtimeStdout(fn commandStdoutFunc, name string, arg ...string) bool {
command := exec.Command(name, arg...)
pipe, _ := command.StdoutPipe()
stderrPipe, _ := command.StderrPipe()
defer pipe.Close()
defer stderrPipe.Close()
if err := command.Start(); err != nil {
all, _ := ioutil.ReadAll(stderrPipe)
return fn(string(all), sanitize.Int(string(all)), true)
}
reader := bufio.NewReader(pipe)
r := true
for {
readString, err := reader.ReadString('\n')
if err != nil {
all, _ := ioutil.ReadAll(stderrPipe)
r = fn(string(all), sanitize.Int(string(all)), true)
break
}
r = fn(readString, 0, false)
if r == false {
break
}
}
return r
}
38 changes: 0 additions & 38 deletions cmdline/cmd_linux.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
package cmdline

import (
"bufio"
"github.com/qwenode/gogo/sanitize"
"io/ioutil"
"os/exec"
)

// commandStdoutFunc call by CommandRealtimeStdout
type commandStdoutFunc func(output string, errCode int, done bool) bool

// CommandRealtimeStdout run exec.command(name,arg...) and realtime output
func CommandRealtimeStdout(fn commandStdoutFunc, name string, arg ...string) bool {
command := exec.Command(name, arg...)
pipe, _ := command.StdoutPipe()
stderrPipe, _ := command.StderrPipe()
defer pipe.Close()
defer stderrPipe.Close()
if err := command.Start(); err != nil {
all, _ := ioutil.ReadAll(stderrPipe)
return fn(string(all), sanitize.Int(string(all)), true)
}
reader := bufio.NewReader(pipe)
r := true
for {
readString, err := reader.ReadString('\n')
if err != nil {
all, _ := ioutil.ReadAll(stderrPipe)
r = fn(string(all), sanitize.Int(string(all)), true)
break
}
r = fn(readString, 0, false)
if r == false {
break
}
}
return r
}

// CommandLineRealtimeStdout run exec.command(name,arg...) and realtime output ,just one command,actually exec /bin/bash -c "your command"
func CommandLineRealtimeStdout(fn commandStdoutFunc, c string) bool {
return CommandRealtimeStdout(fn, "/bin/bash", "-c", c)
Expand Down

0 comments on commit f6a3684

Please sign in to comment.