task.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package formatter
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/docker/distribution/reference"
  7. "github.com/docker/docker/api/types/swarm"
  8. "github.com/docker/docker/cli/command"
  9. "github.com/docker/docker/pkg/stringid"
  10. "github.com/docker/go-units"
  11. )
  12. const (
  13. defaultTaskTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Image}}\t{{.Node}}\t{{.DesiredState}}\t{{.CurrentState}}\t{{.Error}}\t{{.Ports}}"
  14. nodeHeader = "NODE"
  15. taskIDHeader = "ID"
  16. desiredStateHeader = "DESIRED STATE"
  17. currentStateHeader = "CURRENT STATE"
  18. errorHeader = "ERROR"
  19. maxErrLength = 30
  20. )
  21. // NewTaskFormat returns a Format for rendering using a task Context
  22. func NewTaskFormat(source string, quiet bool) Format {
  23. switch source {
  24. case TableFormatKey:
  25. if quiet {
  26. return defaultQuietFormat
  27. }
  28. return defaultTaskTableFormat
  29. case RawFormatKey:
  30. if quiet {
  31. return `id: {{.ID}}`
  32. }
  33. return `id: {{.ID}}\nname: {{.Name}}\nimage: {{.Image}}\nnode: {{.Node}}\ndesired_state: {{.DesiredState}}\ncurrent_state: {{.CurrentState}}\nerror: {{.Error}}\nports: {{.Ports}}\n`
  34. }
  35. return Format(source)
  36. }
  37. // TaskWrite writes the context
  38. func TaskWrite(ctx Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
  39. render := func(format func(subContext subContext) error) error {
  40. for _, task := range tasks {
  41. taskCtx := &taskContext{trunc: ctx.Trunc, task: task, name: names[task.ID], node: nodes[task.ID]}
  42. if err := format(taskCtx); err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. taskCtx := taskContext{}
  49. taskCtx.header = taskHeaderContext{
  50. "ID": taskIDHeader,
  51. "Name": nameHeader,
  52. "Image": imageHeader,
  53. "Node": nodeHeader,
  54. "DesiredState": desiredStateHeader,
  55. "CurrentState": currentStateHeader,
  56. "Error": errorHeader,
  57. "Ports": portsHeader,
  58. }
  59. return ctx.Write(&taskCtx, render)
  60. }
  61. type taskHeaderContext map[string]string
  62. type taskContext struct {
  63. HeaderContext
  64. trunc bool
  65. task swarm.Task
  66. name string
  67. node string
  68. }
  69. func (c *taskContext) MarshalJSON() ([]byte, error) {
  70. return marshalJSON(c)
  71. }
  72. func (c *taskContext) ID() string {
  73. if c.trunc {
  74. return stringid.TruncateID(c.task.ID)
  75. }
  76. return c.task.ID
  77. }
  78. func (c *taskContext) Name() string {
  79. return c.name
  80. }
  81. func (c *taskContext) Image() string {
  82. image := c.task.Spec.ContainerSpec.Image
  83. if c.trunc {
  84. ref, err := reference.ParseNormalizedNamed(image)
  85. if err == nil {
  86. // update image string for display, (strips any digest)
  87. if nt, ok := ref.(reference.NamedTagged); ok {
  88. if namedTagged, err := reference.WithTag(reference.TrimNamed(nt), nt.Tag()); err == nil {
  89. image = reference.FamiliarString(namedTagged)
  90. }
  91. }
  92. }
  93. }
  94. return image
  95. }
  96. func (c *taskContext) Node() string {
  97. return c.node
  98. }
  99. func (c *taskContext) DesiredState() string {
  100. return command.PrettyPrint(c.task.DesiredState)
  101. }
  102. func (c *taskContext) CurrentState() string {
  103. return fmt.Sprintf("%s %s ago",
  104. command.PrettyPrint(c.task.Status.State),
  105. strings.ToLower(units.HumanDuration(time.Since(c.task.Status.Timestamp))),
  106. )
  107. }
  108. func (c *taskContext) Error() string {
  109. // Trim and quote the error message.
  110. taskErr := c.task.Status.Err
  111. if c.trunc && len(taskErr) > maxErrLength {
  112. taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
  113. }
  114. if len(taskErr) > 0 {
  115. taskErr = fmt.Sprintf("\"%s\"", taskErr)
  116. }
  117. return taskErr
  118. }
  119. func (c *taskContext) Ports() string {
  120. if len(c.task.Status.PortStatus.Ports) == 0 {
  121. return ""
  122. }
  123. ports := []string{}
  124. for _, pConfig := range c.task.Status.PortStatus.Ports {
  125. ports = append(ports, fmt.Sprintf("*:%d->%d/%s",
  126. pConfig.PublishedPort,
  127. pConfig.TargetPort,
  128. pConfig.Protocol,
  129. ))
  130. }
  131. return strings.Join(ports, ",")
  132. }