Skip to content

Commit

Permalink
stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ns2Kracy committed Aug 16, 2024
1 parent 414b26a commit ebc7ad9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
65 changes: 24 additions & 41 deletions utils/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
Expand All @@ -24,58 +25,42 @@ func ExecResultStr(cmdStr string) (string, error) {
cmds := strings.Fields(cmdStr)
cmd := exec2.Command(cmds[0], cmds[1:]...)
fmt.Printf("Executing command: %s\n", cmd.String())
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", err
}

defer stdout.Close()
if err = cmd.Start(); err != nil {
return "", err
}

fmt.Printf("Command stderr: %s\n", cmd.Stderr)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf

buf, err := io.ReadAll(stdout)
if err != nil {
if err := cmd.Start(); err != nil {
return "", err
}

return string(buf), cmd.Wait()
return buf.String(), cmd.Wait()
}

func ExecResultStrArray(cmdStr string) ([]string, error) {
cmds := strings.Fields(cmdStr)
cmd := exec2.Command(cmds[0], cmds[1:]...)

fmt.Printf("Executing command: %s\n", cmd.String())

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
defer stdout.Close()
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf

if err = cmd.Start(); err != nil {
if err := cmd.Start(); err != nil {
return nil, err
}

fmt.Printf("Command stderr: %s\n", cmd.Stderr)
var result []string
scanner := bufio.NewScanner(strings.NewReader(buf.String()))
for scanner.Scan() {
result = append(result, scanner.Text())
}

buf := []string{}
outputBuf := bufio.NewReader(stdout)
for {
output, _, err := outputBuf.ReadLine()
if err != nil {
if err.Error() != "EOF" {
fmt.Printf("Error :%s\n", err)
}
break
}
buf = append(buf, string(output))
if err := scanner.Err(); err != nil {
return nil, err
}

return buf, cmd.Wait()
return result, cmd.Wait()
}

func ExecuteScripts(scriptDirectory string) error {
Expand Down Expand Up @@ -140,25 +125,23 @@ func ExecStdin(stdinStr string, name string, args ...string) error {

fmt.Printf("Executing command: %s\n", cmd.String())

var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf

stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
defer stdin.Close()

if err := cmd.Start(); err != nil {
return err
}

fmt.Printf("Command stderr: %s\n", cmd.Stderr)

if _, err := io.WriteString(stdin, stdinStr); err != nil {
return err
}

if err := cmd.Wait(); err != nil {
if err := cmd.Start(); err != nil {
return err
}

return nil
return cmd.Wait()
}
15 changes: 14 additions & 1 deletion utils/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestCommand(t *testing.T) {
func TestSafeCommand(t *testing.T) {
tests := []struct {
name string
cmdStr string
Expand Down Expand Up @@ -60,6 +60,10 @@ func TestCommand(t *testing.T) {
name: "Test Command with new-line injection shell script",
cmdStr: "source /etc/local-storage-helper.sh\nenv",
},
{
name: "Delete SMB User",
cmdStr: "smbpasswd -x testuser",
},
}

t.Run("TestExecuteScripts", func(t *testing.T) {
Expand Down Expand Up @@ -107,3 +111,12 @@ func TestCommand(t *testing.T) {
})
}
}

func TestCommand(t *testing.T) {
output, err := ExecResultStr("smbpasswd -x testuser")
if err != nil {
t.Error(err)
} else {
t.Logf("Output: %s", output)
}
}

0 comments on commit ebc7ad9

Please sign in to comment.