{"modulePath":"github.com/commander-cli/cmd","version":"v1.6.0","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package cmd\n\n## Functions\n\n```go\nfunc CaptureStandardOutput(f func() interface{}) (string, interface{})\n```\nCaptureStandardOutput allows to capture the output which will be written to os.Stdout and os.Stderr. It returns the captured output and the return value of the called function\n\n```go\nfunc WithCustomBaseCommand(baseCommand *exec.Cmd) func(c *Command)\n```\nWithCustomBaseCommand allows the OS specific generated baseCommand to be overridden by an \\*os/exec.Cmd.\n\nExample:\n\n\tc := cmd.NewCommand(\n\t  \"echo hello\",\n\t  cmd.WithCustomBaseCommand(exec.Command(\"/bin/bash\", \"-c\")),\n\t)\n\tc.Execute()\n\n```go\nfunc WithCustomStderr(writers ...io.Writer) func(c *Command)\n```\nWithCustomStderr allows to add custom writers to stderr\n\n```go\nfunc WithCustomStdout(writers ...io.Writer) func(c *Command)\n```\nWithCustomStdout allows to add custom writers to stdout\n\n```go\nfunc WithEnvironmentVariables(env EnvVars) func(c *Command)\n```\nWithEnvironmentVariables sets environment variables for the executed command\n\n```go\nfunc WithInheritedEnvironment(env EnvVars) func(c *Command)\n```\nWithInheritedEnvironment uses the env from the current process and allow to add more variables.\n\n```go\nfunc WithStandardStreams(c *Command)\n```\nWithStandardStreams is used as an option by the NewCommand constructor function and writes the output streams to stderr and stdout of the operating system\n\nExample:\n\n\tc := cmd.NewCommand(\"echo hello\", cmd.WithStandardStreams)\n\tc.Execute()\n\n```go\nfunc WithTimeout(t time.Duration) func(c *Command)\n```\nWithTimeout sets the timeout of the command\n\nExample:\n\n\tcmd.NewCommand(\"sleep 10;\", cmd.WithTimeout(500))\n\n```go\nfunc WithUser(credential syscall.Credential) func(c *Command)\n```\nWithUser allows the command to be run as a different user.\n\nExample:\n\n\tcred := syscall.Credential{Uid: 1000, Gid: 1000}\n\tc := NewCommand(\"echo hello\", cred)\n\tc.Execute()\n\n```go\nfunc WithWorkingDir(dir string) func(c *Command)\n```\nWithWorkingDir sets the current working directory\n\n```go\nfunc WithoutTimeout(c *Command)\n```\nWithoutTimeout disables the timeout for the command\n\n## Types\n\n```go\ntype Command struct {\n\tCommand      string\n\tEnv          []string\n\tDir          string\n\tTimeout      time.Duration\n\tStderrWriter io.Writer\n\tStdoutWriter io.Writer\n\tWorkingDir   string\n\t// contains filtered or unexported fields\n}\n```\nCommand represents a single command which can be executed\n\n```go\nfunc NewCommand(cmd string, options ...func(*Command)) *Command\n```\nNewCommand creates a new command You can add option with variadic option argument Default timeout is set to 30 minutes\n\nExample:\n\n\t     c := cmd.NewCommand(\"echo hello\", function (c *Command) {\n\t\t\t    c.WorkingDir = \"/tmp\"\n\t     })\n\t     c.Execute()\n\nor you can use existing options functions\n\n\tc := cmd.NewCommand(\"echo hello\", cmd.WithStandardStreams)\n\tc.Execute()\n\n```go\nfunc (c *Command) AddEnv(key, value string)\n```\nAddEnv adds an environment variable to the command If a variable gets passed like ${VAR\\_NAME} the env variable will be read out by the current shell\n\n```go\nfunc (c *Command) Combined() string\n```\nCombined returns the combined output of stderr and stdout according to their timeline\n\n```go\nfunc (c *Command) Execute() error\n```\nExecute executes the command and writes the results into it's own instance The results can be received with the Stdout(), Stderr() and ExitCode() methods\n\n```go\nfunc (c *Command) ExecuteContext(ctx context.Context) error\n```\nExecuteContext runs Execute but with Context\n\n```go\nfunc (c *Command) Executed() bool\n```\nExecuted returns if the command was already executed\n\n```go\nfunc (c *Command) ExitCode() int\n```\nExitCode returns the exit code of the command\n\n```go\nfunc (c *Command) Stderr() string\n```\nStderr returns the output to stderr\n\n```go\nfunc (c *Command) Stdout() string\n```\nStdout returns the output to stdout\n\n```go\ntype CommandInterface interface {\n\tAddEnv(string, string)\n\tStdout() string\n\tStderr() string\n\tCombined() string\n\tExitCode() int\n\tExecuted() bool\n\tExecuteContext(context.Context) error\n\tExecute() error\n}\n```\n\n```go\ntype EnvVars map[string]string\n```\nEnvVars represents a map where the key is the name of the env variable and the value is the value of the variable\n\nExample:\n\n\tenv := map[string]string{\"ENV\": \"VALUE\"}\n\n","path":"github.com/commander-cli/cmd","name":"cmd","synopsis":"","isRedistributable":true}
