Documentation ¶
Overview ¶
Package executil provides utilities for working with os/exec.
Example ¶
package main import ( "bytes" "context" "fmt" "os" "os/exec" "path/filepath" "regexp" "runtime" "sync" "time" "cloudeng.io/os/executil" ) func main() { go func() { time.Sleep(time.Second * 5) buf := make([]byte, 1024*1024) n := runtime.Stack(buf, true) fmt.Fprintf(os.Stderr, "%s\n", string(buf[:n])) os.Exit(1) }() ctx := context.Background() all := &bytes.Buffer{} // Use go run testdata/cat.go for compatibility across windows and unix. cmd := exec.CommandContext(ctx, "go", "run", filepath.Join("testdata", "cat.go"), filepath.Join("testdata", "example")) // #nosec G204 ch := make(chan []byte, 1) filter := executil.NewLineFilter(all, regexp.MustCompile("filter me:"), ch) cmd.Stdout = filter var wg sync.WaitGroup wg.Add(1) go func() { if err := cmd.Start(); err != nil { panic(err) } wg.Done() }() buf := <-ch fmt.Println("filtered output") fmt.Println(string(buf)) fmt.Println("all of the output") wg.Wait() if err := filter.Close(); err != nil { fmt.Println(err) } fmt.Println(all.String()) }
Output: filtered output filter me: 33 all of the output some words some more words filter me: 33 and again more words
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecName ¶
ExecName returns path in a form suitable for use as an executable. For unix systems the path is unchanged. For windows a '.exe' suffix is added if not already present.
func NewLineFilter ¶
NewLineFilter returns an io.WriteCloser that scans the contents of the supplied io.Writer and sends lines that match the regexp to the supplied channel. It can be used to filter the output of a command started by the exec package for example for specific output. Call Close on the returned io.WriteCloser to ensure that all resources are reclaimed.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.