File: list.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (82 lines) | stat: -rw-r--r-- 1,776 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package task

import (
	"errors"
	"fmt"
	"os"
	"text/tabwriter"

	"github.com/docker/swarmkit/api"
	"github.com/docker/swarmkit/cmd/swarmctl/common"
	"github.com/spf13/cobra"
)

var (
	listCmd = &cobra.Command{
		Use:   "ls",
		Short: "List tasks",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 0 {
				return errors.New("ls command takes no arguments")
			}

			flags := cmd.Flags()

			all, err := flags.GetBool("all")
			if err != nil {
				return err
			}

			quiet, err := flags.GetBool("quiet")
			if err != nil {
				return err
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}
			r, err := c.ListTasks(common.Context(cmd), &api.ListTasksRequest{})
			if err != nil {
				return err
			}
			res := common.NewResolver(cmd, c)

			var output func(t *api.Task)

			if !quiet {
				w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
				defer func() {
					// Ignore flushing errors - there's nothing we can do.
					_ = w.Flush()
				}()
				common.PrintHeader(w, "ID", "Service", "Desired State", "Last State", "Node")
				output = func(t *api.Task) {
					fmt.Fprintf(w, "%s\t%s.%d\t%s\t%s %s\t%s\n",
						t.ID,
						res.Resolve(api.Service{}, t.ServiceID),
						t.Slot,
						t.DesiredState.String(),
						t.Status.State.String(),
						common.TimestampAgo(t.Status.Timestamp),
						res.Resolve(api.Node{}, t.NodeID),
					)
				}
			} else {
				output = func(t *api.Task) { fmt.Println(t.ID) }
			}

			for _, t := range r.Tasks {
				if all || t.DesiredState <= api.TaskStateRunning {
					output(t)
				}
			}
			return nil
		},
	}
)

func init() {
	listCmd.Flags().BoolP("all", "a", false, "Show all tasks (default shows just running)")
	listCmd.Flags().BoolP("quiet", "q", false, "Only display IDs")
}