github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/completion.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/containerd/containerd" 24 "github.com/containerd/nerdctl/pkg/clientutil" 25 "github.com/containerd/nerdctl/pkg/labels" 26 "github.com/containerd/nerdctl/pkg/netutil" 27 "github.com/spf13/cobra" 28 ) 29 30 func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { 31 globalOptions, err := processRootCmdFlags(cmd) 32 if err != nil { 33 return nil, cobra.ShellCompDirectiveError 34 } 35 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) 36 if err != nil { 37 return nil, cobra.ShellCompDirectiveError 38 } 39 defer cancel() 40 41 imageList, err := client.ImageService().List(ctx, "") 42 43 if err != nil { 44 return nil, cobra.ShellCompDirectiveError 45 } 46 candidates := []string{} 47 for _, img := range imageList { 48 candidates = append(candidates, img.Name) 49 } 50 return candidates, cobra.ShellCompDirectiveNoFileComp 51 } 52 53 func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { 54 globalOptions, err := processRootCmdFlags(cmd) 55 if err != nil { 56 return nil, cobra.ShellCompDirectiveError 57 } 58 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) 59 if err != nil { 60 return nil, cobra.ShellCompDirectiveError 61 } 62 defer cancel() 63 containers, err := client.Containers(ctx) 64 if err != nil { 65 return nil, cobra.ShellCompDirectiveError 66 } 67 getStatus := func(c containerd.Container) containerd.ProcessStatus { 68 ctx2, cancel2 := context.WithTimeout(ctx, 100*time.Millisecond) 69 defer cancel2() 70 task, err := c.Task(ctx2, nil) 71 if err != nil { 72 return containerd.Unknown 73 } 74 st, err := task.Status(ctx2) 75 if err != nil { 76 return containerd.Unknown 77 } 78 return st.Status 79 } 80 candidates := []string{} 81 for _, c := range containers { 82 if filterFunc != nil { 83 if !filterFunc(getStatus(c)) { 84 continue 85 } 86 } 87 lab, err := c.Labels(ctx) 88 if err != nil { 89 continue 90 } 91 name := lab[labels.Name] 92 if name != "" { 93 candidates = append(candidates, name) 94 continue 95 } 96 candidates = append(candidates, c.ID()) 97 } 98 return candidates, cobra.ShellCompDirectiveNoFileComp 99 } 100 101 // shellCompleteNetworkNames includes {"bridge","host","none"} 102 func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { 103 globalOptions, err := processRootCmdFlags(cmd) 104 if err != nil { 105 return nil, cobra.ShellCompDirectiveError 106 } 107 excludeMap := make(map[string]struct{}, len(exclude)) 108 for _, ex := range exclude { 109 excludeMap[ex] = struct{}{} 110 } 111 112 e, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath) 113 if err != nil { 114 return nil, cobra.ShellCompDirectiveError 115 } 116 candidates := []string{} 117 netConfigs, err := e.NetworkMap() 118 if err != nil { 119 return nil, cobra.ShellCompDirectiveError 120 } 121 for netName := range netConfigs { 122 if _, ok := excludeMap[netName]; !ok { 123 candidates = append(candidates, netName) 124 } 125 } 126 for _, s := range []string{"host", "none"} { 127 if _, ok := excludeMap[s]; !ok { 128 candidates = append(candidates, s) 129 } 130 } 131 return candidates, cobra.ShellCompDirectiveNoFileComp 132 } 133 134 func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { 135 globalOptions, err := processRootCmdFlags(cmd) 136 if err != nil { 137 return nil, cobra.ShellCompDirectiveError 138 } 139 vols, err := getVolumes(cmd, globalOptions) 140 if err != nil { 141 return nil, cobra.ShellCompDirectiveError 142 } 143 candidates := []string{} 144 for _, v := range vols { 145 candidates = append(candidates, v.Name) 146 } 147 return candidates, cobra.ShellCompDirectiveNoFileComp 148 } 149 150 func shellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 151 candidates := []string{ 152 "amd64", 153 "arm64", 154 "riscv64", 155 "ppc64le", 156 "s390x", 157 "386", 158 "arm", // alias of "linux/arm/v7" 159 "linux/arm/v6", // "arm/v6" is invalid (interpreted as OS="arm", Arch="v7") 160 } 161 return candidates, cobra.ShellCompDirectiveNoFileComp 162 }