scriptx

package module
v0.0.0-...-40707e4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 9 Imported by: 0

README

scriptx - Extras for Script

Utilities that extend github.com/bitfield/script.

Currently a WIP. The API is subject to change.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSVColumn

func CSVColumn(name string) func(r io.Reader, w io.Writer) error

CSVColumn is a filter function that reads the source as a CSV file and extracts the cell values of the named column, excluding the header itself. If the column cannot be found, the filter will produce nothing. If the column index is beyond the number of columns for a particular row, it will be skipped.

Example
package main

import (
	"github.com/bitfield/script"
	"github.com/lmika/scriptx"
)

func main() {
	script.Slice([]string{
		"letter,fruit,word",
		"a,apple,alpha",
		"b,banana,bravo",
		"c,cherry,charlie",
	}).Filter(scriptx.CSVColumn("fruit")).Stdout()
}
Output:

apple
banana
cherry

func CSVFilter

func CSVFilter(fn func(row []string, header *CSVHeader) []string) func(io.Reader, io.Writer) error

func CSVMapToJSON

func CSVMapToJSON(fn func(row []string, header *CSVHeader) any) func(io.Reader, io.Writer) error
Example
package main

import (
	"github.com/bitfield/script"
	"github.com/lmika/scriptx"
)

func main() {
	script.Slice([]string{
		"letter,fruit,word",
		"a,apple,alpha",
		"b,banana,bravo",
		"c,cherry,charlie",
	}).Filter(scriptx.CSVMapToJSON(func(row []string, header *scriptx.CSVHeader) any {
		return struct {
			Letter string `json:"letter"`
			Fruit  string `json:"fruit"`
			Word   string `json:"word"`
		}{
			Letter: header.Value(row, "letter"),
			Fruit:  header.Value(row, "fruit"),
			Word:   header.Value(row, "word"),
		}
	})).Stdout()
}
Output:

{"letter":"a","fruit":"apple","word":"alpha"}
{"letter":"b","fruit":"banana","word":"bravo"}
{"letter":"c","fruit":"cherry","word":"charlie"}

func CSVMapToString

func CSVMapToString(fn func(row []string, header *CSVHeader) string) func(io.Reader, io.Writer) error

func CSVSort

func CSVSort(lessThan func(row1, row2 []string, header *CSVHeader) bool) func(io.Reader, io.Writer) error

func Printf

func Printf(ptrn string) func(r io.Reader, w io.Writer) error

func Run

func Run(pipes ...Runnable)

Run takes a number of pipes and executes them in order, until they all succeed or the first error is encountered. If a runnable task fails with an error, it will write the error to stderr and terminate the program with exit code 1. It's designed to be used within the "main()" function.

func SubPipe

func SubPipe(fn func(line string) *script.Pipe) func(r io.Reader, w io.Writer) error

func ToCSV

func ToCSV(splitter Splitter) func(io.Reader, io.Writer) error

ToCSV is a filter function that reads the source as a series of lines, splits them into tokens using the passed in Splitter, and writes them to the output as a CSV.

Example
package main

import (
	"regexp"

	"github.com/bitfield/script"
	"github.com/lmika/scriptx"
)

func main() {
	script.Slice([]string{
		"letter  fruit   word",
		"a       apple   alpha",
		"b       banana  bravo",
		"c       cherry  charlie",
	}).Filter(scriptx.ToCSV(regexp.MustCompile(`\s+`))).Stdout()
}
Output:

letter,fruit,word
a,apple,alpha
b,banana,bravo
c,cherry,charlie

func ToJSON

func ToJSON(fn func(line string) any) func(r io.Reader, w io.Writer) error

ToJSON is a filter function which converts each line from the source to a JSON structure. The result is a line-terminated list of JSON objects. The passed in function is to return a Go value that can be marshalled to JSON value.

Example
package main

import (
	"github.com/bitfield/script"
	"github.com/lmika/scriptx"
)

func main() {
	fruits := []string{"apple", "banana", "pineapple"}
	script.Slice(fruits).Filter(scriptx.ToJSON(func(fruit string) any {
		return map[string]any{
			"fruit":  fruit,
			"length": len(fruit),
		}
	})).Stdout()
}
Output:

{"fruit":"apple","length":5}
{"fruit":"banana","length":6}
{"fruit":"pineapple","length":9}

Types

type CSVHeader

type CSVHeader struct {
	// contains filtered or unexported fields
}

func (*CSVHeader) Column

func (ch *CSVHeader) Column(name string) int

func (*CSVHeader) Value

func (ch *CSVHeader) Value(row []string, name string) string

type Runnable

type Runnable interface {
	// Stdout invokes the runnable task with the result written to stdout, if successful.  It returns the number
	// of bytes written out, or an error if the task was unsuccessful.
	Stdout() (int, error)
}

Runnable is a pipeline that can be executed by Run. The *script.Pipe type implements this interface

type Splitter

type Splitter interface {
	Split(line string, n int) []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL