mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
ls: format opt
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
226
commands/ls.go
226
commands/ls.go
@ -2,25 +2,41 @@ package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/docker/buildx/builder"
|
||||
"github.com/docker/buildx/store"
|
||||
"github.com/docker/buildx/store/storeutil"
|
||||
"github.com/docker/buildx/util/cobrautil"
|
||||
"github.com/docker/buildx/util/cobrautil/completion"
|
||||
"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"
|
||||
)
|
||||
|
||||
const (
|
||||
lsNameNodeHeader = "NAME/NODE"
|
||||
lsDriverEndpointHeader = "DRIVER/ENDPOINT"
|
||||
lsStatusHeader = "STATUS"
|
||||
lsLastActivityHeader = "LAST ACTIVITY"
|
||||
lsBuildkitHeader = "BUILDKIT"
|
||||
lsPlatformsHeader = "PLATFORMS"
|
||||
|
||||
lsIndent = ` \_ `
|
||||
|
||||
lsDefaultTableFormat = "table {{.Name}}\t{{.DriverEndpoint}}\t{{.Status}}\t{{.Buildkit}}\t{{.Platforms}}"
|
||||
)
|
||||
|
||||
type lsOptions struct {
|
||||
format string
|
||||
}
|
||||
|
||||
func runLs(dockerCli command.Cli, in lsOptions) error {
|
||||
@ -59,22 +75,9 @@ func runLs(dockerCli command.Cli, in lsOptions) error {
|
||||
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 ok := printBuilder(w, b); !ok {
|
||||
printErr = true
|
||||
}
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
|
||||
if printErr {
|
||||
if hasErrors, err := lsPrint(dockerCli, current, builders, in.format); err != nil {
|
||||
return err
|
||||
} else if hasErrors {
|
||||
_, _ = fmt.Fprintf(dockerCli.Err(), "\n")
|
||||
for _, b := range builders {
|
||||
if b.Err() != nil {
|
||||
@ -92,31 +95,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
|
||||
|
||||
@ -130,8 +108,170 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
|
||||
ValidArgsFunction: completion.Disable,
|
||||
}
|
||||
|
||||
flags := cmd.Flags()
|
||||
flags.StringVar(&options.format, "format", formatter.TableFormatKey, "Format the output")
|
||||
|
||||
// hide builder persistent flag for this command
|
||||
cobrautil.HideInheritedFlags(cmd, "builder")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func lsPrint(dockerCli command.Cli, current *store.NodeGroup, builders []*builder.Builder, format string) (hasErrors bool, _ error) {
|
||||
if format == formatter.TableFormatKey {
|
||||
format = lsDefaultTableFormat
|
||||
}
|
||||
|
||||
ctx := formatter.Context{
|
||||
Output: dockerCli.Out(),
|
||||
Format: formatter.Format(format),
|
||||
}
|
||||
|
||||
sort.SliceStable(builders, func(i, j int) bool {
|
||||
ierr := builders[i].Err() != nil
|
||||
jerr := builders[j].Err() != nil
|
||||
if ierr && !jerr {
|
||||
return false
|
||||
} else if !ierr && jerr {
|
||||
return true
|
||||
}
|
||||
return i < j
|
||||
})
|
||||
|
||||
render := func(format func(subContext formatter.SubContext) error) error {
|
||||
for _, b := range builders {
|
||||
if err := format(&lsContext{
|
||||
Builder: &lsBuilder{
|
||||
Builder: b,
|
||||
Current: b.Name == current.Name,
|
||||
},
|
||||
format: ctx.Format,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if b.Err() != nil {
|
||||
if ctx.Format.IsTable() {
|
||||
hasErrors = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
for _, n := range b.Nodes() {
|
||||
if n.Err != nil {
|
||||
if ctx.Format.IsTable() {
|
||||
hasErrors = true
|
||||
}
|
||||
}
|
||||
if err := format(&lsContext{
|
||||
format: ctx.Format,
|
||||
Builder: &lsBuilder{
|
||||
Builder: b,
|
||||
Current: b.Name == current.Name,
|
||||
},
|
||||
node: n,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
lsCtx := lsContext{}
|
||||
lsCtx.Header = formatter.SubHeaderContext{
|
||||
"Name": lsNameNodeHeader,
|
||||
"DriverEndpoint": lsDriverEndpointHeader,
|
||||
"LastActivity": lsLastActivityHeader,
|
||||
"Status": lsStatusHeader,
|
||||
"Buildkit": lsBuildkitHeader,
|
||||
"Platforms": lsPlatformsHeader,
|
||||
}
|
||||
|
||||
return hasErrors, ctx.Write(&lsCtx, render)
|
||||
}
|
||||
|
||||
type lsBuilder struct {
|
||||
*builder.Builder
|
||||
Current bool
|
||||
}
|
||||
|
||||
type lsContext struct {
|
||||
formatter.HeaderContext
|
||||
Builder *lsBuilder
|
||||
|
||||
format formatter.Format
|
||||
node builder.Node
|
||||
}
|
||||
|
||||
func (c *lsContext) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(c.Builder)
|
||||
}
|
||||
|
||||
func (c *lsContext) Name() string {
|
||||
if c.node.Name == "" {
|
||||
name := c.Builder.Name
|
||||
if c.Builder.Current && c.format.IsTable() {
|
||||
name += "*"
|
||||
}
|
||||
return name
|
||||
}
|
||||
if c.format.IsTable() {
|
||||
return lsIndent + c.node.Name
|
||||
}
|
||||
return c.node.Name
|
||||
}
|
||||
|
||||
func (c *lsContext) DriverEndpoint() string {
|
||||
if c.node.Name == "" {
|
||||
return c.Builder.Driver
|
||||
}
|
||||
if c.format.IsTable() {
|
||||
return lsIndent + c.node.Endpoint
|
||||
}
|
||||
return c.node.Endpoint
|
||||
}
|
||||
|
||||
func (c *lsContext) LastActivity() string {
|
||||
if c.node.Name != "" || c.Builder.LastActivity.IsZero() {
|
||||
return ""
|
||||
}
|
||||
return c.Builder.LastActivity.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func (c *lsContext) Status() string {
|
||||
if c.node.Name == "" {
|
||||
if c.Builder.Err() != nil {
|
||||
return "error"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if c.node.Err != nil {
|
||||
return "error"
|
||||
}
|
||||
if c.node.DriverInfo != nil {
|
||||
return c.node.DriverInfo.Status.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *lsContext) Buildkit() string {
|
||||
if c.node.Name == "" {
|
||||
return ""
|
||||
}
|
||||
return c.node.Version
|
||||
}
|
||||
|
||||
func (c *lsContext) Platforms() string {
|
||||
if c.node.Name == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(platformutil.FormatInGroups(c.node.Node.Platforms, c.node.Platforms), ", ")
|
||||
}
|
||||
|
||||
func (c *lsContext) Error() string {
|
||||
if c.node.Name != "" && c.node.Err != nil {
|
||||
return c.node.Err.Error()
|
||||
} else if err := c.Builder.Err(); err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user