diff --git a/README.md b/README.md index aec9d43..b84db2f 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,12 @@ Unlike many other templating mechanisms, this utility will never output an inval Under the hood, it parses the input yaml files, finds string values, interpolates environment values into them using bash-like interpolation syntax and then re-assembles the resulting YAML(s) that are printed on stdout. +Also, there is an option of executing pre and post interpolation processors in ECMAScript 5.1(+). + ## Use ```bash -$ VAR=VALUE manifestor [ ...] | kubectl apply -f - +$ VAR=VALUE manifestor [--processors=] [ ...] | kubectl apply -f - ``` ## Interpolation format @@ -62,3 +64,23 @@ spec: servicePort: 3030 path: /api ``` + +## Pre and post interpolation processors + +String interpolation of environment variables is quite limited. +It won't cater for the cases where a number, boolean value or a whole sub-object needs to be changed, inserted or deleted. +In order to cover those cases, we support executing so-called _processors_. + +A _processor_ is a JavaScript function that takes an object representing one entity from the manifest and can change it in place. + +### Naming convention + +TODO + +### Available functions + +TODO + +### Available objects + +TODO diff --git a/main.go b/main.go index 622af7c..f5bba79 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,8 @@ func main() { app := &cli.App{ Flags: []cli.Flag{ &cli.StringSliceFlag{ - Name: "processors", + Name: "processor", EnvVars: []string{"PROCESSORS"}, - Value: &cli.StringSlice{}, }, }, Action: func(c *cli.Context) (err error) { @@ -43,15 +42,25 @@ func main() { vm := goja.New() - for _, p := range c.StringSlice("processors") { + for _, p := range c.StringSlice("processor") { script, err := os.ReadFile(p) if err != nil { return fmt.Errorf("while reading script %s: %w", p, err) } vm.RunScript(p, string(script)) } + vm.Set("env", env) + vm.Set("parseYaml", func(docString string) (interface{}, error) { + var v interface{} + err := yaml.NewDecoder(strings.NewReader(docString)).Decode(&v) + if err != nil { + return nil, err + } + return v, nil + }) + var preProcessors []func(interface{}) error var postProcessors []func(interface{}) error