Skip to content

Latest commit

 

History

History
 
 

run

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Run

Go Reference

Provides a wrapper around os/exec with method chaining for modifying behaviour.

Import: github.com/DavidGamba/dgtools/run

Examples

Run command and only return Stdout
	out, err := run.CMD("./command", "arg1", "arg2").STDOutOutput()
Run command and combine Stderr and Stdout
	out, err := run.CMD("./command", "arg1", "arg2").CombinedOutput()
Run command and change Working Directory
	out, err := run.CMD("./command", "arg1", "arg2").Dir("..").CombinedOutput()
Run command and set environment variables
	out, err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").CombinedOutput()
Run command and log the command that is going to be executed to os.Stderr
	out, err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").Log().CombinedOutput()
Run command and override the default Logger
	run.Logger = log.New(os.Stderr, "", log.LstdFlags)
	out, err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").Log().CombinedOutput()
Run command without trapping its output
	err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").Log().Run()
Run command interactively by tying Stdin
	err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").Log().Stdin().Run()
Pass data ([]byte) directly to the Stdin of the command
	err := run.CMD("./command", "arg1", "arg2").Dir("..").Env("DEBUG=true").Log().In(data).CombinedOutput()
Run a command with a cancelation context
	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
	defer cancel()
	out, err := run.CMD("./command", "arg1", "arg2").Ctx(ctx).CombinedOutput()
Run a command and pass a custom io.Writer to run:
	// Defaults to os.Stdout and os.Stderr
	err := run.CMD("./command", "arg1", "arg2").Run()

	// Combined output to myCombinedWriter
	err := run.CMD("./command", "arg1", "arg2").Run(myCombinedWriter)

	// Separate streams
	err := run.CMD("./command", "arg1", "arg2").Run(myStdoutWriter, myStderrWriter)
Run command and only return Stdout but print Stderr to os.Stderr as it happends
	out, err := run.CMD("./command", "arg1", "arg2").STDOutOutput()
Run command and only return Stdout but discard Stderr for quiet mode.
	out, err := run.CMD("./command", "arg1", "arg2").DiscardErr().STDOutOutput()
Run command and only return Stdout but save Stderr output to the error object if there was an error
	out, err := run.CMD("./command", "arg1", "arg2").SaveErr().STDOutOutput()
	if err != nil {
		var exitErr *exec.ExitError
		if errors.As(err, &exitErr) {
			errOutput := exitErr.Stderr
			log.Printf("Failed with exit code: %d, full error output: %s\n", exitErr.ExitCode(), string(errOutput))

LICENSE

This file is part of run.

Copyright © 2020-2021 David Gamba Rios

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.