Skip to content

Instantly share code, notes, and snippets.

@nuxlli
Last active April 3, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuxlli/fbccc8b9850919cc10aef891a9a611f5 to your computer and use it in GitHub Desktop.
Save nuxlli/fbccc8b9850919cc10aef891a9a611f5 to your computer and use it in GitHub Desktop.
Generate tags from a lista without create new lists
{{- define "tags" }}
{{- tmpl.Exec "tags_with_pt" (slice "" 0 .) }}
{{- end }}
{{- define "tags_with_pt" }}
{{- $prefix := (index . 0)}}
{{- $pt := (index . 1) }}
{{- $items := (index . 2)}}
{{- range $i, $item := $items }}
{{- if ge $i $pt }}
{{- if (eq (printf "%T" $item) "string") }}
{{- $item = (slice $item)}}
{{- end }}
{{- range $value := $item }}
{{- print "- " $prefix $value "\n" }}
{{- $subprefix := (print $prefix $value "-")}}
{{- $new_pt := (add $i 1)}}
{{- tmpl.Exec "tags_with_pt" (slice $subprefix $new_pt $items) }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
tags:
{{- $tags := (slice (slice "poseidon" "izi_api") "deploy" $env) }}
{{ tmpl.Exec "tags" $tags | strings.Indent 2 }}
const slice = (...args) => args;
const index = (args, i) => args[i];
const print = (...args) => args.join('');
const add = (...args) => args.reduce((acc, v) => acc + v, 0);
const ge = (a, b) => a >= b;
function tags(items) {
return tags_with_pt(slice("", 0, items));
}
function tags_with_pt(args) {
let prefix = index(args, 0);
let pt = index(args, 1);
let items = index(args, 2);
items.map((item, i) => {
if (ge(i, pt)) {
if (typeof item == "string") {
item = slice(item);
}
item.map((value) => {
console.log(print("-", prefix, value));
const subprefix = print(prefix, value, "-");
const new_pt = add(i, 1);
tags_with_pt(slice(subprefix, new_pt, items))
})
}
});
}
tags([["poseidon", "izi-api"], ["deploy", "release", "test"], "prod"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment