buildctl: expose parseTemplate()

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda
2022-07-31 15:53:11 +09:00
parent 54a0df8bdd
commit 405565c005
2 changed files with 27 additions and 26 deletions

View File

@@ -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)
}

View File

@@ -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)
}