cob

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 10 Imported by: 10

README

cob 🌽 COmmand Builder

Go Reference codecov gosec Go Report Card

cob is a Go package for building and running *exec.Cmd objects.

Installation

go get github.com/broothie/cob@latest

Documentation

Detailed documentation can be found at pkg.go.dev.

Usage

// Easily build `*exec.Cmd` objects:
cmd, err := cob.New(ctx, "echo",
	cob.AddArgs("Hello", "World"),
	cob.AddEnv("SHELL", "bash"),
	cob.SetStdout(os.Stdout),
)

// Or, run them and easily get `stdout` and `stderr`:
stdout, stderr, cmd, err := cob.Output(ctx, "echo", cob.AddArgs("Hello", "World"))

Here is a list of all available options:

cmd, err := cob.New(ctx, "echo",
	cob.SetArgs("Hello", "World"),         // Set args directly
	cob.AddArgs("more", "args"),           // Add to existing args
	cob.SetEnv("SHELL=bash"),              // Set env directly
	cob.AddEnv("SHELL", "bash"),           // Add to existing env
	cob.SetStdin(os.Stdin),                // Set stdin directly
	cob.AddStdins(someReader),             // Add to existing stdin
	cob.SetStdout(os.Stdout),              // Set stdout directly
	cob.AddStdouts(someWriter),            // Add to existing stdout
	cob.SetStderr(os.Stderr),              // Set stderr directly
	cob.AddStderrs(errorWriter),           // Add to existing stderr
	cob.SetDir("/tmp"),                    // Set working directory
	cob.SetExtraFiles(os.Stdin),           // Set extra files directly
	cob.AddExtraFiles(someFile),           // Add to existing extra files
	cob.SetSysProcAttr(&syscall.SysProcAttr{}),  // Set process attributes
	cob.SetWaitDelay(time.Second),         // Set timeout for Wait()
)

Why?

The *exec.Cmd API isn't terrible by any means, I just find building the actual *exec.Cmd objects to be somewhat cumbersome.

With this library we go from this:

stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)

cmd := exec.CommandContext(ctx, "command", "arg1", "arg2")
cmd.Stdin = os.Stdin
cmd.Stdout = io.MultiWriter(stdout, os.Stdout)
cmd.Stderr = stderr
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, value))

if err := cmd.Run(); err != nil {
	return err
}

to this:

stdout, stderr, cmd, err := cmd.Output(ctx, "command",
	cob.AddArgs("arg1", "arg2"),
	cob.SetStdin(os.Stdin),
	cob.AddStdouts(os.Stdout),
	cob.AddEnv(key, value),
)
if err != nil {
	return err
}

which I personally find clearer and more concise.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCommandBuild = errors.New("error building command")

ErrCommandBuild is the error returned when building a command.

Functions

func AddArgs

func AddArgs(args ...string) option.Func[*exec.Cmd]

AddArgs adds arguments to the command.

func AddEnv

func AddEnv(key, value string) option.Func[*exec.Cmd]

AddEnv adds an environment variable to the command.

func AddExtraFiles

func AddExtraFiles(extraFiles ...*os.File) option.Func[*exec.Cmd]

AddExtraFiles adds extra files to the command.

func AddStderrs

func AddStderrs(stderrs ...io.Writer) option.Func[*exec.Cmd]

AddStderrs adds standard errors to the command.

func AddStdins

func AddStdins(stdins ...io.Reader) option.Func[*exec.Cmd]

AddStdins adds standard inputs to the command.

func AddStdouts

func AddStdouts(stdouts ...io.Writer) option.Func[*exec.Cmd]

AddStdouts adds standard outputs to the command.

func New

func New(ctx context.Context, name string, options ...option.Option[*exec.Cmd]) (*exec.Cmd, error)

New builds a new command.

func Output

func Output(ctx context.Context, name string, options ...option.Option[*exec.Cmd]) (*bytes.Buffer, *bytes.Buffer, *exec.Cmd, error)

func Run

func Run(ctx context.Context, name string, options ...option.Option[*exec.Cmd]) (*exec.Cmd, error)

Run runs the command and returns the combined output.

Example
package main

import (
	"context"
	"os"

	"github.com/broothie/cob"
)

func main() {

	cob.Run(context.TODO(), "echo",
		cob.AddArgs("Hello,"),
		cob.AddArgs("World"),
		cob.AddEnv("SHELL", "bash"),
		cob.SetStdin(os.Stdin),
		cob.SetStdout(os.Stdout),
	)
}
Output:
Hello, World

func SetArgs

func SetArgs(args ...string) option.Func[*exec.Cmd]

SetArgs sets the arguments for the command.

func SetDir

func SetDir(dir string) option.Func[*exec.Cmd]

SetDir sets the working directory for the command.

func SetEnv

func SetEnv(env ...string) option.Func[*exec.Cmd]

SetEnv sets the environment variables for the command.

func SetExtraFiles

func SetExtraFiles(extraFiles ...*os.File) option.Func[*exec.Cmd]

SetExtraFiles sets the extra files for the command.

func SetStderr

func SetStderr(stderr io.Writer) option.Func[*exec.Cmd]

SetStderr sets the standard error for the command.

func SetStdin

func SetStdin(stdin io.Reader) option.Func[*exec.Cmd]

SetStdin sets the standard input for the command.

func SetStdout

func SetStdout(stdout io.Writer) option.Func[*exec.Cmd]

SetStdout sets the standard output for the command.

func SetSysProcAttr

func SetSysProcAttr(attr *syscall.SysProcAttr) option.Func[*exec.Cmd]

SetSysProcAttr sets the system process attributes for the command.

func SetWaitDelay

func SetWaitDelay(delay time.Duration) option.Func[*exec.Cmd]

SetWaitDelay sets the wait delay for the command.

func Start

func Start(ctx context.Context, name string, options ...option.Option[*exec.Cmd]) (*exec.Cmd, error)

Start starts the command and returns it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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