-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will make it a much smoother experience to test .liquid or .tpl files as they are being developed. Should be functionally equivalent to the process done in-agent.
- Loading branch information
1 parent
97979db
commit 6dc7f8f
Showing
7 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package template | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
"github.com/osteele/liquid" | ||
console "github.com/pluralsh/console-client-go" | ||
) | ||
|
||
var ( | ||
extensions = []string{".json", ".yaml", ".yml", ".yaml.liquid", ".yml.liquid", ".json.liquid"} | ||
liquidEngine = liquid.NewEngine() | ||
sprigFunctions = map[string]string{ | ||
"toJson": "to_json", | ||
"fromJson": "from_json", | ||
"b64enc": "b64enc", | ||
"b64dec": "b64dec", | ||
"semverCompare": "semver_compare", | ||
"sha256sum": "sha26sum", | ||
"quote": "quote", | ||
"squote": "squote", | ||
"replace": "replace", | ||
"coalesce": "coalesce", | ||
} | ||
) | ||
|
||
func init() { | ||
fncs := sprig.TxtFuncMap() | ||
for key, name := range sprigFunctions { | ||
liquidEngine.RegisterFilter(name, fncs[key]) | ||
} | ||
liquidEngine.RegisterFilter("indent", indent) | ||
liquidEngine.RegisterFilter("nindent", nindent) | ||
liquidEngine.RegisterFilter("replace", strings.ReplaceAll) | ||
|
||
liquidEngine.RegisterFilter("default", dfault) | ||
liquidEngine.RegisterFilter("ternary", ternary) | ||
} | ||
|
||
func renderLiquid(input []byte, bindings map[string]interface{}) ([]byte, error) { | ||
return liquidEngine.ParseAndRender(input, bindings) | ||
} | ||
|
||
func clusterConfiguration(cluster *console.BaseClusterFragment) map[string]interface{} { | ||
res := map[string]interface{}{ | ||
"ID": cluster.ID, | ||
"Self": cluster.Self, | ||
"Handle": cluster.Handle, | ||
"Name": cluster.Name, | ||
"Version": cluster.Version, | ||
"CurrentVersion": cluster.CurrentVersion, | ||
"KasUrl": cluster.KasURL, | ||
"Metadata": cluster.Metadata, | ||
} | ||
|
||
for k, v := range res { | ||
res[strings.ToLower(k)] = v | ||
} | ||
res["kasUrl"] = cluster.KasURL | ||
res["currentVersion"] = cluster.CurrentVersion | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package template | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
console "github.com/pluralsh/console-client-go" | ||
) | ||
|
||
func RenderYaml(path string, bindings map[string]interface{}) ([]byte, error) { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if strings.HasPrefix(path, ".tpl") { | ||
return renderTpl(content, bindings) | ||
} | ||
|
||
return renderLiquid(content, bindings) | ||
} | ||
|
||
func RenderService(path string, svc *console.ServiceDeploymentExtended) ([]byte, error) { | ||
bindings := map[string]interface{}{ | ||
"Configuration": configMap(svc), | ||
"Cluster": clusterConfiguration(svc.Cluster), | ||
"Contexts": contexts(svc), | ||
} | ||
return RenderYaml(path, bindings) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package template | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
) | ||
|
||
func renderTpl(input []byte, bindings map[string]interface{}) ([]byte, error) { | ||
tpl, err := template.New("gotpl").Funcs(sprig.TxtFuncMap()).Parse(string(input)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var buffer bytes.Buffer | ||
err = tpl.Execute(&buffer, bindings) | ||
return buffer.Bytes(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package template | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
console "github.com/pluralsh/console-client-go" | ||
) | ||
|
||
func configMap(svc *console.ServiceDeploymentExtended) map[string]string { | ||
res := map[string]string{} | ||
for _, config := range svc.Configuration { | ||
res[config.Name] = config.Value | ||
} | ||
|
||
return res | ||
} | ||
|
||
func contexts(svc *console.ServiceDeploymentExtended) map[string]map[string]interface{} { | ||
res := map[string]map[string]interface{}{} | ||
for _, context := range svc.Contexts { | ||
res[context.Name] = context.Configuration | ||
} | ||
return res | ||
} | ||
|
||
func indent(v string, spaces int) string { | ||
pad := strings.Repeat(" ", spaces) | ||
return pad + strings.ReplaceAll(v, "\n", "\n"+pad) | ||
} | ||
|
||
func nindent(v string, spaces int) string { | ||
return "\n" + indent(v, spaces) | ||
} | ||
|
||
func ternary(v bool, vt interface{}, vf interface{}) interface{} { | ||
if v { | ||
return vt | ||
} | ||
|
||
return vf | ||
} | ||
|
||
func dfault(v1, v2 interface{}) interface{} { | ||
if empty(v1) { | ||
return v2 | ||
} | ||
|
||
return v1 | ||
} | ||
|
||
func empty(given interface{}) bool { | ||
g := reflect.ValueOf(given) | ||
if !g.IsValid() { | ||
return true | ||
} | ||
|
||
// Basically adapted from text/template.isTrue | ||
switch g.Kind() { | ||
default: | ||
return g.IsNil() | ||
case reflect.Array, reflect.Slice, reflect.Map, reflect.String: | ||
return g.Len() == 0 | ||
case reflect.Bool: | ||
return !g.Bool() | ||
case reflect.Complex64, reflect.Complex128: | ||
return g.Complex() == 0 | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return g.Int() == 0 | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return g.Uint() == 0 | ||
case reflect.Float32, reflect.Float64: | ||
return g.Float() == 0 | ||
case reflect.Struct: | ||
return false | ||
} | ||
} |