MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / runCommandWithStreaming

Function runCommandWithStreaming

internal/ui/plugins/ansible/plugin.go:2898–2947  ·  view source on GitHub ↗
(cmd *exec.Cmd, stream func(string))

Source from the content-addressed store, hash-verified

2896}
2897
2898func runCommandWithStreaming(cmd *exec.Cmd, stream func(string)) (string, error) {
2899 stdoutPipe, err := cmd.StdoutPipe()
2900 if err != nil {
2901 return "", fmt.Errorf("stdout pipe: %w", err)
2902 }
2903 stderrPipe, err := cmd.StderrPipe()
2904 if err != nil {
2905 return "", fmt.Errorf("stderr pipe: %w", err)
2906 }
2907
2908 if err := cmd.Start(); err != nil {
2909 return "", err
2910 }
2911
2912 var (
2913 wg sync.WaitGroup
2914 mu sync.Mutex
2915 b bytes.Buffer
2916 )
2917 appendLine := func(line string) {
2918 mu.Lock()
2919 defer mu.Unlock()
2920 if b.Len() > 0 {
2921 b.WriteString("\n")
2922 }
2923 b.WriteString(line)
2924 if stream != nil {
2925 stream(line)
2926 }
2927 }
2928 readPipe := func(r io.Reader) {
2929 defer wg.Done()
2930 scanner := bufio.NewScanner(r)
2931 const maxScanToken = 1024 * 1024
2932 buf := make([]byte, 0, 64*1024)
2933 scanner.Buffer(buf, maxScanToken)
2934 for scanner.Scan() {
2935 appendLine(scanner.Text())
2936 }
2937 }
2938
2939 wg.Add(2)
2940 go readPipe(stdoutPipe)
2941 go readPipe(stderrPipe)
2942
2943 waitErr := cmd.Wait()
2944 wg.Wait()
2945
2946 return b.String(), waitErr
2947}
2948
2949func ansibleLogger() interface {
2950 Debug(format string, args ...interface{})

Callers 1

executeSSHScriptFunction · 0.85

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected