github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/main_unix.go (about) 1 //go:build freebsd || linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package main 20 21 import ( 22 "github.com/containerd/log" 23 "github.com/containerd/nerdctl/pkg/clientutil" 24 "github.com/containerd/nerdctl/pkg/infoutil" 25 "github.com/containerd/nerdctl/pkg/rootlessutil" 26 "github.com/spf13/cobra" 27 ) 28 29 func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 30 globalOptions, err := processRootCmdFlags(cmd) 31 if err != nil { 32 return nil, cobra.ShellCompDirectiveError 33 } 34 if rootlessutil.IsRootlessParent() { 35 _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) 36 return nil, cobra.ShellCompDirectiveNoFileComp 37 } 38 if err != nil { 39 return nil, cobra.ShellCompDirectiveNoFileComp 40 } 41 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) 42 if err != nil { 43 return nil, cobra.ShellCompDirectiveError 44 } 45 defer cancel() 46 nsService := client.NamespaceService() 47 nsList, err := nsService.List(ctx) 48 if err != nil { 49 log.L.Warn(err) 50 return nil, cobra.ShellCompDirectiveError 51 } 52 var candidates []string 53 candidates = append(candidates, nsList...) 54 return candidates, cobra.ShellCompDirectiveNoFileComp 55 } 56 57 func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 58 globalOptions, err := processRootCmdFlags(cmd) 59 if err != nil { 60 return nil, cobra.ShellCompDirectiveError 61 } 62 if rootlessutil.IsRootlessParent() { 63 _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) 64 return nil, cobra.ShellCompDirectiveNoFileComp 65 } 66 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) 67 if err != nil { 68 return nil, cobra.ShellCompDirectiveError 69 } 70 defer cancel() 71 snapshotterPlugins, err := infoutil.GetSnapshotterNames(ctx, client.IntrospectionService()) 72 if err != nil { 73 return nil, cobra.ShellCompDirectiveError 74 } 75 var candidates []string 76 candidates = append(candidates, snapshotterPlugins...) 77 return candidates, cobra.ShellCompDirectiveNoFileComp 78 }