137 lines
2.6 KiB
Markdown
137 lines
2.6 KiB
Markdown
# package script
|
|
|
|
This package is intended as a collection of helper functions and tools fow quick construction of scripts that process .csv tables.
|
|
|
|
Note that this is a work in progress. API is not guaranteed to be stable.
|
|
|
|
## Example usage
|
|
|
|
This demonstrates a very basic case: reading from stdin, checking whether array len is 2 and switching array elements.
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"code.uint32.ru/dmitry/script"
|
|
)
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
|
defer stop()
|
|
|
|
reader := script.NewStdinReader()
|
|
writer := script.NewStdoutWriter()
|
|
|
|
processor := func(_ context.Context, in []string) ([]string, error) {
|
|
if len(in) != 2 {
|
|
return nil, errors.New("incorrect input len")
|
|
}
|
|
|
|
return []string{in[1], in[0]}, nil
|
|
}
|
|
|
|
conf := script.RunConfig{
|
|
Input: reader,
|
|
Output: writer,
|
|
Processor: processor,
|
|
}
|
|
|
|
if err := script.Run(ctx, conf); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
A more conplicated example featuring a processor which is itself a struct with external dependency.
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"code.uint32.ru/dmitry/script"
|
|
)
|
|
|
|
// ExternalDep represent an external dependency
|
|
// which your script utilizes. This can be a DB connection etc.
|
|
type ExternalDep interface {
|
|
QueryID(string) ([]string, error)
|
|
}
|
|
|
|
type Processor struct {
|
|
dep ExternalDep
|
|
}
|
|
|
|
func New(e ExternalDep) *Processor {
|
|
|
|
return &Processor{
|
|
dep: e,
|
|
}
|
|
}
|
|
|
|
func (p *Processor) Process(ctx context.Context, in []string) ([]string, error) {
|
|
var out []string
|
|
|
|
result, err := p.dep.QueryID(in[0])
|
|
if err != nil {
|
|
// you can as well record the error and continue
|
|
// processing without yielding the error here
|
|
return nil, err
|
|
}
|
|
|
|
out = append(out, result...)
|
|
out = append(out, "processed")
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
|
defer stop()
|
|
|
|
r, err := script.NewCSVReader("/tmp/some_file.csv")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer func() {
|
|
if err := r.Close(); err != nil {
|
|
fmt.Println("err closing reader", err)
|
|
}
|
|
}()
|
|
|
|
w, err := script.NewCSVWriter("/tmp/some_output.csv")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer func() {
|
|
if err := w.Close(); err != nil {
|
|
fmt.Println("err closing writer", err)
|
|
}
|
|
}()
|
|
|
|
// intializing the processor
|
|
p := New(ExternalDep(nil))
|
|
|
|
conf := script.RunConfig{
|
|
Input: r,
|
|
Output: w,
|
|
Processor: p.Process, // Process implements script.Processor
|
|
}
|
|
|
|
if err := script.Run(ctx, conf); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
``` |