ExecuteCommandWithOutputToStdout

This commit is contained in:
yo 2022-11-20 20:16:23 +01:00
parent 561ae4386a
commit 9c18a83ee8

View File

@ -225,6 +225,78 @@ func executeCommand(cmdline string) (string, error) {
return string(out), err
}
// Executed command outputs to stdout in realtime
func executeCommandWithOutputToStdout(cmdline string) (error) {
var cmd []string
var err error
if gUseSudo {
cmd = append(cmd, "sudo")
}
var word string
var in_escaped bool
// Split by words, or " enclosed words
for i, c := range (cmdline) {
if string(c) == "\"" {
if in_escaped {
// This is the closing "
cmd = append(cmd, word)
in_escaped = false
} else {
in_escaped = true
}
continue
}
if string(c) == " " {
if in_escaped {
word = word + string(c)
continue
} else {
cmd = append(cmd, word)
word = ""
continue
}
}
if i == (len(cmdline) - 1) {
word = word + string(c)
cmd = append(cmd, word)
break
}
// else
word = word + string(c)
}
var execHandle *exec.Cmd
if len(cmd) > 1 {
execHandle = exec.Command(cmd[0], cmd[1:]...)
} else {
execHandle = exec.Command(cmd[0])
}
stdout, err := execHandle.StdoutPipe()
if err != nil {
return err
}
execHandle.Start()
buf := bufio.NewReader(stdout)
for {
line, _, err := buf.ReadLine()
if err != nil {
if err.Error() == "EOF" {
return nil
} else {
return err
}
}
fmt.Println(string(line))
}
return fmt.Errorf("Unknown error: you shouldn't be here!\n")
}
func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
var cmd []string
@ -677,7 +749,7 @@ func setJailConfigUpdated(jail *Jail) error {
return err
}
j.ConfigUpdated = true
return nil
}