Categorygithub.com/posener/script
modulepackage
1.2.0
Repository: https://github.com/posener/script.git
Documentation: pkg.go.dev

# README

script

codecov GoDoc

Package script provides helper functions to write scripts.

Inspired by https://github.com/bitfield/script, with some improvements:

  • Output between streamed commands is a stream and not loaded to memory.

  • Better representation and handling of errors.

  • Proper incocation, usage and handling of stderr of custom commands.

The script chain is represented by a Stream object. While each command in the stream is abstracted by the Command struct. This library provides basic functionality, but can be extended freely.

Examples

HelloWorld

A simple "hello world" example that creats a stream and pipe it to the stdout.

// Create an "hello world" stream and use the ToStdout method to write it to stdout.
Echo("hello world").ToStdout()

Output:

hello world

Iterate

An example that shows how to iterate scanned lines.

// Stream can be any stream, in this case we have echoed 3 lines.
stream := Echo("first\nsecond\nthird")

// To iterate over the stream lines, it is better not to read it into memory and split over the
// lines, but use the `bufio.Scanner`:
defer stream.Close()
scanner := bufio.NewScanner(stream)
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

Output:

first
second
third

Through

An example that shows how to create custom commands using the Through method with a PipeFn function.

Echo("1\n2\n3").Through(PipeFn(func(r io.Reader) (io.Reader, error) {
    // Create a command that sums up all numbers in input.
    //
    // In this example we create a reader function such that the whole code will fit into the
    // example function body. A more proper and readable way to do it was to create a new
    // type with a state that implements the `io.Reader` interface.

    // Use buffered reader to read lines from input.
    buf := bufio.NewReader(r)

    // Store the sum of all numbers.
    sum := 0

    // Read function reads the next line and adds it to the sum. If it gets and EOF error, it
    // writes the sum to the output and returns an EOF.
    read := func(b []byte) (int, error) {
        // Read next line from input.
        line, _, err := buf.ReadLine()

        // if EOF write sum to output.
        if err == io.EOF {
            return copy(b, append([]byte(strconv.Itoa(sum)), '\n')), io.EOF
        }
        if err != nil {
            return 0, err
        }

        // Convert the line to a number and add it to the sum.
        if i, err := strconv.Atoi(string(line)); err == nil {
            sum += i
        }

        // We don't write anything to output, so we return 0 bytes with no error.
        return 0, nil
    }

    return readerFn(read), nil
})).ToStdout()

Output:

6

Readme created from Go doc with goreadme

# Functions

No description provided by the author
Cat outputs the contents of the given files.
Echo writes to stdout.
Exec executes a command and returns a stream of the stdout of the command.
ExecHandleStderr executes a command, returns a stream of the stdout of the command and enable collecting the stderr of the command.
No description provided by the author
From creates a stream from a reader.
Ls returns a stream of a list files.
Stdin starts a stream from stdin.
Writer creates a stream from a function that writes to a writer.

# Structs

Count represents the output of `wc` shell command.
Cut is a `Modifier` that takes selected fields from each line according to a given delimiter.
File contains information about a file.
Files is a stream of a list of files.
Grep is a modifier that filters only line that match `Re`.
Stream is a chain of operations on a stream of bytes.
Uniq report or omit repeated lines.

# Interfaces

Modifier modifies input lines to output.
Pipe reads from a reader and returns another reader.

# Type aliases

ModifyFn is a function for modifying input lines.
PipeFn is a function that implements Pipe.