subprocess

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2018 License: MIT Imports: 5 Imported by: 0

README

pygz/subprocess

GitHub release Software License GoDoc Build Status Build status

A Go library that returns standard output/error & exit status code data from new spawned processes.

Documentation

Overview

Package subprocess provides support for standard output/error pipe data & exit status codes with new spawned system processes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Response

type Response struct {
	StdOut   string
	StdErr   string
	ExitCode int
}

Response is a struct that is returned from the public functions in the subprocess package. It contains the following fields:

Response.StdOut - (string) standard output stream cast to a string
Response.StdErr - (string) standard error stream cast to a string
Response.ExitCode - (int) executable exit status code as an integer

func Run

func Run(executable string, args ...string) Response

Run is a public function that executes a system command and returns the standard output stream, standard error stream, and exit status code data in a returned subprocess.Response struct. Run takes the following parameters:

executable (string) - the system executable for the command call
args (...string) - one or more executable arguments as comma delimited parameters

Example:

func main() {
    response := Run("go", "--help")
    fmt.Printf("%s\n", response.StdOut)
    fmt.Printf("%s\n", response.StdErr)
    fmt.Printf("%d\n", response.ExitCode)
}

func RunShell added in v0.2.0

func RunShell(shell string, shellflag string, command ...string) Response

RunShell is a public function that executes a system command with a shell and returns the standard output stream, standard error stream, and exit status code data in a returned subprocess.Response struct. RunShell takes the following parameters:

shell (string) - path to shell, optional.  Defaults = /bin/sh on Linux, macOS; bash on Windows
shellflag (string) - flag to execute system executable from a shell executable. Default = `-c` on all platforms
command (...string) - one or more executable arguments as comma delimited parameters

Example:

func main() {
    response := RunShell("", "", "ls", "-l")
    fmt.Printf("%s\n", response.StdOut)
    fmt.Printf("%s\n", response.StdErr)
    fmt.Printf("%d\n", response.ExitCode)
    response2 := RunShell("/usr/local/bin/zsh", "-c", "ls", "-l")
    fmt.Printf("%s\n", response.StdOut)
    fmt.Printf("%s\n", response.StdErr)
    fmt.Printf("%d\n", response.ExitCode)
}

Jump to

Keyboard shortcuts

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