Documentation
¶
Index ¶
- type Cmd
- type Config
- type ConfigBuilder
- func (s *ConfigBuilder) Build() *Config
- func (s *ConfigBuilder) SplitFunc(v SplitFunc) *ConfigBuilder
- func (s *ConfigBuilder) StderrConsumer(v func(Token)) *ConfigBuilder
- func (s *ConfigBuilder) StderrWriter(v io.Writer) *ConfigBuilder
- func (s *ConfigBuilder) StdoutConsumer(v func(Token)) *ConfigBuilder
- func (s *ConfigBuilder) StdoutWriter(v io.Writer) *ConfigBuilder
- type ConfigItem
- type Env
- type ExecutableTasks
- type NullBuffer
- type Option
- type Result
- type Scanner
- type Script
- type SplitFunc
- type Task
- type Tasks
- type Token
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) Exec ¶ added in v0.10.0
Exec invokes execve(2). This ignores Cmd.Stdin.
Example ¶
package main
import (
"github.com/berquerant/execx"
)
func main() {
cmd := execx.New("echo", "Hello, ${NAME}!")
cmd.Env.Set("NAME", "world")
if err := cmd.Exec(); err != nil {
panic(err)
}
}
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, default is bufio.ScanLines. default is `[]byte("\n")`. If WithStdoutWriter set, write the standard output into it instead of [Result.Stdout]. If WithStderrWriter set, write the standard error into it instead of [Result.Stderr].
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]
StdoutWriter *ConfigItem[io.Writer]
StderrWriter *ConfigItem[io.Writer]
}
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) StderrWriter ¶ added in v0.4.0
func (s *ConfigBuilder) StderrWriter(v io.Writer) *ConfigBuilder
func (*ConfigBuilder) StdoutConsumer ¶
func (s *ConfigBuilder) StdoutConsumer(v func(Token)) *ConfigBuilder
func (*ConfigBuilder) StdoutWriter ¶ added in v0.4.0
func (s *ConfigBuilder) StdoutWriter(v io.Writer) *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 ExecutableTasks ¶ added in v0.10.0
Example ¶
package main
import (
"context"
"fmt"
"github.com/berquerant/execx"
)
func main() {
err := execx.NewExecutableTasks(
execx.NewTasks().
Add(execx.NewTask("f", `echo "f($*)"`)).
Add(execx.NewTask("g", `r="$(f "$@")"
echo "g(${r})"`)).
Add(execx.NewTask("h", `g "A" "$A"`)),
execx.EnvFromSlice([]string{"A=envA"}),
"h",
).IntoScript("sh").Runner(func(cmd *execx.Cmd) error {
_, err := cmd.Run(context.TODO(), execx.WithStdoutConsumer(func(x execx.Token) {
fmt.Println(x.String())
}))
return err
})
if err != nil {
panic(err)
}
}
Output: g(f(A envA))
func NewExecutableTasks ¶ added in v0.10.0
func NewExecutableTasks( tasks Tasks, env Env, entrypoint ...string, ) *ExecutableTasks
func (ExecutableTasks) IntoScript ¶ added in v0.10.0
func (t ExecutableTasks) IntoScript(shell string, arg ...string) *Script
func (ExecutableTasks) String ¶ added in v0.10.0
func (t ExecutableTasks) String() string
type NullBuffer ¶ added in v0.8.0
type NullBuffer struct{}
NullBuffer providing no data on Read and perfoming no actions on Write.
type Option ¶
type Option func(*Config)
func WithSplitFunc ¶ added in v0.3.0
func WithStderrConsumer ¶
func WithStderrWriter ¶ added in v0.4.0
func WithStdoutConsumer ¶
func WithStdoutWriter ¶ added in v0.4.0
type Result ¶
type Result struct {
// ExpandedArgs is actual command.
ExpandedArgs []string
Stdout io.Reader
Stderr io.Reader
}
Result is Cmd execution result.
type Scanner ¶ added in v0.8.0
type Scanner struct {
// contains filtered or unexported fields
}
Scanner reads data from Reader and simultaneously writes it to Writer while passing it to the consumer.
func NewScanner ¶ added in v0.8.0
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
Example (Args) ¶
package main
import (
"context"
"fmt"
"io"
"github.com/berquerant/execx"
)
func main() {
script := execx.NewScript(`echo "$*"`, "sh")
if err := script.Runner(func(cmd *execx.Cmd) error {
cmd.Args = append(cmd.Args, "hello", "world")
r, err := cmd.Run(context.TODO())
if err != nil {
return err
}
out, err := io.ReadAll(r.Stdout)
if err != nil {
return err
}
fmt.Printf("%s\n", out)
return nil
}); err != nil {
panic(err)
}
}
Output: hello world