ycat

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: MIT Imports: 15 Imported by: 0

README

ycat

Command line processor for YAML/JSON files using Jsonnet

Usage

ycat - command line YAML/JSON processor

USAGE:
    ycat [OPTIONS] [INPUT...]
    ycat [OPTIONS] [PIPELINE...]

OPTIONS:
    -o, --out {json|j|yaml|y}    Set output format
    -h, --help                   Show help and exit

INPUT:
    [FILE...]                    Read values from file(s)
    -y, --yaml [FILE...]         Read YAML values from file(s)
    -j, --json [FILE...]         Read JSON values from file(s)
    -n, --null                   Inject a null value 
    -a, --array                  Merge values to array

PIPELINE:
    [INPUT...] [ENV...] EVAL

ENV:
    -v, --var <VAR>=<CODE>       Bind Jsonnet variable to code
              <VAR>==<VALUE>     Bind Jsonnet variable to a string value
    -i, --import <VAR>=<FILE>    Import file into a local Jsonnet variable
        --input-var <VAR>        Change the name of the input value variable (default x) 
        --max-stack <SIZE>       Jsonnet VM max stack size (default 500)

EVAL:
    <SCRIPT>                     Evaluate a Jsonnet script for each value.
    -x, --exec <SCRIPT>          Same as above regardless of file extension.
    -e, --eval <SNIPPET>         Evaluate a Jsonnet snippet for each value.


If no INPUT is specified, values are read from stdin as YAML.
If FILE is "-" or "" values are read from stdin until EOF.
If FILE has no type option, format is detected from extension:
    .json         -> JSON
    .yaml, .yml   -> YAML
    .jsonnet      -> Jsonnet script
    .*            -> YCAT_FORMAT environment variable or YAML

Default output format is YAML unless YCAT_OUTPUT environment variable is 'json'

Examples

Concatenate files to a single YAML stream (type is selected from extension)

$ ycat foo.yaml bar.yaml baz.json

Concatenate files to single JSON stream (one item per-line)

$ ycat -o j foo.yaml bar.yaml baz.json

Concatenate JSON values from stdin to a single YAML file

$ ycat -j

Concatenate YAML from a.txt, stdin, b.yaml and JSON from a.json,

$ ycat -y a.txt - b.txt -j a.json

Concatenate to YAML array

$ ycat a.json b.yaml -a

Concatenate YAML from a.yaml and b.yaml setting key foo to bar on each top level object

$ ycat a.yaml b.yaml -e 'x+{foo: "bar"}'

Same as above with results merged into a single array

$ ycat a.yaml b.yaml -e 'x+{foo: "bar"}' -a

Add kubernetes namespace foo to all resources without namespace

$ ycat *.yaml -e ' { metadata +: { namespace: "foo" } } + x'

Execute foo.jsonnet file with x local var bound to variables from bar.json, baz.yaml

$ ycat bar.json baz.yaml foo.jsonnet

Process with jq using a pipe

$ ycat -oj a.yaml b.json | jq ... | ycat 

Installation

Download an executable for your platform from github releases.

Alternatively, assuming $GOPATH/bin is in $PATH just

go get github.com/alxarch/ycat/cmd/ycat

Input / Output

YAML input

Multiple YAML values separated by ---\n are processed separately. Value reading stops at ...\n or EOF.

YAML output

Each result value is appended to the output with ---\n separator.

JSON input

Multiple JSON values separated by whitespace are processed separately. Value reading stops at EOF.

JSON output

Each result value is appended into a new line of output.

Jsonnet

Jsonnet is a templating language from google that's really versatile in handling configuration files. Visit their site for more information.

Each value is bound to a local variable named x inside the snippet by default. Use --input-var to change the name.

To use Jsonnet code from a file in the snippet use -i <VAR>=<FILE> and the exported value will be available as a local variable in the snippet.

To run a .jsonnet script just add it as an argument. Variables are the same as the snippet.

Local variables are bound before code in a script or snippet. It's up to the user to avoid conflicts/overrides.

Some experimental (undocumented for now) helper methods are bound to _ local variable. These will be documented once tests are in place and the API is more stable. For now look at ycat.libsonnet file.

Caveats

  • YAML comments are not preserved. (This is a shortcoming of gopkg.in/yaml package since there's no access to the AST)
  • Only the JSON compatible subset of YAML is supported (the one that makes sense)
  • Key order of objects is not preserved if processed with Jsonnet

TODO

  • Add support for pretty printed output
  • Add support for reading .txt files
  • Add support for reading files as base64
  • Add support for reading files as hex
  • Add support for sorting by JSONPath

Documentation

Overview

Code generated by ycat; DO NOT EDIT.

Index

Constants

View Source
const DefaultInputVar = "x"

DefaultInputVar is the default name for the stream value

View Source
const EnvDefaultFormat = "YCAT_FORMAT"

EnvDefaultFormat is the name of the env var for file format detection

View Source
const EnvDefaultOutput = "YCAT_OUTPUT"

EnvDefaultOutput is the name of the env var for default file output

View Source
const Usage = `` /* 1599-byte string literal not displayed */

Usage for ycat cmd

Variables

This section is empty.

Functions

func Drain

func Drain(s Stream) bool

Drain is a helper that drains all values from a stream

func EvalFilename added in v0.2.0

func EvalFilename() (string, error)

EvalFilename returns a filename on CWD

func MarshalJSONString added in v0.2.3

func MarshalJSONString(x interface{}) (string, error)

MarshalJSONString marshals any value to JSON string

func MergeErrors

func MergeErrors(cs ...<-chan error) <-chan error

MergeErrors is a helper function that merges error channels

Types

type Consumer added in v0.2.3

type Consumer interface {
	Consume(s ReadStream) error
}

Consumer consumes values from a readable stream

type ConsumerFunc added in v0.2.3

type ConsumerFunc func(s ReadStream) error

ConsumerFunc is a Consumer callback

func StreamWriteJSON added in v0.2.3

func StreamWriteJSON(w io.WriteCloser) ConsumerFunc

StreamWriteJSON creates a StreamTask to write values as JSON to a Writer

func StreamWriteYAML added in v0.2.3

func StreamWriteYAML(w io.WriteCloser) ConsumerFunc

StreamWriteYAML creates a StreamTask to write values as YAML to a Writer

func (ConsumerFunc) Consume added in v0.2.3

func (f ConsumerFunc) Consume(s ReadStream) error

Consume implements Consumer

func (ConsumerFunc) Run added in v0.2.3

func (f ConsumerFunc) Run(s Stream) error

Run implements StreamTask

type Debug added in v0.2.4

type Debug string

func (Debug) Run added in v0.2.4

func (d Debug) Run(s Stream) (err error)

type Decoder

type Decoder interface {
	Decode(interface{}) error
}

Decoder is a value decoder

func NewDecoder

func NewDecoder(r io.Reader, format Format) Decoder

NewDecoder creates a new Decoder decoding values from a Reader

type Eval

type Eval struct {
	Bind         string
	MaxStackSize int
	Array        bool
	Vars         map[string]Var
	// contains filtered or unexported fields
}

Eval is the execution environment for Jsonnet

func (*Eval) AddVar added in v0.2.0

func (e *Eval) AddVar(typ VarType, name, value string)

AddVar adds an external variable

func (*Eval) Render

func (e *Eval) Render(snippet string) string

Render renders a snippet binding local variables

func (*Eval) Snippet

func (e *Eval) Snippet(filename, snippet string) StreamTask

EvalSnippetTask transforms a stream of input values with Jsonnet

func (*Eval) SnippetFromFile added in v0.2.4

func (e *Eval) SnippetFromFile(filename string) StreamTask

func (*Eval) VM added in v0.2.0

func (e *Eval) VM() (vm *jsonnet.VM)

VM updates or creates a Jsonnet VM

type Format

type Format uint

Format is input file format

const (
	Auto Format = iota
	YAML
	JSON
	JSONNET
)

Input formats

func DefaultFormat added in v0.2.4

func DefaultFormat() Format

DefaultFormat returns the default format from environment

func DetectFormat

func DetectFormat(filename string) Format

DetectFormat detects an input format from the extension

func FormatFromString

func FormatFromString(s string) Format

FormatFromString converts a string to Format

type JSONStringMarshaler added in v0.2.3

type JSONStringMarshaler interface {
	MarshalJSONString() (string, error)
}

JSONStringMarshaler marshals to JSON string

type Map added in v0.2.3

type Map yaml.MapSlice

Map wraps yaml.MapSlice to provide json Marshaler/Unmarshaller

func NewMap added in v0.2.3

func NewMap(pairs ...interface{}) (m Map)

NewMap creates a Map from key/value pairs

func (Map) MarshalJSON added in v0.2.3

func (m Map) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for Map

func (Map) MarshalJSONString added in v0.2.3

func (m Map) MarshalJSONString() (string, error)

MarshalJSONString implements JSONStringMarshaler for Map

func (Map) MarshalYAML added in v0.2.3

func (m Map) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaler for Map

func (*Map) UnmarshalJSON added in v0.2.3

func (m *Map) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler for Map

func (*Map) UnmarshalYAML added in v0.2.3

func (m *Map) UnmarshalYAML(fn func(x interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaler for Map

type NullStream added in v0.2.0

type NullStream struct{}

NullStream is a Producer that pushes a null

func (NullStream) Produce added in v0.2.3

func (NullStream) Produce(s WriteStream) error

Produce implements Producer for NullStream

type Output

type Output int

Output is an output format

const (
	OutputInvalid Output = iota
	OutputYAML
	OutputJSON
)

Output formats

func DefaultOutput added in v0.2.4

func DefaultOutput() Output

DefaultOutput returns the default output from environment

func OutputFromString

func OutputFromString(s string) Output

OutputFromString converts a string to Output

type Pipeline added in v0.2.0

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

Pipeline is the endpoint of a value stream process

func MakePipeline added in v0.2.3

func MakePipeline(ctx context.Context, tasks ...StreamTask) (p *Pipeline)

MakePipeline builds and runs a pipeline

func (*Pipeline) Errors added in v0.2.0

func (p *Pipeline) Errors() <-chan error

Errors returns a channel with errors from tasks

func (*Pipeline) Pipe added in v0.2.0

func (p *Pipeline) Pipe(ctx context.Context, tasks ...StreamTask) *Pipeline

Pipe adds tasks ro a pipeline

func (*Pipeline) Values added in v0.2.0

func (p *Pipeline) Values() <-chan RawValue

Values returns a channel with values from tasks

type Producer added in v0.2.3

type Producer interface {
	Produce(s WriteStream) error
}

Producer generates values for a WriteStream

type ProducerFunc added in v0.2.3

type ProducerFunc func(s WriteStream) error

ProducerFunc is a Producer callback

func ReadFromFile added in v0.2.3

func ReadFromFile(path string, format Format) ProducerFunc

ReadFromFile creates a StreamTask to read values from a file

func ReadFromTask added in v0.2.0

func ReadFromTask(r io.Reader, format Format) ProducerFunc

ReadFromTask creates a StreamTask to read values from a Reader

func (ProducerFunc) Produce added in v0.2.3

func (f ProducerFunc) Produce(s WriteStream) error

Produce implements Producer for ProducerFunc

func (ProducerFunc) Run added in v0.2.3

func (f ProducerFunc) Run(s Stream) error

Run implements StreamTask for ProducerFunc

type Producers added in v0.2.3

type Producers []Producer

Producers is a sequence of Producers

func (Producers) Produce added in v0.2.3

func (tasks Producers) Produce(s WriteStream) error

Produce implements Producer for Producers

func (Producers) Run added in v0.2.3

func (tasks Producers) Run(s Stream) error

Run implements StreamTask for Producers

type RawValue added in v0.2.3

type RawValue string

RawValue is a JSON string

func NewRawValue added in v0.2.3

func NewRawValue(x interface{}) (RawValue, error)

NewRawValue creates a new RawValue from any value

func RawValueArray added in v0.2.3

func RawValueArray(values ...RawValue) RawValue

RawValueArray joins RawValues to an array

func (RawValue) Compact added in v0.2.3

func (v RawValue) Compact() (RawValue, error)

Compact removes insignificant white space

func (RawValue) Kind added in v0.2.3

func (v RawValue) Kind() ValueType

Kind returns the value's type

func (RawValue) MarshalJSON added in v0.2.3

func (v RawValue) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for RawValue

func (RawValue) MarshalJSONString added in v0.2.4

func (v RawValue) MarshalJSONString() string

func (RawValue) MarshalYAML added in v0.2.3

func (v RawValue) MarshalYAML() (x interface{}, err error)

MarshalYAML implements yaml.Marshaler for RawValue

func (RawValue) String added in v0.2.3

func (v RawValue) String() string

func (*RawValue) UnmarshalJSON added in v0.2.3

func (v *RawValue) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for RawValue

func (*RawValue) UnmarshalYAML added in v0.2.3

func (v *RawValue) UnmarshalYAML(fn func(interface{}) error) (err error)

UnmarshalYAML implements yaml.Unmarshaler for RawValue

type ReadStream added in v0.2.3

type ReadStream interface {
	Next() (RawValue, bool)
}

ReadStream is a readable stream of values

type Stream added in v0.2.0

type Stream interface {
	ReadStream
	WriteStream
}

Stream is a readable/writable stream of values

type StreamFunc added in v0.2.0

type StreamFunc func(s Stream) error

StreamFunc is a StreamTask callback

func (StreamFunc) Run added in v0.2.0

func (f StreamFunc) Run(s Stream) error

Run implements StreamTask for StramFunc

type StreamTask added in v0.2.0

type StreamTask interface {
	Run(s Stream) error
}

StreamTask represents a task to run on a Stream

func ParseArgs added in v0.2.0

func ParseArgs(argv []string, stdin io.Reader, stdout io.WriteCloser) ([]StreamTask, bool, error)

ParseArgs builds a StreamTask sequence from arguments

type ToArray

type ToArray struct{}

ToArray concatenates stream values to an array

func (ToArray) Run added in v0.2.0

func (ToArray) Run(s Stream) (err error)

Run implements StreamTask for ToArray

type ValueType

type ValueType int

ValueType is the JSON type of a value

const (
	Invalid ValueType = iota - 1
	Null
	Object
	Array
	String
	Number
	Boolean
)

Value types

func (ValueType) String

func (t ValueType) String() string

type Var added in v0.2.0

type Var struct {
	Type  VarType
	Value string
}

Var is an external variable

func (Var) Render added in v0.2.0

func (v Var) Render(w *strings.Builder, name string)

Render renders the Jsonnet snippet to be executed

type VarType added in v0.2.0

type VarType uint

VarType is the type of an external variable

const (
	FileVar VarType
	CodeVar
	RawVar
)

VarTypes

type WriteStream added in v0.2.3

type WriteStream interface {
	Push(RawValue) bool
}

WriteStream is a writtable stream of values

Directories

Path Synopsis
cmd
ycat command

Jump to

Keyboard shortcuts

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