subprocess

package module
v0.0.0-...-a1a6de4 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: MIT Imports: 8 Imported by: 3

README

Subprocesses

Spawn subprocesses in Go.

go get -u "github.com/estebangarcia21/subprocess"

Sanitized mode

package main

import (
	"log"

	"github.com/estebangarcia21/subprocess"
)

func main() {
	s := subprocess.New("ls", subprocess.Arg("-lh"))

	if err := s.Exec(); err != nil {
		log.Fatal(err)
	}
}

Shell mode

package main

import (
	"log"

	"github.com/estebangarcia21/subprocess"
)

func main() {
	s := subprocess.New("ls -lh", subprocess.Shell)

	if err := s.Exec(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Arg adds sanitized argument to command.
	Args = func(args ...string) Option {
		return func(s *Subprocess) {
			for _, arg := range args {
				s.args = append(s.args, arg)
			}
		}
	}
	// Arg adds sanitized argument to command.
	Arg = func(arg string) Option {
		return func(s *Subprocess) {
			s.args = append(s.args, arg)
		}
	}
	// Context determines where the subprocess will be executed.
	// A relative or absolute path may be provided.
	Context = func(path string) Option {
		return func(s *Subprocess) {
			s.context = path
		}
	}
	// Silent hides all output from the subprocess.
	Silent Option = func(s *Subprocess) {
		s.hideStdout = true
		s.hideStderr = true
	}
	// HideStdout hides the stdout output of the subprocess.
	HideStdout Option = func(s *Subprocess) {
		s.hideStdout = true
	}
	// HideStderr hides the stderr output of the subprocess.
	HideStderr Option = func(s *Subprocess) {
		s.hideStderr = true
	}
	// Shell determines whether the command will directly be run in the shell
	// without parameter sanitization.
	//
	// Only applicable for darwin or linux based systems. Windows subprocesses
	// are always executed in the Powershell from the CMD prompt.
	Shell Option = func(s *Subprocess) {
		s.shell = true
	}
	CatchError Option = func(s *Subprocess) {
		s.catchError = true
	}
)

Subprocess options.

View Source
var (
	ErrUngracefulExit = errors.New("exited ungracefully with a non 0 exit code")
)

Functions

This section is empty.

Types

type Option

type Option func(s *Subprocess)

Option is a configuration argument for a subprocess.

type Subprocess

type Subprocess struct {
	// contains filtered or unexported fields
}

Subprocess represents a monitored process executed by the application.

func New

func New(cmd string, opts ...Option) *Subprocess

New creates a new Subprocess with the default exit code of 1.

func (*Subprocess) Exec

func (s *Subprocess) Exec() error

Exec starts the subprocess.

func (*Subprocess) ExecAsync

func (s *Subprocess) ExecAsync() chan error

ExecAsync starts the subprocess asynchronously.

Returns a channel that closes once the result is finished.

func (*Subprocess) ExitCode

func (s *Subprocess) ExitCode() int

ExitCode returns the exit code of the subprocess. If the process was terminated by a signal or has not finished.

func (*Subprocess) IsFinished

func (s *Subprocess) IsFinished() bool

IsFinished returns true if the process has finished.

func (*Subprocess) Stderr

func (s *Subprocess) Stderr() []byte

Stderr returns the bytes that the process has sent to stderr.

func (*Subprocess) StderrText

func (s *Subprocess) StderrText() string

StderrText returns the bytes that the process has sent to stderr. The bytes are encoded in a new string.

func (*Subprocess) Stdout

func (s *Subprocess) Stdout() []byte

Stdout returns the bytes that the process has sent to stdout.

func (*Subprocess) StdoutText

func (s *Subprocess) StdoutText() string

StdoutText returns the bytes that the process has sent to stdout. The bytes are encoded in a new string.

Jump to

Keyboard shortcuts

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