Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
Cmd is an external command.
func New ¶
Create a new Cmd.
Set the current directory to [Cmd.Dir], current environment variables to [Cmd.Env].
func (Cmd) Run ¶
Run executes the command.
If WithStdoutConsumer set, you can get the standard output of a command without waiting for the command to finish. If WithStderrConsumer set, you can get the standard error of a command without waiting for the command to finish. WithSplitFunc sets the split function for a scanner used in consumers.
Example ¶
package main
import (
"context"
"fmt"
"io"
"strings"
"github.com/berquerant/execx"
)
func main() {
cmd := execx.New("echo", "Hello, ${NAME}!")
cmd.Env.Set("NAME", "world")
r, err := cmd.Run(context.TODO())
if err != nil {
panic(err)
}
fmt.Println(strings.Join(r.ExpandedArgs, " "))
b, err := io.ReadAll(r.Stdout)
if err != nil {
panic(err)
}
fmt.Print(string(b))
}
Output: echo Hello, world! Hello, world!
type Config ¶
type Config struct {
StdoutConsumer *ConfigItem[func(Token)]
StderrConsumer *ConfigItem[func(Token)]
SplitFunc *ConfigItem[SplitFunc]
}
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
func NewConfigBuilder ¶
func NewConfigBuilder() *ConfigBuilder
func (*ConfigBuilder) Build ¶
func (s *ConfigBuilder) Build() *Config
func (*ConfigBuilder) SplitFunc ¶ added in v0.3.0
func (s *ConfigBuilder) SplitFunc(v SplitFunc) *ConfigBuilder
func (*ConfigBuilder) StderrConsumer ¶
func (s *ConfigBuilder) StderrConsumer(v func(Token)) *ConfigBuilder
func (*ConfigBuilder) StdoutConsumer ¶
func (s *ConfigBuilder) StdoutConsumer(v func(Token)) *ConfigBuilder
type ConfigItem ¶
type ConfigItem[T any] struct { // contains filtered or unexported fields }
func NewConfigItem ¶
func NewConfigItem[T any](defaultValue T) *ConfigItem[T]
func (*ConfigItem[T]) Default ¶
func (s *ConfigItem[T]) Default() T
func (*ConfigItem[T]) Get ¶
func (s *ConfigItem[T]) Get() T
func (*ConfigItem[T]) IsModified ¶
func (s *ConfigItem[T]) IsModified() bool
func (*ConfigItem[T]) Set ¶
func (s *ConfigItem[T]) Set(value T)
type Env ¶
Env represents a set of environment variables.
func EnvFromSlice ¶
EnvFromSlice creates a new Env from strings, in the form "key=value".
func (Env) ExpandStrings ¶
ExpandStrings expands environment variables in multiple targets.
type Option ¶
type Option func(*Config)
func WithSplitFunc ¶ added in v0.3.0
func WithStderrConsumer ¶
func WithStdoutConsumer ¶
type Result ¶
type Result struct {
// ExpandedArgs is actual command.
ExpandedArgs []string
Stdout io.Reader
Stderr io.Reader
}
Result is Cmd execution result.
type Script ¶
type Script struct {
// Shell executes Content.
Shell []string
// Content is a set of commands.
Content string
Env Env
// If KeepScriptFile is true, then do not regenerate script files,
// and not reflect changes in Content and Env when calling Runner.
KeepScriptFile bool
// contains filtered or unexported fields
}
Script is an executable script, set of commands.
func (*Script) Runner ¶
Runner creates a new Cmd and pass it to f.
Example ¶
package main
import (
"bytes"
"context"
"fmt"
"github.com/berquerant/execx"
)
func main() {
script := execx.NewScript(
`echo line1
echo ${line2}
cat -
echo line3 > /dev/stderr`,
"sh",
)
script.Env.Set("line2", "LINE2")
if err := script.Runner(func(cmd *execx.Cmd) error {
cmd.Stdin = bytes.NewBufferString("from stdin\n")
_, err := cmd.Run(
context.TODO(),
execx.WithStdoutConsumer(func(x execx.Token) {
fmt.Printf("1:%s\n", x)
}),
execx.WithStderrConsumer(func(x execx.Token) {
fmt.Printf("2:%s\n", x)
}),
)
return err
}); err != nil {
panic(err)
}
}
Output: 1:line1 1:LINE2 1:from stdin 2:line3