lang: core: Move template to golang namespace

I don't think this template function should be in any way authoritative,
so let's namespace it.
This commit is contained in:
James Shubin
2024-09-13 15:51:24 -04:00
parent 5bbc06d8bc
commit 29eebd0d07
33 changed files with 110 additions and 43 deletions

View File

@@ -1,7 +1,8 @@
import "datetime"
import "golang"
$d = datetime.now()
consul:kv "love" {
key => "mgmt/time",
value => template("hello! it is now: {{ datetime_print . }}\n", $d),
value => golang.template("hello! it is now: {{ datetime_print . }}\n", $d),
}

View File

@@ -1,9 +1,10 @@
import "datetime"
import "golang"
$d = datetime.now()
file "/tmp/mgmt/datetime" {
state => $const.res.file.state.exists,
content => template("Hello! It is now: {{ datetime_print . }}\n", $d),
content => golang.template("Hello! It is now: {{ datetime_print . }}\n", $d),
}
file "/tmp/mgmt/" {

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "sys"
$secplusone = datetime.now() + $ayear
@@ -13,6 +14,6 @@ $theload float = sys.load()->x1 # ambiguous so we specify the type!
if 5 > 3 {
file "/tmp/mgmt/datetime" {
state => $const.res.file.state.exists,
content => template("Now + 1 year is: {{ .year }} seconds, aka: {{ datetime_print .year }}\n\nload average: {{ .load }}\n", $tmplvalues),
content => golang.template("Now + 1 year is: {{ .year }} seconds, aka: {{ datetime_print .year }}\n\nload average: {{ .load }}\n", $tmplvalues),
}
}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "sys"
import "example"
@@ -15,5 +16,5 @@ $vumeter = example.vumeter("====", 10, 0.9)
file "/tmp/mgmt/datetime" {
state => $const.res.file.state.exists,
content => template("Now + 1 year is: {{ .year }} seconds, aka: {{ datetime_print .year }}\n\nload average: {{ .load }}\n\nvu: {{ .vumeter }}\n", $tmplvalues),
content => golang.template("Now + 1 year is: {{ .year }} seconds, aka: {{ datetime_print .year }}\n\nload average: {{ .load }}\n\nvu: {{ .vumeter }}\n", $tmplvalues),
}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "sys"
import "example"
@@ -6,5 +7,5 @@ $now = datetime.now()
file "/tmp/mgmt-datetime" {
state => $const.res.file.state.exists,
content => template("Il est l'or Monseignor: {{ . }}\n", datetime.format($now, "15:04:05")),
content => golang.template("Il est l'or Monseignor: {{ . }}\n", datetime.format($now, "15:04:05")),
}

View File

@@ -6,6 +6,7 @@
# time ./mgmt run --hostname h4 --seeds=http://127.0.0.1:2379 --client-urls=http://127.0.0.1:2385 --server-urls=http://127.0.0.1:2386 --tmp-prefix --no-pgp empty
# time ./mgmt deploy --no-git --seeds=http://127.0.0.1:2379 lang examples/lang/exchange0.mcl
import "golang"
import "sys"
import "world"
@@ -15,5 +16,5 @@ $exchanged = world.exchange("keyns", $rand)
$host = sys.hostname()
file "/tmp/mgmt/exchange-${host}" {
state => $const.res.file.state.exists,
content => template("Found: {{ . }}\n", $exchanged),
content => golang.template("Found: {{ . }}\n", $exchanged),
}

View File

@@ -1,3 +1,4 @@
import "golang"
import "iter"
import "math"
@@ -9,6 +10,6 @@ $in1 = [8, -1, 0, 2, 4, 5, 13,]
$out1 = iter.filter($in1, $fn1)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -1,3 +1,4 @@
import "golang"
import "iter"
import "math"
@@ -9,6 +10,6 @@ $in1 = ["xxxxxx", "a", "bb", "ccc", "dddd", "eeeee",]
$out1 = iter.filter($in1, $fn1)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
$dt = datetime.now()
@@ -6,5 +7,5 @@ $hystvalues = {"ix0" => $dt, "ix1" => history($dt, 1), "ix2" => history($dt, 2),
file "/tmp/mgmt/history" {
state => $const.res.file.state.exists,
content => template("Index(0) {{.ix0}}: {{ datetime_print .ix0 }}\nIndex(1) {{.ix1}}: {{ datetime_print .ix1 }}\nIndex(2) {{.ix2}}: {{ datetime_print .ix2 }}\nIndex(3) {{.ix3}}: {{ datetime_print .ix3 }}\n", $hystvalues),
content => golang.template("Index(0) {{.ix0}}: {{ datetime_print .ix0 }}\nIndex(1) {{.ix1}}: {{ datetime_print .ix1 }}\nIndex(2) {{.ix2}}: {{ datetime_print .ix2 }}\nIndex(3) {{.ix3}}: {{ datetime_print .ix3 }}\n", $hystvalues),
}

View File

@@ -1,8 +1,9 @@
import "golang"
import "sys"
file "/tmp/mgmt/systemload" {
state => $const.res.file.state.exists,
content => template("load average: {{ .load }} threshold: {{ .threshold }}\n", $tmplvalues),
content => golang.template("load average: {{ .load }} threshold: {{ .threshold }}\n", $tmplvalues),
}
$tmplvalues = struct{load => $theload, threshold => $threshold,}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "iter"
import "math"
@@ -19,7 +20,7 @@ $in1 = if $mod {
$out1 = iter.map($in1, $fn)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test "example1" {
anotherstr => $t1,

View File

@@ -1,3 +1,4 @@
import "golang"
import "iter"
$fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
@@ -8,6 +9,6 @@ $in1 = ["a", "bb", "ccc", "dddd", "eeeee",]
$out1 = iter.map($in1, $fn)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "iter"
import "math"
@@ -23,6 +24,6 @@ $in1 = if $mod {
$out1 = iter.map($in1, $fn)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -1,4 +1,5 @@
import "datetime"
import "golang"
import "iter"
import "math"
@@ -35,7 +36,7 @@ $in1 = if $modb {
$out1 = iter.map($in1, $fn)
$t1 = template("out1: {{ . }}", $out1)
$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -1,11 +1,12 @@
import "datetime"
import "fmt"
import "golang"
$now = datetime.now()
$day = datetime.weekday($now)
$is_friday = $day == "friday"
$s1 = template("Hello! It is now: {{ datetime_print . }}\n", $now)
$s1 = golang.template("Hello! It is now: {{ datetime_print . }}\n", $now)
$s2 = if $is_friday {
"It's friday!!! (don't break anything, read-only)"
} else {

View File

@@ -1,3 +1,4 @@
import "golang"
import "sys"
import "world"
@@ -19,5 +20,5 @@ $set = world.schedule("xsched", $opts)
$host = sys.hostname()
file "/tmp/mgmt/scheduled-${host}" {
state => $const.res.file.state.exists,
content => template("set: {{ . }}\n", $set),
content => golang.template("set: {{ . }}\n", $set),
}

View File

@@ -1,4 +1,5 @@
import "fmt"
import "golang"
import "example"
$answer = 42
@@ -9,5 +10,5 @@ print "print1" {
}
print "print2" {
msg => template("an str is: {{ int2str . }}", $answer),
msg => golang.template("an str is: {{ int2str . }}", $answer),
}