mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
f8c7c921d9
This fix tries to address the issue raised in 27189 where it is not possible to support configured formatting stored in config.json. Since `--format` was not supported in `docker service ps`, the flag `--format` has also been added in this fix. This fix 1. Add `--format` to `docker service ps` 2. Add `tasksFormat` to config.json 3. Add `--format` to `docker stack ps` 4. Add `--format` to `docker node ps` The related docs has been updated. An integration test has been added. This fix fixes 27189. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package task
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/docker/docker/cli/command/formatter"
|
|
"github.com/docker/docker/cli/command/idresolver"
|
|
)
|
|
|
|
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 format.
|
|
// Besides this, command `docker node ps <node>`
|
|
// and `docker stack ps` will call this, too.
|
|
func Print(dockerCli command.Cli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, trunc, quiet bool, format string) error {
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
names := map[string]string{}
|
|
nodes := map[string]string{}
|
|
|
|
tasksCtx := formatter.Context{
|
|
Output: dockerCli.Out(),
|
|
Format: formatter.NewTaskFormat(format, quiet),
|
|
Trunc: trunc,
|
|
}
|
|
|
|
prevName := ""
|
|
for _, task := range tasks {
|
|
serviceName, 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 := ""
|
|
if task.Slot != 0 {
|
|
name = fmt.Sprintf("%v.%v", serviceName, task.Slot)
|
|
} else {
|
|
name = fmt.Sprintf("%v.%v", serviceName, task.NodeID)
|
|
}
|
|
|
|
// Indent the name if necessary
|
|
indentedName := name
|
|
if name == prevName {
|
|
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
|
}
|
|
prevName = name
|
|
|
|
names[task.ID] = name
|
|
if tasksCtx.Format.IsTable() {
|
|
names[task.ID] = indentedName
|
|
}
|
|
nodes[task.ID] = nodeValue
|
|
}
|
|
|
|
return formatter.TaskWrite(tasksCtx, tasks, names, nodes)
|
|
}
|