FIXED:
* *Some* documentation weirdness on pkg.go dev rendering. It still uses
  the Markdown render by default, and it seems if you use anchor links
  in a bulletpoint list, pandoc just says "lol screw you"...

ADDED:
* tplx/sprigx tpl function `osHostname`
This commit is contained in:
brent saner
2026-01-28 09:16:18 -05:00
parent 927ad08057
commit d092f62d11
2 changed files with 116 additions and 79 deletions

View File

@@ -21,11 +21,22 @@ SprigX are extensions to https://masterminds.github.io/sprig/[the `sprig` librar
They provide functions that offer more enriched use cases and domain-specific data. They provide functions that offer more enriched use cases and domain-specific data.
[TIP]
====
If you are reading this README on the Go Module Directory documentation (https://pkg.go.dev/r00t2.io/goutils/tplx/sprigx)
or the directory landing page (https://git.r00t2.io/r00t2/go_goutils/src/branch/master/tplx/sprigx), it may not render correctly.
Be sure to view it at properly via https://git.r00t2.io/r00t2/go_goutils/src/branch/master/tplx/sprigx/README.adoc[the AsciiDoc rendering^]
or by downloading and viewing the https://git.r00t2.io/r00t2/go_goutils/raw/branch/master/tplx/sprigx/README.html[HTML version^].
====
[id="use"] [id="use"]
== How do I Use SprigX? == How do I Use SprigX?
The same way you would `sprig`!
[%collapsible] [%collapsible]
.The same way you would `sprig`! .Like this.
==== ====
[source,go] [source,go]
---- ----
@@ -40,21 +51,23 @@ import (
var ( var (
txtTpl *txtTplLib.Template = txtTplLib. txtTpl *txtTplLib.Template = txtTplLib.
New(""). New("").
Funcs( Funcs(
sprigx.TxtFuncMap(), sprigx.TxtFuncMap(),
) )
htmlTpl *htmlTplLib.Template = htmlTplLib. htmlTpl *htmlTplLib.Template = htmlTplLib.
New(""). New("").
Funcs( Funcs(
sprigx.HtmlFuncMap(), sprigx.HtmlFuncMap(),
) )
) )
---- ----
==== ====
They can even be combined/used together.
[%collapsible] [%collapsible]
.They can even be combined/used together. .Like this.
==== ====
[source,go] [source,go]
---- ----
@@ -68,23 +81,23 @@ import (
) )
var txtTpl *template.Template = template. var txtTpl *template.Template = template.
New(""). New("").
Funcs( Funcs(
sprigx.TxtFuncMap(), sprigx.TxtFuncMap(),
). ).
Funcs( Funcs(
sprig.TxtFuncMap(), sprig.TxtFuncMap(),
) )
// Or: // Or:
/* /*
var txtTpl *template.Template = template. var txtTpl *template.Template = template.
New(""). New("").
Funcs( Funcs(
sprig.TxtFuncMap(), sprig.TxtFuncMap(),
). ).
Funcs( Funcs(
sprigx.TxtFuncMap(), sprigx.TxtFuncMap(),
) )
*/ */
---- ----
==== ====
@@ -93,8 +106,42 @@ If a `<template>.FuncMap` is added via `.Funcs()` *after* template parsing, it w
For example, if both `sprig` and `sprigx` provide a function `foo`: For example, if both `sprig` and `sprigx` provide a function `foo`:
this will use `foo` from `sprigx`
[%collapsible] [%collapsible]
.this will use `foo` from `sprigx` .(show)
====
[source,go]
----
package main
import (
"text/template"
"github.com/Masterminds/sprig/v3"
"r00t2.io/goutils/tplx/sprigx"
)
const (
myTpl string = `{{ "This is an example template string." | foo }}`
)
var (
tpl *template.Template = template.Must(
template.
New("").
Funcs(sprig.TxtFuncMap()).
Parse(myTpl),
).
Funcs(sprigx.TxtFuncMap())
)
----
====
whereas this will use `foo` from `sprig`
[%collapsible]
.(show)
==== ====
[source,go] [source,go]
---- ----
@@ -113,52 +160,20 @@ const (
var ( var (
tpl *template.Template = template.Must( tpl *template.Template = template.Must(
template. template.
New(""). New("").
Funcs(sprig.TxtFuncMap()). Funcs(sprigx.TxtFuncMap()).
Parse(myTpl), Parse(myTpl),
). ).
Funcs(sprigx.TxtFuncMap()) Funcs(sprig.TxtFuncMap())
) )
---- ----
==== ====
whereas and a function can even be explicitly overridden.
[%collapsible] [%collapsible]
.this will use `foo` from `sprig` .(show)
====
[source,go]
----
package main
import (
"text/template"
"github.com/Masterminds/sprig/v3"
"r00t2.io/goutils/tplx/sprigx"
)
const (
myTpl string = `{{ "This is an example template string." | foo }}`
)
var (
tpl *template.Template = template.Must(
template.
New("").
Funcs(sprigx.TxtFuncMap()).
Parse(myTpl),
).
Funcs(sprig.TxtFuncMap())
)
----
====
and a function can even be
[%collapsible]
.explicitly overridden.
==== ====
This would override a function `foo` and `foo2` in `sprigx` from `foo` and `foo2` from `sprig`, but leave all other `sprig` functions untouched. This would override a function `foo` and `foo2` in `sprigx` from `foo` and `foo2` from `sprig`, but leave all other `sprig` functions untouched.
@@ -180,19 +195,19 @@ const (
var ( var (
overrideFuncs template.FuncMap = sprig.TxtFuncMap() overrideFuncs template.FuncMap = sprig.TxtFuncMap()
tpl *template.Template = template.Must( tpl *template.Template = template.Must(
template. template.
New(""). New("").
Funcs(sprigx.TxtFuncMap()). Funcs(sprigx.TxtFuncMap()).
Parse(myTpl), Parse(myTpl),
). ).
Funcs( Funcs(
template.FuncMap( template.FuncMap(
map[string]any{ map[string]any{
"foo": overrideFuncs["foo"], "foo": overrideFuncs["foo"],
"foo2": overrideFuncs["foo2"], "foo2": overrideFuncs["foo2"],
}, },
), ),
) )
) )
---- ----
==== ====
@@ -201,8 +216,17 @@ var (
== Functions == Functions
Expect this list to grow over time, and potentially more frequently than the `sprigx` functions. Expect this list to grow over time, and potentially more frequently than the `sprigx` functions.
[id="fn_os"]
=== Operating System
[id="fn_os_hstnm"]
==== `osHostname`
`osHostname` simply wraps and returns the result of calling https://pkg.go.dev/os#Hostname[`os.Hostname`^].
As such, it comes with the same caveats - it's possible for it to error, and it isn't guaranteed to be an FQDN -- it will be precisely/exactly whatever the kernel's hostname is set as.
[id="fn_sys"] [id="fn_sys"]
=== System/OS/Platform === System/Platform/Architecture
[id="fn_sys_arch"] [id="fn_sys_arch"]
==== `sysArch` ==== `sysArch`

View File

@@ -0,0 +1,13 @@
package sprigx
import (
"os"
)
// osHostname returns os.Hostname()
func osHostname() (out string, err error) {
out, err = os.Hostname()
return
}