talira

Making it easy to write shell-like scripts in Go

open sourceMITfree

script is a Go library for the jobs shell scripts are good at: reading files, running subprocesses, counting lines, matching strings and fetching URLs. Like a shell pipeline, you chain operations on a stream of data, such as script.File("log.txt").Match("Error").CountLines(), with sources (files, stdin, arguments, commands, HTTP requests, file listings), filters (match, reject, replace, column, jq, first and last lines, unique, hashes, custom Go functions) and sinks (stdout, files, strings, slices). Its author, John Arundel, wrote it as an exercise in his book The Power of Go: Tools.

Problems it solves

  • my bash scripts have grown too big and I want to rewrite them in Go without losing the pipeline style
  • write system administration programs in Go as easily as a shell script
  • grep, count and filter lines from files or stdin in a few lines of Go
  • run shell commands from Go and pipe their output through filters
  • fetch a URL and filter the response with jq-style queries in Go
  • type-checked, testable replacements for shell one-liners in DevOps tooling

Features

  • Chainable pipelines of sources, filters and sinks
  • Sources: files, stdin, arguments, commands, HTTP requests and file listings
  • Filters: Match, Reject, Replace (with regexp variants), Column, First, Last, Freq, Unique, JQ, hashing and custom Go functions
  • Run commands with Exec, ExecForEach and Shell
  • HTTP Get, Post and Do with a custom client
  • Sinks: stdout, files (write and append), strings, slices and line counts
  • Tee to send output to several places