github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/utils.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"runtime/debug"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/pflag"
    11  )
    12  
    13  // print results from CLI command
    14  func printCmdResults(ok []string, failures map[string]error) error {
    15  	for _, id := range ok {
    16  		fmt.Println(id)
    17  	}
    18  
    19  	if len(failures) > 0 {
    20  		keys := reflect.ValueOf(failures).MapKeys()
    21  		lastKey := keys[len(keys)-1].String()
    22  		lastErr := failures[lastKey]
    23  		delete(failures, lastKey)
    24  
    25  		for _, err := range failures {
    26  			outputError(err)
    27  		}
    28  		return lastErr
    29  	}
    30  	return nil
    31  }
    32  
    33  // markFlagHiddenForRemoteClient makes the flag not appear as part of the CLI
    34  // on the remote-client
    35  func markFlagHiddenForRemoteClient(flagName string, flags *pflag.FlagSet) {
    36  	if remoteclient {
    37  		if err := flags.MarkHidden(flagName); err != nil {
    38  			debug.PrintStack()
    39  			logrus.Errorf("unable to mark %s as hidden in the remote-client", flagName)
    40  		}
    41  	}
    42  }
    43  
    44  // markFlagHidden is a helper function to log an error if marking
    45  // a flag as hidden happens to fail
    46  func markFlagHidden(flags *pflag.FlagSet, flag string) {
    47  	if err := flags.MarkHidden(flag); err != nil {
    48  		logrus.Errorf("unable to mark flag '%s' as hidden: %q", flag, err)
    49  	}
    50  }
    51  
    52  func aliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {
    53  	switch name {
    54  	case "healthcheck-command":
    55  		name = "health-cmd"
    56  	case "healthcheck-interval":
    57  		name = "health-interval"
    58  	case "healthcheck-retries":
    59  		name = "health-retries"
    60  	case "healthcheck-start-period":
    61  		name = "health-start-period"
    62  	case "healthcheck-timeout":
    63  		name = "health-timeout"
    64  	case "net":
    65  		name = "network"
    66  	case "timeout":
    67  		name = "time"
    68  	}
    69  	return pflag.NormalizedName(name)
    70  }
    71  
    72  // Check if a file exists and is not a directory
    73  func checkIfFileExists(name string) bool {
    74  	file, err := os.Stat(name)
    75  	// All errors return file == nil
    76  	if err != nil {
    77  		return false
    78  	}
    79  	return !file.IsDir()
    80  }
    81  
    82  // Check if a file is or is not a directory
    83  func fileIsDir(name string) bool {
    84  	file, err := os.Stat(name)
    85  	// All errors return file == nil
    86  	if err != nil {
    87  		return false
    88  	}
    89  	return file.IsDir()
    90  }