Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rainu/go-command-chain
A go library for easy configure and run command chains. Such like pipelining in unix shells.
https://github.com/rainu/go-command-chain
command go golang library pipeline shell
Last synced: 13 days ago
JSON representation
A go library for easy configure and run command chains. Such like pipelining in unix shells.
- Host: GitHub
- URL: https://github.com/rainu/go-command-chain
- Owner: rainu
- License: mit
- Created: 2021-05-12T17:47:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-03T13:52:29.000Z (over 1 year ago)
- Last Synced: 2024-04-22T13:31:55.268Z (7 months ago)
- Topics: command, go, golang, library, pipeline, shell
- Language: Go
- Homepage:
- Size: 102 KB
- Stars: 57
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. (Command Line / Standard CLI)
- fucking-awesome-go - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. (Command Line / Standard CLI)
- awesome-go - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. (Command Line / Standard CLI)
- awesome-go - command-chain - A go library for configure and run command chains - such like pipelining in unix shells. (Command Line / Standard CLI)
- awesome-go-extra - go-command-chain - 05-12T17:47:41Z|2022-03-26T15:48:37Z| (Build Automation / Standard CLI)
- awesome-go-with-stars - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. (Command Line / Standard CLI)
- awesome-go-plus - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. (Command Line / Standard CLI)
- awesome-go-plus - command-chain - A go library for configure and run command chains - such as pipelining in unix shells. ![stars](https://img.shields.io/badge/stars-62-blue) ![forks](https://img.shields.io/badge/forks-2-blue) (Command Line / Standard CLI)
README
[![Go](https://github.com/rainu/go-command-chain/actions/workflows/build.yml/badge.svg)](https://github.com/rainu/go-command-chain/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/rainu/go-command-chain/branch/main/graph/badge.svg)](https://codecov.io/gh/rainu/go-command-chain)
[![Go Report Card](https://goreportcard.com/badge/github.com/rainu/go-command-chain)](https://goreportcard.com/report/github.com/rainu/go-command-chain)
[![Go Reference](https://pkg.go.dev/badge/github.com/rainu/go-command-chain.svg)](https://pkg.go.dev/github.com/rainu/go-command-chain)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)# go-command-chain
![](https://media.discordapp.net/attachments/1101609055094575192/1102605830592925717/rainu_cyberpunkt_netrunner_cat_1_dad93401-86aa-4ea2-b4d6-c171077d401d.png)A go library for easy configure and run command chains. Such like pipelining in unix shells.
# Example
```sh
cat log_file.txt | grep error | wc -l
``````go
package mainimport (
"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](example_test.go).
# 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:```go
package mainimport (
"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.```go
package mainimport (
"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.
```go
package mainimport (
"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)
}
}
```