Documentation ¶
Overview ¶
Package shell provides helper functions for running shell commands.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run executes a command in the default shell. It returns the exit code, output, and error (if any).
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { _, out, _ := shell.Run(context.Background(), "echo", "foo", "bar") fmt.Println(out) }
Output:
func RunWith ¶
func RunWith(ctx context.Context, opts RunOptions, command string, args ...string) (int, string, error)
RunWith executes a command with given options in the default shell. It returns the exit code, output, and error (if any).
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { opts := shell.RunOptions{ Environment: map[string]string{ "PLACEHOLDER": "foo bar", }, } _, out, _ := shell.RunWith(context.Background(), opts, "printenv", "PLACEHOLDER") fmt.Println(out) }
Output:
Types ¶
type RunOptions ¶
type RunOptions struct { // WorkingDir is the working directory for a command. WorkingDir string // Environment is a map of key-values representing environment variables for a command. Environment map[string]string }
RunOptions are optional settings for a command.
type RunnerFunc ¶
RunnerFunc is a function for running a bounded command.
func Runner ¶
func Runner(command string, args ...string) RunnerFunc
Runner binds a command to a list of arguments and returns a function. The returned function can be used for running the bounded command in the default shell.
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { echo := shell.Runner("echo", "foo", "bar") _, out, _ := echo(context.Background(), "baz") fmt.Println(out) }
Output:
func (RunnerFunc) WithArgs ¶
func (f RunnerFunc) WithArgs(args ...string) RunnerFunc
WithArgs binds more arguments to a Runner function and returns a new Runner function.
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { echo := shell.Runner("echo", "foo") echo = echo.WithArgs("bar") _, out, _ := echo(context.Background(), "baz") fmt.Println(out) }
Output:
type RunnerWithFunc ¶
RunnerWithFunc is a function for running a bounded command with given options.
func RunnerWith ¶
func RunnerWith(command string, args ...string) RunnerWithFunc
RunnerWith binds a command to a list of arguments with given options and returns a function. The returned function can be used for running the bounded command in the default shell.
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { opts := shell.RunOptions{ Environment: map[string]string{ "TOKEN": "access-token", }, } printenv := shell.RunnerWith("printenv") _, out, _ := printenv(context.Background(), opts, "TOKEN") fmt.Println(out) }
Output:
func (RunnerWithFunc) WithArgs ¶
func (f RunnerWithFunc) WithArgs(args ...string) RunnerWithFunc
WithArgs binds more arguments to a RunnerWith function and returns a new RunnerWith function.
Example ¶
package main import ( "context" "fmt" "github.com/gardenbed/basil-cli/internal/shell" ) func main() { opts := shell.RunOptions{ Environment: map[string]string{ "TOKEN": "access-token", }, } printenv := shell.RunnerWith("printenv") printenv = printenv.WithArgs("TOKEN") _, out, _ := printenv(context.Background(), opts) fmt.Println(out) }
Output: