(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool)
| 57 | } |
| 58 | |
| 59 | func ContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { |
| 60 | globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 61 | if err != nil { |
| 62 | return nil, cobra.ShellCompDirectiveError |
| 63 | } |
| 64 | client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) |
| 65 | if err != nil { |
| 66 | return nil, cobra.ShellCompDirectiveError |
| 67 | } |
| 68 | defer cancel() |
| 69 | containers, err := client.Containers(ctx) |
| 70 | if err != nil { |
| 71 | return nil, cobra.ShellCompDirectiveError |
| 72 | } |
| 73 | getStatus := func(c containerd.Container) containerd.ProcessStatus { |
| 74 | ctx2, cancel2 := context.WithTimeout(ctx, 100*time.Millisecond) |
| 75 | defer cancel2() |
| 76 | task, err := c.Task(ctx2, nil) |
| 77 | if err != nil { |
| 78 | return containerd.Unknown |
| 79 | } |
| 80 | st, err := task.Status(ctx2) |
| 81 | if err != nil { |
| 82 | return containerd.Unknown |
| 83 | } |
| 84 | return st.Status |
| 85 | } |
| 86 | candidates := []string{} |
| 87 | for _, c := range containers { |
| 88 | if filterFunc != nil { |
| 89 | if !filterFunc(getStatus(c)) { |
| 90 | continue |
| 91 | } |
| 92 | } |
| 93 | lab, err := c.Labels(ctx) |
| 94 | if err != nil { |
| 95 | continue |
| 96 | } |
| 97 | name := lab[labels.Name] |
| 98 | if name != "" { |
| 99 | candidates = append(candidates, name) |
| 100 | continue |
| 101 | } |
| 102 | candidates = append(candidates, c.ID()) |
| 103 | } |
| 104 | return candidates, cobra.ShellCompDirectiveNoFileComp |
| 105 | } |
| 106 | |
| 107 | // NetworkNames includes {"bridge","host","none"} |
| 108 | func NetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { |
no test coverage detected
searching dependent graphs…