• Stars
    star
    59
  • Rank 509,601 (Top 11 %)
  • Language
    Go
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A go library for easy configure and run command chains. Such like pipelining in unix shells.

Go codecov Go Report Card Go Reference Mentioned in Awesome Go

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)
	}
}

More Repositories

1

spring-custom-token-auth

A little example how to use spring with a custom token authentication.
Java
26
star
2

mqtt-shell

A interactive shell-like command line interface (CLI) for MQTT written in Go.
Go
20
star
3

alexa-skill

A modular spring-boot application for alexa (amazon) skill.
Java
17
star
4

telegram-bot-api

This is an api-project for the telegram bot (HTTPS) api.
Java
16
star
5

samsung-remote

A golang library for remote controlling Samsung televisions via http(s) or websocket connection.
Go
15
star
6

samsung-remote-mqtt

A mqtt bridge for samsung smart tv.
Go
11
star
7

mqtt-executor

A simple MQTT client written in go that subscribes to a configurable list of MQTT topics on the specified broker and executes a given shell script/command whenever a message arrives.
Go
11
star
8

pdf-template

This project shows how to generate a PDF from a thymleaf-template.
HTML
10
star
9

dev-notes

A progressive web application (PWA) for notes that is fun!
Vue
9
star
10

launchpad

Go library to make it easy to use the Novation Launchpad (S and MK2)
Go
8
star
11

jsimpleshell

A framework for setting up a powerful shell as cli in java.
Java
6
star
12

krita-leonardo-ai

Krita Plugin for the Leonardo.Ai
Python
5
star
13

backup2glacier

A CLI-Tool for uploading (encrypted) backups to AWS-Glacier
Go
3
star
14

jb-project-opener

List all your JetBrains Projects and open the selected one
Go
2
star
15

docker-atom-editor

The atom editor inside a docker container.
Shell
2
star
16

rocketchat-user-proxy

A REST-Service which provides some basic endpoints to send Rocket.Chat-Messages.
Go
2
star
17

mqtt-logger

A simple MQTT client written in go that subscribes to a configurable list of MQTT topics on the specified broker and logs the whole payload to stdout.
Go
2
star
18

launchpad-super-trigger

Trigger application for the Novation Launchpad S
Go
2
star
19

wow-quest-reader

A World of Warcraft Addon which can read the quest text with meant of AI text-to-speech api(s).
Go
2
star
20

tldr-news

TooLong;DidntRead News - A tool for lazy readers. Crawl news ticker and provide a summary generated by openAI.
Vue
2
star
21

docker-gnucash

A gnucash docker image
2
star
22

gh-pages-with-nuxtjs

Demo project for how to create a github-page with Nuxt.js
Vue
1
star
23

ClipSync

Synchronize the clipboardcontent between multiple systems
Java
1
star
24

back2git

Backup to git - Watch files for changes and backup it to any git repository
Go
1
star
25

jsimpleshell-rc

This project contains remote-controll classes, that can be used for establish secret client-server communication between shell and user.
Java
1
star
26

r-ray

A lightweight proxy application where the client can define the request to the target.
Go
1
star
27

dbc

Database-Container. Collection implementations that store data into a database.
Java
1
star
28

rpaper

A device which connects to an mqtt-broker and receive picture information to draw it on ePaper display: rPaper.
C++
1
star
29

crypto-track-pwa

Track all crypto currencies and show the composition of your portfolio.
Vue
1
star
30

qrudicon

A combination of QR-Code and Identicon
Vue
1
star
31

bthvn2020

Vue
1
star
32

ifc

In-File Container. Collection implementations that store data into a file.
Java
1
star
33

env-parser

Parse environment variable names and build a single object out of them.
JavaScript
1
star