{"id":13410399,"url":"https://github.com/rainu/go-command-chain","last_synced_at":"2025-04-22T18:03:52.532Z","repository":{"id":144268625,"uuid":"366802415","full_name":"rainu/go-command-chain","owner":"rainu","description":"A go library for easy configure and run command chains. Such like pipelining in unix shells.","archived":false,"fork":false,"pushed_at":"2023-05-03T13:52:29.000Z","size":104,"stargazers_count":65,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-17T08:59:38.631Z","etag":null,"topics":["command","go","golang","library","pipeline","shell"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rainu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-05-12T17:47:41.000Z","updated_at":"2025-02-22T13:14:17.000Z","dependencies_parsed_at":"2023-08-20T22:27:38.041Z","dependency_job_id":null,"html_url":"https://github.com/rainu/go-command-chain","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainu%2Fgo-command-chain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainu%2Fgo-command-chain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainu%2Fgo-command-chain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainu%2Fgo-command-chain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rainu","download_url":"https://codeload.github.com/rainu/go-command-chain/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250295993,"owners_count":21407036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command","go","golang","library","pipeline","shell"],"created_at":"2024-07-30T20:01:06.655Z","updated_at":"2025-04-22T18:03:52.490Z","avatar_url":"https://github.com/rainu.png","language":"Go","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)\n[![codecov](https://codecov.io/gh/rainu/go-command-chain/branch/main/graph/badge.svg)](https://codecov.io/gh/rainu/go-command-chain)\n[![Go Report Card](https://goreportcard.com/badge/github.com/rainu/go-command-chain)](https://goreportcard.com/report/github.com/rainu/go-command-chain)\n[![Go Reference](https://pkg.go.dev/badge/github.com/rainu/go-command-chain.svg)](https://pkg.go.dev/github.com/rainu/go-command-chain)\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\n# go-command-chain\n![](https://media.discordapp.net/attachments/1101609055094575192/1102605830592925717/rainu_cyberpunkt_netrunner_cat_1_dad93401-86aa-4ea2-b4d6-c171077d401d.png)\n\nA go library for easy configure and run command chains. Such like pipelining in unix shells.\n\n# Example\n```sh\ncat log_file.txt | grep error | wc -l\n```\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/rainu/go-command-chain\"\n)\n\nfunc main() {\n\tstdOut, stdErr, err := cmdchain.Builder().\n\t\tJoin(\"cat\", \"log_file.txt\").\n\t\tJoin(\"grep\", \"error\").\n\t\tJoin(\"wc\", \"-l\").\n\t\tFinalize().RunAndGet()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif stdErr != \"\" {\n\t\tpanic(stdErr)\n\t}\n\tfmt.Printf(\"Errors found: %s\", stdOut)\n}\n```\n\nFor more examples how to use the command chain see [examples](example_test.go).\n\n# Why you should use this library?\n\nIf you want to execute a complex command pipeline you could come up with the idea of just execute **one** command: the\nshell itself such like to following code:\n\n```go\npackage main\n\nimport (\n\t\"os/exec\"\n)\n\nfunc main() {\n\texec.Command(\"sh\", \"-c\", \"cat log_file.txt | grep error | wc -l\").Run()\n}\n```\n\nBut this procedure has some negative points:\n* you must have installed the shell - in correct version - on the system itself\n    * so you are dependent on the shell\n* you have no control over the individual commands - only the parent process (shell command itself)\n* pipelining can be complex (redirection of stderr etc.) - so you have to know the pipeline syntax\n    * maybe this syntax is different for shell versions\n\n## (advanced) features\n\n### input injections\n**Multiple** different input stream for each command can be configured. This can be useful if you want to \nforward multiple input sources to one command.\n\n```go\npackage main\n\nimport (\n\t\"github.com/rainu/go-command-chain\"\n\t\"strings\"\n)\n\nfunc main() {\n\tinputContent1 := strings.NewReader(\"content from application itself\\n\")\n\tinputContent2 := strings.NewReader(\"another content from application itself\\n\")\n\n\terr := cmdchain.Builder().\n\t\tJoin(\"echo\", \"test\").WithInjections(inputContent1, inputContent2).\n\t\tJoin(\"grep\", \"test\").\n\t\tJoin(\"wc\", \"-l\").\n\t\tFinalize().Run()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\n### forking of stdout and stderr\n\nStdout and stderr of **each** command can be **forked** to different io.Writer.\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"github.com/rainu/go-command-chain\"\n)\n\nfunc main() {\n\techoErr := \u0026bytes.Buffer{}\n\techoOut := \u0026bytes.Buffer{}\n\tgrepErr := \u0026bytes.Buffer{}\n\t\n\terr := cmdchain.Builder().\n\t\tJoin(\"echo\", \"test\").WithOutputForks(echoOut).WithErrorForks(echoErr).\n\t\tJoin(\"grep\", \"test\").WithErrorForks(grepErr).\n\t\tJoin(\"wc\", \"-l\").\n\t\tFinalize().Run()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n","funding_links":[],"categories":["命令行","Command Line","Build Automation"],"sub_categories":["标准CLI","Standard CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frainu%2Fgo-command-chain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frainu%2Fgo-command-chain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frainu%2Fgo-command-chain/lists"}