Skip to content

Commit

Permalink
add parseYaml to js
Browse files Browse the repository at this point in the history
  • Loading branch information
draganm committed Feb 3, 2022
1 parent 9b65f7d commit bea2d14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<yaml file> ...] | kubectl apply -f -
$ VAR=VALUE manifestor [--processors=] [<yaml file> ...] | kubectl apply -f -
```

## Interpolation format
Expand Down Expand Up @@ -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
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Expand Down

0 comments on commit bea2d14

Please sign in to comment.