2017-08-08 11:26:24 -04:00
package templates
import (
"bytes"
"encoding/json"
"strings"
"text/template"
)
// basicFunctions are the set of initial
// functions provided to every template.
var basicFunctions = template . FuncMap {
2023-11-20 12:04:36 -05:00
"json" : func ( v any ) string {
2017-08-08 11:26:24 -04:00
buf := & bytes . Buffer { }
enc := json . NewEncoder ( buf )
enc . SetEscapeHTML ( false )
2022-09-02 17:49:14 -04:00
err := enc . Encode ( v )
if err != nil {
panic ( err )
}
2017-08-08 11:26:24 -04:00
// Remove the trailing new line added by the encoder
return strings . TrimSpace ( buf . String ( ) )
} ,
"split" : strings . Split ,
"join" : strings . Join ,
golangci-lint: enable more linters
fix some nolintlint false positives
For some reason, nolintlint doesn't consider these used, but they seem to be
legitimate cases where deprecated fields are used.
templates/templates.go:27:29: directive `//nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope` is unused for linter "staticcheck" (nolintlint)
"title": strings.Title, //nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
^
cli/command/formatter/image_test.go:75:31: directive `//nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.` is unused for linter "staticcheck" (nolintlint)
call: ctx.VirtualSize, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
^
cli/command/registry/formatter_search.go:100:39: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
return c.formatBool(c.s.IsAutomated) //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:50:55: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:53:31: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:59:27: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:202:84: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-02 15:50:18 -04:00
"title" : strings . Title , //nolint:nolintlint,staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
2017-08-08 11:26:24 -04:00
"lower" : strings . ToLower ,
"upper" : strings . ToUpper ,
"pad" : padWithSpace ,
"truncate" : truncateWithLength ,
}
// HeaderFunctions are used to created headers of a table.
// This is a replacement of basicFunctions for header generation
// because we want the header to remain intact.
2019-12-24 11:32:44 -05:00
// Some functions like `pad` are not overridden (to preserve alignment
// with the columns).
2017-08-08 11:26:24 -04:00
var HeaderFunctions = template . FuncMap {
"json" : func ( v string ) string {
return v
} ,
2019-12-24 11:32:44 -05:00
"split" : func ( v string , _ string ) string {
// we want the table header to show the name of the column, and not
// split the table header itself. Using a different signature
// here, and return a string instead of []string
return v
} ,
"join" : func ( v string , _ string ) string {
// table headers are always a string, so use a different signature
// for the "join" function (string instead of []string)
return v
} ,
2017-08-08 11:26:24 -04:00
"title" : func ( v string ) string {
return v
} ,
"lower" : func ( v string ) string {
return v
} ,
"upper" : func ( v string ) string {
return v
} ,
"truncate" : func ( v string , _ int ) string {
return v
} ,
}
// Parse creates a new anonymous template with the basic functions
// and parses the given format.
func Parse ( format string ) ( * template . Template , error ) {
return NewParse ( "" , format )
}
2017-12-06 15:35:43 -05:00
// New creates a new empty template with the provided tag and built-in
// template functions.
func New ( tag string ) * template . Template {
return template . New ( tag ) . Funcs ( basicFunctions )
}
2017-08-08 11:26:24 -04:00
// NewParse creates a new tagged template with the basic functions
// and parses the given format.
func NewParse ( tag , format string ) ( * template . Template , error ) {
2017-12-06 15:35:43 -05:00
return New ( tag ) . Parse ( format )
2017-08-08 11:26:24 -04:00
}
// padWithSpace adds whitespace to the input if the input is non-empty
func padWithSpace ( source string , prefix , suffix int ) string {
if source == "" {
return source
}
return strings . Repeat ( " " , prefix ) + source + strings . Repeat ( " " , suffix )
}
// truncateWithLength truncates the source string up to the length provided by the input
func truncateWithLength ( source string , length int ) string {
if len ( source ) < length {
return source
}
return source [ : length ]
}