mirror of
https://github.com/docker/cli.git
synced 2026-10-15 18:33:41 -04:00
Move api/client -> cli/command
Using
gomvpkg
-from github.com/docker/docker/api/client
-to github.com/docker/docker/cli/command
-vcs_mv_cmd 'git mv {{.Src}} {{.Dst}}'
Signed-off-by: Daniel Nephin <dnephin@docker.com>
Upstream-commit: 3bd1eb4b76
Component: cli
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/cli/command"
|
||||
"github.com/docker/docker/cli/command/idresolver"
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
const (
|
||||
psTaskItemFmt = "%s\t%s\t%s\t%s\t%s\t%s %s ago\t%s\n"
|
||||
maxErrLength = 30
|
||||
)
|
||||
|
||||
type tasksBySlot []swarm.Task
|
||||
|
||||
func (t tasksBySlot) Len() int {
|
||||
return len(t)
|
||||
}
|
||||
|
||||
func (t tasksBySlot) Swap(i, j int) {
|
||||
t[i], t[j] = t[j], t[i]
|
||||
}
|
||||
|
||||
func (t tasksBySlot) Less(i, j int) bool {
|
||||
// Sort by slot.
|
||||
if t[i].Slot != t[j].Slot {
|
||||
return t[i].Slot < t[j].Slot
|
||||
}
|
||||
|
||||
// If same slot, sort by most recent.
|
||||
return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
|
||||
}
|
||||
|
||||
// Print task information in a table format
|
||||
func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
||||
sort.Stable(tasksBySlot(tasks))
|
||||
|
||||
writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
|
||||
|
||||
// Ignore flushing errors
|
||||
defer writer.Flush()
|
||||
fmt.Fprintln(writer, strings.Join([]string{"ID", "NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
|
||||
|
||||
prevName := ""
|
||||
for _, task := range tasks {
|
||||
serviceValue, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := serviceValue
|
||||
if task.Slot > 0 {
|
||||
name = fmt.Sprintf("%s.%d", name, task.Slot)
|
||||
}
|
||||
|
||||
// Indent the name if necessary
|
||||
indentedName := name
|
||||
if prevName == name {
|
||||
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
||||
}
|
||||
prevName = name
|
||||
|
||||
// Trim and quote the error message.
|
||||
taskErr := task.Status.Err
|
||||
if !noTrunc && len(taskErr) > maxErrLength {
|
||||
taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
|
||||
}
|
||||
if len(taskErr) > 0 {
|
||||
taskErr = fmt.Sprintf("\"%s\"", taskErr)
|
||||
}
|
||||
|
||||
fmt.Fprintf(
|
||||
writer,
|
||||
psTaskItemFmt,
|
||||
task.ID,
|
||||
indentedName,
|
||||
task.Spec.ContainerSpec.Image,
|
||||
nodeValue,
|
||||
command.PrettyPrint(task.DesiredState),
|
||||
command.PrettyPrint(task.Status.State),
|
||||
strings.ToLower(units.HumanDuration(time.Since(task.Status.Timestamp))),
|
||||
taskErr,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user