File: halfpipe.go

package info (click to toggle)
miller 6.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,928 kB
  • sloc: ruby: 162; sh: 119; makefile: 87
file content (94 lines) | stat: -rw-r--r-- 2,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package lib

import (
	"fmt"
	"os"

	"github.com/johnkerl/miller/v6/pkg/platform"
)

// OpenOutboundHalfPipe returns a handle to a process. Writing to that handle
// writes to the process' stdin. The process' stdout and stderr are the current
// process' stdout and stderr.
//
// This is for pipe-output-redirection in the Miller put/filter DSL.
//
// Note I am not using os.exec.Cmd which is billed as being simpler than using
// os.StartProcess. It may indeed be simpler when you want to handle the
// subprocess' stdin/stdout/stderr all three within the parent process.  Here I
// found it much easier to use os.StartProcess to let the stdout/stderr run
// free.

func OpenOutboundHalfPipe(commandString string) (*os.File, error) {
	readPipe, writePipe, err := os.Pipe()
	if err != nil {
		return nil, err
	}

	var procAttr os.ProcAttr
	procAttr.Files = []*os.File{
		readPipe,
		os.Stdout,
		os.Stderr,
	}

	// /bin/sh -c "..." or cmd /c "..."
	shellRunArray := platform.GetShellRunArray(commandString)

	process, err := os.StartProcess(shellRunArray[0], shellRunArray, &procAttr)
	if err != nil {
		return nil, err
	}

	go process.Wait()

	return writePipe, nil
}

// OpenInboundHalfPipe returns a handle to a process. Reading from that handle
// reads from the process' stdout. The process' stdin and stderr are the
// current process' stdin and stderr.
//
// This is for the Miller prepipe feature.
//
// Note I am not using os.exec.Cmd which is billed as being simpler than using
// os.StartProcess. It may indeed be simpler when you want to handle the
// subprocess' stdin/stdout/stderr all three within the parent process.  Here I
// found it much easier to use os.StartProcess to let the stdin/stderr run
// free.

func OpenInboundHalfPipe(commandString string) (*os.File, error) {
	readPipe, writePipe, err := os.Pipe()
	if err != nil {
		return nil, err
	}

	var procAttr os.ProcAttr
	procAttr.Files = []*os.File{
		os.Stdin,
		writePipe,
		os.Stderr,
	}

	// /bin/sh -c "..." or cmd /c "..."
	shellRunArray := platform.GetShellRunArray(commandString)

	process, err := os.StartProcess(shellRunArray[0], shellRunArray, &procAttr)
	if err != nil {
		return nil, err
	}

	// TODO comment somewhere
	// https://stackoverflow.com/questions/47486128/why-does-io-pipe-continue-to-block-even-when-eof-is-reached

	// TODO comment
	go func(process *os.Process, readPipe *os.File) {
		_, err := process.Wait()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", "mlr", err)
		}
		readPipe.Close()
	}(process, readPipe)

	return readPipe, nil
}