modulepackage
0.4.0
Repository: https://github.com/rainu/go-command-chain.git
Documentation: pkg.go.dev
# README
go-command-chain
A go library for easy configure and run command chains. Such like pipelining in unix shells.
Example
cat log_file.txt | grep error | wc -l
package main
import (
"fmt"
"github.com/rainu/go-command-chain"
)
func main() {
stdOut, stdErr, err := cmdchain.Builder().
Join("cat", "log_file.txt").
Join("grep", "error").
Join("wc", "-l").
Finalize().RunAndGet()
if err != nil {
panic(err)
}
if stdErr != "" {
panic(stdErr)
}
fmt.Printf("Errors found: %s", stdOut)
}
For more examples how to use the command chain see examples.
Why you should use this library?
If you want to execute a complex command pipeline you could come up with the idea of just execute one command: the shell itself such like to following code:
package main
import (
"os/exec"
)
func main() {
exec.Command("sh", "-c", "cat log_file.txt | grep error | wc -l").Run()
}
But this procedure has some negative points:
- you must have installed the shell - in correct version - on the system itself
- so you are dependent on the shell
- you have no control over the individual commands - only the parent process (shell command itself)
- pipelining can be complex (redirection of stderr etc.) - so you have to know the pipeline syntax
- maybe this syntax is different for shell versions
(advanced) features
input injections
Multiple different input stream for each command can be configured. This can be useful if you want to forward multiple input sources to one command.
package main
import (
"github.com/rainu/go-command-chain"
"strings"
)
func main() {
inputContent1 := strings.NewReader("content from application itself\n")
inputContent2 := strings.NewReader("another content from application itself\n")
err := cmdchain.Builder().
Join("echo", "test").WithInjections(inputContent1, inputContent2).
Join("grep", "test").
Join("wc", "-l").
Finalize().Run()
if err != nil {
panic(err)
}
}
forking of stdout and stderr
Stdout and stderr of each command can be forked to different io.Writer.
package main
import (
"bytes"
"github.com/rainu/go-command-chain"
)
func main() {
echoErr := &bytes.Buffer{}
echoOut := &bytes.Buffer{}
grepErr := &bytes.Buffer{}
err := cmdchain.Builder().
Join("echo", "test").WithOutputForks(echoOut).WithErrorForks(echoErr).
Join("grep", "test").WithErrorForks(grepErr).
Join("wc", "-l").
Finalize().Run()
if err != nil {
panic(err)
}
}
# Packages
No description provided by the author
# Functions
Builder creates a new command chain builder.
IgnoreAll will return an ErrorChecker.
IgnoreExitCode will return an ErrorChecker.
IgnoreExitErrors will return an ErrorChecker.
IgnoreNothing will return an ErrorChecker.
# Structs
MultipleErrors fusions multiple errors into one error.
# Interfaces
ChainBuilder contains methods for joining new commands to the current cain or finalize them.
CommandBuilder contains methods for configuring the previous joined command.
FinalizedBuilder contains methods for configuration the the finalized chain.
FirstCommandBuilder contains methods for building the chain.
# Type aliases
CommandApplier is a function which will get the command's index and the command's reference.
ErrorChecker is a function which will receive the command's error.