github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/parse/common.go (about)

     1  package parse
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"github.com/spf13/cobra"
     6  )
     7  
     8  // CheckAllLatestAndCIDFile checks that --all and --latest are used correctly.
     9  // If cidfile is set, also check for the --cidfile flag.
    10  func CheckAllLatestAndCIDFile(c *cobra.Command, args []string, ignoreArgLen bool, cidfile bool) error {
    11  	argLen := len(args)
    12  	if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil {
    13  		if !cidfile {
    14  			return errors.New("unable to lookup values for 'latest' or 'all'")
    15  		} else if c.Flags().Lookup("cidfile") == nil {
    16  			return errors.New("unable to lookup values for 'latest', 'all' or 'cidfile'")
    17  		}
    18  	}
    19  
    20  	specifiedAll, _ := c.Flags().GetBool("all")
    21  	specifiedLatest, _ := c.Flags().GetBool("latest")
    22  	specifiedCIDFile := false
    23  	if cid, _ := c.Flags().GetStringArray("cidfile"); len(cid) > 0 {
    24  		specifiedCIDFile = true
    25  	}
    26  
    27  	if specifiedCIDFile && (specifiedAll || specifiedLatest) {
    28  		return errors.Errorf("--all, --latest and --cidfile cannot be used together")
    29  	} else if specifiedAll && specifiedLatest {
    30  		return errors.Errorf("--all and --latest cannot be used together")
    31  	}
    32  
    33  	if ignoreArgLen {
    34  		return nil
    35  	}
    36  	if (argLen > 0) && (specifiedAll || specifiedLatest) {
    37  		return errors.Errorf("no arguments are needed with --all or --latest")
    38  	} else if cidfile && (argLen > 0) && (specifiedAll || specifiedLatest || specifiedCIDFile) {
    39  		return errors.Errorf("no arguments are needed with --all, --latest or --cidfile")
    40  	}
    41  
    42  	if specifiedCIDFile {
    43  		return nil
    44  	}
    45  
    46  	if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedCIDFile {
    47  		return errors.Errorf("you must provide at least one name or id")
    48  	}
    49  	return nil
    50  }