executil

package
v0.0.0-...-052fa94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 2

README

Package cloudeng.io/os/executil

import cloudeng.io/os/executil

Package executil provides utilities for working with os/exec.

Functions

Func ExecName
func ExecName(path string) string

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 Getenv
func Getenv(env []string, key string) (string, bool)

Getenv retrieves the value of an environment variable from the provided slice.

Func GoBuild
func GoBuild(ctx context.Context, binary string, args ...string) (string, error)
Func IsStopped
func IsStopped(pid int) bool

IsStopped returns true if the process with the specified pid has stopped or does not exist. Wait must have been called on the process otherwise this function will return true on some systems since the process may still exist as a defunct process.

Func NewLineFilter
func NewLineFilter(forward io.Writer, ch chan<- []byte, res ...*regexp.Regexp) io.WriteCloser

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. If no regexps are supplied, all lines are sent. Close must be called on the returned io.WriteCloser to ensure that all resources are reclaimed.

Func ReplaceEnvVar
func ReplaceEnvVar(env []string, key, value string) []string

ReplaceEnvVar replaces the value of an environment variable in the provided slice. If the variable does not exist, it is added to the slice.

Func SignalAndWait
func SignalAndWait(ctx context.Context, perSignalOrWait time.Duration, cmd *exec.Cmd, sigs ...os.Signal) error

SignalAndWait provides a convenience function to signal a process to terminate by sending it one or more signals and waiting for it to terminate but with a timeout on calling Wait and on waiting for the process to stop after each signal. The perSignalOrWait duration is used as the timeout for both calling Wait and for waiting for the process to stop after each signal, hence the total time spent waiting may be up to len(sigs)+1 times perSignalOrWait. If the process stops after any signal, SignalAndWait returns immediately.

Func WaitForStopped
func WaitForStopped(ctx context.Context, pid int, waitFor time.Duration) error

WaitForStopped waits for the process with the specified pid to stop within the specified duration. It assumes that Wait has been called.

Examples

Example
TODO
  • cnicolaou: make sure all goroutines shutdown.

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, ch, regexp.MustCompile("filter me:"))
	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

func ExecName(path string) string

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 Getenv

func Getenv(env []string, key string) (string, bool)

Getenv retrieves the value of an environment variable from the provided slice.

func GoBuild

func GoBuild(ctx context.Context, binary string, args ...string) (string, error)

func IsStopped

func IsStopped(pid int) bool

IsStopped returns true if the process with the specified pid has stopped or does not exist. Wait must have been called on the process otherwise this function will return true on some systems since the process may still exist as a defunct process.

func NewLineFilter

func NewLineFilter(forward io.Writer, ch chan<- []byte, res ...*regexp.Regexp) io.WriteCloser

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. If no regexps are supplied, all lines are sent. Close must be called on the returned io.WriteCloser to ensure that all resources are reclaimed.

func ReplaceEnvVar

func ReplaceEnvVar(env []string, key, value string) []string

ReplaceEnvVar replaces the value of an environment variable in the provided slice. If the variable does not exist, it is added to the slice.

func SignalAndWait

func SignalAndWait(ctx context.Context, perSignalOrWait time.Duration, cmd *exec.Cmd, sigs ...os.Signal) error

SignalAndWait provides a convenience function to signal a process to terminate by sending it one or more signals and waiting for it to terminate but with a timeout on calling Wait and on waiting for the process to stop after each signal. The perSignalOrWait duration is used as the timeout for both calling Wait and for waiting for the process to stop after each signal, hence the total time spent waiting may be up to len(sigs)+1 times perSignalOrWait. If the process stops after any signal, SignalAndWait returns immediately.

func WaitForStopped

func WaitForStopped(ctx context.Context, pid int, waitFor time.Duration) error

WaitForStopped waits for the process with the specified pid to stop within the specified duration. It assumes that Wait has been called.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL