github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/main_linux.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  	ncdefaults "github.com/containerd/nerdctl/pkg/defaults"
    21  	"github.com/containerd/nerdctl/pkg/rootlessutil"
    22  	"github.com/containerd/nerdctl/pkg/strutil"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool {
    27  	commands := []string{}
    28  	for tcmd := cmd; tcmd != nil; tcmd = tcmd.Parent() {
    29  		commands = append(commands, tcmd.Name())
    30  	}
    31  	commands = strutil.ReverseStrSlice(commands)
    32  
    33  	if !rootlessutil.IsRootlessParent() {
    34  		return false
    35  	}
    36  	if len(commands) < 2 {
    37  		return true
    38  	}
    39  	switch commands[1] {
    40  	// completion, login, logout, version: false, because it shouldn't require the daemon to be running
    41  	// apparmor: false, because it requires the initial mount namespace to access /sys/kernel/security
    42  	// cp, compose cp: false, because it requires the initial mount namespace to inspect file owners
    43  	case "", "completion", "login", "logout", "apparmor", "cp", "version":
    44  		return false
    45  	case "container":
    46  		if len(commands) < 3 {
    47  			return true
    48  		}
    49  		switch commands[2] {
    50  		case "cp":
    51  			return false
    52  		}
    53  	case "compose":
    54  		if len(commands) < 3 {
    55  			return true
    56  		}
    57  		switch commands[2] {
    58  		case "cp":
    59  			return false
    60  		}
    61  	}
    62  	return true
    63  }
    64  
    65  func shellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    66  	candidates := []string{"cgroupfs"}
    67  	if ncdefaults.IsSystemdAvailable() {
    68  		candidates = append(candidates, "systemd")
    69  	}
    70  	if rootlessutil.IsRootless() {
    71  		candidates = append(candidates, "none")
    72  	}
    73  	return candidates, cobra.ShellCompDirectiveNoFileComp
    74  }
    75  
    76  func addApparmorCommand(rootCmd *cobra.Command) {
    77  	rootCmd.AddCommand(newApparmorCommand())
    78  }
    79  
    80  func addCpCommand(rootCmd *cobra.Command) {
    81  	rootCmd.AddCommand(newCpCommand())
    82  }