
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
mygithub.libinneed.workers.dev/rainu/go-command-chain
A go library for easy configure and run command chains. Such like pipelining in unix shells.
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)
}
package main
import (
"fmt"
"github.com/rainu/go-command-chain"
)
func main() {
stdOut, stdErr, err := cmdchain.Builder().
JoinShellCmd(`cat log_file.txt | grep error | 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.
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:
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)
}
}
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)
}
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.