mirror of
https://github.com/moby/buildkit.git
synced 2026-08-12 12:15:16 +00:00
buildctl: expose parseTemplate()
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user