generateTaskNames generates names for the given tasks, and returns a copy of the slice with the 'Name' field set. Depending if the "--no-resolve" option is set, names have the following pattern: - ServiceName.Slot or ServiceID.Slot for tasks that are part of a replicated service - ServiceName.Node
(ctx context.Context, tasks client.TaskListResult, resolver *idresolver.IDResolver)
| 89 | // |
| 90 | // Task-names are not unique in cases where "tasks" contains previous/rotated tasks. |
| 91 | func generateTaskNames(ctx context.Context, tasks client.TaskListResult, resolver *idresolver.IDResolver) (client.TaskListResult, error) { |
| 92 | // Use a copy of the tasks list, to not modify the original slice |
| 93 | // see https://github.com/go101/go101/wiki/How-to-efficiently-clone-a-slice%3F |
| 94 | t := append(tasks.Items[:0:0], tasks.Items...) //nolint:gocritic // ignore appendAssign: append result not assigned to the same slice |
| 95 | |
| 96 | for i, task := range t { |
| 97 | serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID) |
| 98 | if err != nil { |
| 99 | return client.TaskListResult{}, err |
| 100 | } |
| 101 | if task.Slot != 0 { |
| 102 | t[i].Name = fmt.Sprintf("%v.%v", serviceName, task.Slot) |
| 103 | } else { |
| 104 | t[i].Name = fmt.Sprintf("%v.%v", serviceName, task.NodeID) |
| 105 | } |
| 106 | } |
| 107 | return client.TaskListResult{Items: t}, nil |
| 108 | } |
| 109 | |
| 110 | // DefaultFormat returns the default format from the config file, or table |
| 111 | // format if nothing is set in the config. |