Skip to content

Commit

Permalink
buildctl: expose parseTemplate()
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Jul 31, 2022
1 parent 54a0df8 commit 405565c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
26 changes: 26 additions & 0 deletions cmd/buildctl/common/common.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}
27 changes: 1 addition & 26 deletions cmd/buildctl/debug/workers.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 405565c

Please sign in to comment.