diff --git a/cmd/buildctl/common/common.go b/cmd/buildctl/common/common.go index e1013160a41e..3a691dc0bac7 100644 --- a/cmd/buildctl/common/common.go +++ b/cmd/buildctl/common/common.go @@ -1,10 +1,14 @@ package common import ( + "bytes" "context" + "encoding/json" "net/url" "os" "path/filepath" + "strings" + "text/template" "time" "github.com/moby/buildkit/client" @@ -88,3 +92,25 @@ func ResolveClient(c *cli.Context) (*client.Client, error) { return client.New(ctx, c.GlobalString("addr"), opts...) } + +func ParseTemplate(format string) (*template.Template, error) { + // aliases is from https://github.com/containerd/nerdctl/blob/v0.17.1/cmd/nerdctl/fmtutil.go#L116-L126 (Apache License 2.0) + aliases := map[string]string{ + "json": "{{json .}}", + } + if alias, ok := aliases[format]; ok { + format = alias + } + // funcs is from https://github.com/docker/cli/blob/v20.10.12/templates/templates.go#L12-L20 (Apache License 2.0) + funcs := template.FuncMap{ + "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()) + }, + } + return template.New("").Funcs(funcs).Parse(format) +} diff --git a/cmd/buildctl/debug/workers.go b/cmd/buildctl/debug/workers.go index dbe4499f8e78..9a68d34d532b 100644 --- a/cmd/buildctl/debug/workers.go +++ b/cmd/buildctl/debug/workers.go @@ -1,15 +1,12 @@ package debug import ( - "bytes" "context" - "encoding/json" "fmt" "os" "sort" "strings" "text/tabwriter" - "text/template" "github.com/containerd/containerd/platforms" "github.com/moby/buildkit/client" @@ -54,7 +51,7 @@ func listWorkers(clicontext *cli.Context) error { if clicontext.Bool("verbose") { logrus.Debug("Ignoring --verbose") } - tmpl, err := parseTemplate(format) + tmpl, err := bccommon.ParseTemplate(format) if err != nil { return err } @@ -137,25 +134,3 @@ func joinPlatforms(p []ocispecs.Platform) string { } return strings.Join(str, ",") } - -func parseTemplate(format string) (*template.Template, error) { - // aliases is from https://github.com/containerd/nerdctl/blob/v0.17.1/cmd/nerdctl/fmtutil.go#L116-L126 (Apache License 2.0) - aliases := map[string]string{ - "json": "{{json .}}", - } - if alias, ok := aliases[format]; ok { - format = alias - } - // funcs is from https://github.com/docker/cli/blob/v20.10.12/templates/templates.go#L12-L20 (Apache License 2.0) - funcs := template.FuncMap{ - "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()) - }, - } - return template.New("").Funcs(funcs).Parse(format) -}