Skip to content

Commit

Permalink
Add support for indent for toYaml: fix #46
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Oct 20, 2020
1 parent 8d36e4f commit 0edb5ab
Show file tree
Hide file tree
Showing 25 changed files with 11,492 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

BIN_NAME := bin/remco

VERSION := 0.12.0
VERSION := 0.12.1
GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_DIRTY := $(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
BUILD_DATE := $(shell date '+%Y-%m-%d-%H:%M:%S')
Expand Down
8 changes: 7 additions & 1 deletion cmd/remco/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type BackendConfigs struct {

// GetBackends returns a slice with all BackendConfigs for easy iteration.
func (c *BackendConfigs) GetBackends() []template.BackendConnector {
return []template.BackendConnector{
bc := []template.BackendConnector{
c.Etcd,
c.File,
c.Env,
Expand All @@ -50,6 +50,12 @@ func (c *BackendConfigs) GetBackends() []template.BackendConnector {
c.Zookeeper,
c.Mock,
}

for _, v := range c.Plugin {
bc = append(bc, &v)
}

return bc
}

// Configuration is the representation of an config file
Expand Down
7 changes: 1 addition & 6 deletions cmd/remco/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,13 @@ func (ru *Supervisor) runResource(r []Resource, stop, stopped chan struct{}) {
go func(r Resource) {
defer wait.Done()

backendConfigs := r.Backends.GetBackends()
for _, v := range r.Backends.Plugin {
backendConfigs = append(backendConfigs, &v)
}

rsc := template.ResourceConfig{
Exec: r.Exec,
Template: r.Template,
Name: r.Name,
StartCmd: r.StartCmd,
ReloadCmd: r.ReloadCmd,
Connectors: backendConfigs,
Connectors: r.Backends.GetBackends(),
}
res, err := template.NewResourceFromResourceConfig(ctx, ru.reapLock, rsc)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
Expand Down
41 changes: 37 additions & 4 deletions pkg/template/template_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
package template

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"path"
"path/filepath"
"reflect"
Expand All @@ -22,8 +25,9 @@ import (
"github.com/HeavyHorst/memkv"
"github.com/HeavyHorst/pongo2"
"github.com/dop251/goja"
"github.com/ghodss/yaml"
gyml "github.com/ghodss/yaml"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

func init() {
Expand Down Expand Up @@ -88,6 +92,12 @@ func pongoJSFilter(js string) func(in *pongo2.Value, param *pongo2.Value) (*pong
}
}

func parseParamMap(in string) (url.Values, error) {
in = strings.ReplaceAll(in, ", ", ",")
in = strings.ReplaceAll(in, ",", "&")
return url.ParseQuery(in)
}

func filterBase64(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
Expand Down Expand Up @@ -133,14 +143,37 @@ func filterToJSON(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2
}

func filterToYAML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
b, err := yaml.Marshal(in.Interface())
b := bytes.Buffer{}
yamlEncoder := yaml.NewEncoder(&b)

if param.String() != "" {
pm, err := parseParamMap(param.String())
if err != nil {
return nil, &pongo2.Error{
Sender: "filter:filterToYAML",
OrigError: fmt.Errorf("could't parese parameter list: %w", err),
}
}

indent, err := strconv.Atoi(pm.Get("indent"))
if err != nil {
return nil, &pongo2.Error{
Sender: "filter:filterToYAML",
OrigError: fmt.Errorf("couldn't parse integer: %w", err),
}
}

yamlEncoder.SetIndent(indent)
}

err := yamlEncoder.Encode(in.Interface())
if err != nil {
return nil, &pongo2.Error{
Sender: "filter:filterToYAML",
OrigError: err,
}
}
return pongo2.AsValue(string(b)), nil
return pongo2.AsValue(string(b.Bytes())), nil
}

func filterParseInt(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
Expand Down Expand Up @@ -191,7 +224,7 @@ func filterUnmarshalYAML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value,
}

var ret interface{}
if err := yaml.Unmarshal([]byte(in.String()), &ret); err != nil {
if err := gyml.Unmarshal([]byte(in.String()), &ret); err != nil {
return nil, &pongo2.Error{
Sender: "filterUnmarshalYAML",
OrigError: err,
Expand Down
17 changes: 17 additions & 0 deletions vendor/gopkg.in/yaml.v3/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/gopkg.in/yaml.v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/gopkg.in/yaml.v3/NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 150 additions & 0 deletions vendor/gopkg.in/yaml.v3/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0edb5ab

Please sign in to comment.