Documentation ¶
Index ¶
- func CSVColumn(name string) func(r io.Reader, w io.Writer) error
- func CSVFilter(fn func(row []string, header *CSVHeader) []string) func(io.Reader, io.Writer) error
- func CSVMapToJSON(fn func(row []string, header *CSVHeader) any) func(io.Reader, io.Writer) error
- func CSVMapToString(fn func(row []string, header *CSVHeader) string) func(io.Reader, io.Writer) error
- func CSVSort(lessThan func(row1, row2 []string, header *CSVHeader) bool) func(io.Reader, io.Writer) error
- func Printf(ptrn string) func(r io.Reader, w io.Writer) error
- func Run(pipes ...Runnable)
- func SubPipe(fn func(line string) *script.Pipe) func(r io.Reader, w io.Writer) error
- func ToCSV(splitter Splitter) func(io.Reader, io.Writer) error
- func ToJSON(fn func(line string) any) func(r io.Reader, w io.Writer) error
- type CSVHeader
- type Runnable
- type Splitter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CSVColumn ¶
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 CSVMapToJSON ¶
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 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 ToCSV ¶
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 ¶
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 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