mirror of
https://github.com/containers/podman.git
synced 2025-12-10 07:42:12 +08:00
vendor latest containers/common
We had a couple of regressions in containers/common in the last release. Before cutting a new release, let's vendor it here. Since 3.0 has been branched, we can vendor a non-release commit of c/common. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
12
vendor/github.com/containers/common/pkg/report/doc.go
generated
vendored
12
vendor/github.com/containers/common/pkg/report/doc.go
generated
vendored
@@ -38,7 +38,17 @@ Helpers:
|
||||
... process JSON and output
|
||||
}
|
||||
|
||||
and
|
||||
Template Functions:
|
||||
|
||||
The following template functions are added to the template when parsed:
|
||||
- join strings.Join, {{join .Field separator}}
|
||||
- lower strings.ToLower {{ .Field | lower }}
|
||||
- split strings.Split {{ .Field | split }}
|
||||
- title strings.Title {{ .Field | title }}
|
||||
- upper strings.ToUpper {{ .Field | upper }}
|
||||
|
||||
report.Funcs() may be used to add additional template functions.
|
||||
Adding an existing function will replace that function for the life of that template.
|
||||
|
||||
|
||||
Note: Your code should not ignore errors
|
||||
|
||||
54
vendor/github.com/containers/common/pkg/report/template.go
generated
vendored
54
vendor/github.com/containers/common/pkg/report/template.go
generated
vendored
@@ -1,6 +1,8 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -21,16 +23,32 @@ type FuncMap template.FuncMap
|
||||
var tableReplacer = strings.NewReplacer(
|
||||
"table ", "",
|
||||
`\t`, "\t",
|
||||
`\n`, "\n",
|
||||
" ", "\t",
|
||||
)
|
||||
|
||||
// escapedReplacer will clean up escaped characters from CLI
|
||||
var escapedReplacer = strings.NewReplacer(
|
||||
`\t`, "\t",
|
||||
`\n`, "\n",
|
||||
)
|
||||
|
||||
var DefaultFuncs = FuncMap{
|
||||
"join": strings.Join,
|
||||
"json": func(v interface{}) string {
|
||||
buf := &bytes.Buffer{}
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
enc.Encode(v)
|
||||
// Remove the trailing new line added by the encoder
|
||||
return strings.TrimSpace(buf.String())
|
||||
},
|
||||
"lower": strings.ToLower,
|
||||
"pad": padWithSpace,
|
||||
"split": strings.Split,
|
||||
"title": strings.Title,
|
||||
"truncate": truncateWithLength,
|
||||
"upper": strings.ToUpper,
|
||||
}
|
||||
|
||||
// NormalizeFormat reads given go template format provided by CLI and munges it into what we need
|
||||
func NormalizeFormat(format string) string {
|
||||
var f string
|
||||
@@ -47,6 +65,22 @@ func NormalizeFormat(format string) string {
|
||||
return f
|
||||
}
|
||||
|
||||
// padWithSpace adds spaces*prefix and spaces*suffix to the input when it 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]
|
||||
}
|
||||
|
||||
// Headers queries the interface for field names.
|
||||
// Array of map is returned to support range templates
|
||||
// Note: unexported fields can be supported by adding field to overrides
|
||||
@@ -88,7 +122,7 @@ func Headers(object interface{}, overrides map[string]string) []map[string]strin
|
||||
|
||||
// NewTemplate creates a new template object
|
||||
func NewTemplate(name string) *Template {
|
||||
return &Template{template.New(name), false}
|
||||
return &Template{Template: template.New(name).Funcs(template.FuncMap(DefaultFuncs))}
|
||||
}
|
||||
|
||||
// Parse parses text as a template body for t
|
||||
@@ -100,13 +134,21 @@ func (t *Template) Parse(text string) (*Template, error) {
|
||||
text = NormalizeFormat(text)
|
||||
}
|
||||
|
||||
tt, err := t.Template.Parse(text)
|
||||
tt, err := t.Template.Funcs(template.FuncMap(DefaultFuncs)).Parse(text)
|
||||
return &Template{tt, t.isTable}, err
|
||||
}
|
||||
|
||||
// Funcs adds the elements of the argument map to the template's function map
|
||||
// Funcs adds the elements of the argument map to the template's function map.
|
||||
// A default template function will be replace if there is a key collision.
|
||||
func (t *Template) Funcs(funcMap FuncMap) *Template {
|
||||
return &Template{t.Template.Funcs(template.FuncMap(funcMap)), t.isTable}
|
||||
m := make(FuncMap)
|
||||
for k, v := range DefaultFuncs {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range funcMap {
|
||||
m[k] = v
|
||||
}
|
||||
return &Template{Template: t.Template.Funcs(template.FuncMap(m)), isTable: t.isTable}
|
||||
}
|
||||
|
||||
// IsTable returns true if format string defines a "table"
|
||||
|
||||
Reference in New Issue
Block a user