Skip to content

Commit

Permalink
ls: add format, quiet and no-trunc opts
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Dec 19, 2022
1 parent 88852e2 commit d949a1d
Show file tree
Hide file tree
Showing 44 changed files with 13,227 additions and 64 deletions.
25 changes: 25 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package builder

import (
"context"
"encoding/json"
"os"
"sort"
"strings"
"sync"
"time"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/store"
Expand Down Expand Up @@ -240,6 +243,28 @@ func (b *Builder) Factory(ctx context.Context) (_ driver.Factory, err error) {
return b.driverFactory.Factory, err
}

func (b *Builder) MarshalJSON() ([]byte, error) {
var err string
if b.err != nil {
err = strings.TrimSpace(b.err.Error())
}
return json.Marshal(struct {
Name string
Driver string
LastActivity time.Time `json:",omitempty"`
Dynamic bool
Nodes []Node
Err string `json:",omitempty"`
}{
Name: b.Name,
Driver: b.Driver,
LastActivity: b.LastActivity,
Dynamic: b.Dynamic,
Nodes: b.nodes,
Err: err,
})
}

// GetBuilders returns all builders
func GetBuilders(dockerCli command.Cli, txn *store.Txn) ([]*Builder, error) {
storeng, err := txn.List()
Expand Down
42 changes: 42 additions & 0 deletions builder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package builder

import (
"context"
"encoding/json"
"strings"

"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/driver"
ctxkube "github.com/docker/buildx/driver/kubernetes/context"
"github.com/docker/buildx/store"
Expand Down Expand Up @@ -163,6 +166,45 @@ func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err e
return b.nodes, nil
}

func (n *Node) MarshalJSON() ([]byte, error) {
var status string
if n.DriverInfo != nil {
status = n.DriverInfo.Status.String()
}
var err string
if n.Err != nil {
status = "error"
err = strings.TrimSpace(n.Err.Error())
}
var pp []string
for _, p := range n.Platforms {
pp = append(pp, platforms.Format(p))
}
return json.Marshal(struct {
Name string
Endpoint string
Platforms []string `json:",omitempty"`
Flags []string `json:",omitempty"`
DriverOpts map[string]string `json:",omitempty"`
Files map[string][]byte `json:",omitempty"`
Status string `json:",omitempty"`
ProxyConfig map[string]string `json:",omitempty"`
Version string `json:",omitempty"`
Err string `json:",omitempty"`
}{
Name: n.Name,
Endpoint: n.Endpoint,
Platforms: pp,
Flags: n.Flags,
DriverOpts: n.DriverOpts,
Files: n.Files,
Status: status,
ProxyConfig: n.ProxyConfig,
Version: n.Version,
Err: err,
})
}

func (n *Node) loadData(ctx context.Context) error {
if n.Driver == nil {
return nil
Expand Down
82 changes: 25 additions & 57 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package commands
import (
"context"
"fmt"
"io"
"strings"
"text/tabwriter"
"time"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/moby/buildkit/util/appcontext"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

type lsOptions struct {
quiet bool
noTrunc bool
format string
}

func runLs(dockerCli command.Cli, in lsOptions) error {
Expand All @@ -41,39 +42,26 @@ func runLs(dockerCli command.Cli, in lsOptions) error {
return err
}

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

eg, _ := errgroup.WithContext(timeoutCtx)
for _, b := range builders {
func(b *builder.Builder) {
eg.Go(func() error {
_, _ = b.LoadNodes(timeoutCtx, true)
return nil
})
}(b)
}

if err := eg.Wait(); err != nil {
return err
}

w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "NAME/NODE\tDRIVER/ENDPOINT\tSTATUS\tBUILDKIT\tPLATFORMS\n")

printErr := false
for _, b := range builders {
if current.Name == b.Name {
b.Name += " *"
if !in.quiet {
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
eg, _ := errgroup.WithContext(timeoutCtx)
for _, b := range builders {
func(b *builder.Builder) {
eg.Go(func() error {
_, _ = b.LoadNodes(timeoutCtx, true)
return nil
})
}(b)
}
if ok := printBuilder(w, b); !ok {
printErr = true
if err := eg.Wait(); err != nil {
return err
}
}

w.Flush()

if printErr {
if hasErrors, err := lsPrint(dockerCli, current, builders, !in.noTrunc, in.quiet, in.format); err != nil {
return err
} else if hasErrors {
_, _ = fmt.Fprintf(dockerCli.Err(), "\n")
for _, b := range builders {
if b.Err() != nil {
Expand All @@ -91,31 +79,6 @@ func runLs(dockerCli command.Cli, in lsOptions) error {
return nil
}

func printBuilder(w io.Writer, b *builder.Builder) (ok bool) {
ok = true
var err string
if b.Err() != nil {
ok = false
err = "error"
}
fmt.Fprintf(w, "%s\t%s\t%s\t\t\n", b.Name, b.Driver, err)
if b.Err() == nil {
for _, n := range b.Nodes() {
var status string
if n.DriverInfo != nil {
status = n.DriverInfo.Status.String()
}
if n.Err != nil {
ok = false
fmt.Fprintf(w, " %s\t%s\t%s\t\t\n", n.Name, n.Endpoint, "error")
} else {
fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\n", n.Name, n.Endpoint, status, n.Version, strings.Join(platformutil.FormatInGroups(n.Node.Platforms, n.Platforms), ", "))
}
}
}
return
}

func lsCmd(dockerCli command.Cli) *cobra.Command {
var options lsOptions

Expand All @@ -128,6 +91,11 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
},
}

flags := cmd.Flags()
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display builder names")
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
flags.StringVar(&options.format, "format", formatter.TableFormatKey, "Format the output")

// hide builder persistent flag for this command
cobrautil.HideInheritedFlags(cmd, "builder")

Expand Down
Loading

0 comments on commit d949a1d

Please sign in to comment.