aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/parse/common.go
blob: b3aa88da2ca8c0297992db04c6e8f1c0a06e5f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package parse

import (
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

// TODO: the two functions here are almost identical. It may be worth looking
// into generalizing the two a bit more and share code but time is scarce and
// we only live once.

// CheckAllLatestAndCIDFile checks that --all and --latest are used correctly.
// If cidfile is set, also check for the --cidfile flag.
func CheckAllLatestAndCIDFile(c *cobra.Command, args []string, ignoreArgLen bool, cidfile bool) error {
	argLen := len(args)
	if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil {
		if !cidfile {
			return errors.New("unable to lookup values for 'latest' or 'all'")
		} else if c.Flags().Lookup("cidfile") == nil {
			return errors.New("unable to lookup values for 'latest', 'all' or 'cidfile'")
		}
	}

	specifiedAll, _ := c.Flags().GetBool("all")
	specifiedLatest, _ := c.Flags().GetBool("latest")
	specifiedCIDFile := false
	if cid, _ := c.Flags().GetStringArray("cidfile"); len(cid) > 0 {
		specifiedCIDFile = true
	}

	if specifiedCIDFile && (specifiedAll || specifiedLatest) {
		return errors.Errorf("--all, --latest and --cidfile cannot be used together")
	} else if specifiedAll && specifiedLatest {
		return errors.Errorf("--all and --latest cannot be used together")
	}

	if (argLen > 0) && specifiedAll {
		return errors.Errorf("no arguments are needed with --all")
	}

	if ignoreArgLen {
		return nil
	}

	if argLen > 0 {
		if specifiedLatest {
			return errors.Errorf("no arguments are needed with --latest")
		} else if cidfile && (specifiedLatest || specifiedCIDFile) {
			return errors.Errorf("no arguments are needed with --latest or --cidfile")
		}
	}

	if specifiedCIDFile {
		return nil
	}

	if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedCIDFile {
		return errors.Errorf("you must provide at least one name or id")
	}
	return nil
}

// CheckAllLatestAndPodIDFile checks that --all and --latest are used correctly.
// If withIDFile is set, also check for the --pod-id-file flag.
func CheckAllLatestAndPodIDFile(c *cobra.Command, args []string, ignoreArgLen bool, withIDFile bool) error {
	argLen := len(args)
	if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil {
		if !withIDFile {
			return errors.New("unable to lookup values for 'latest' or 'all'")
		} else if c.Flags().Lookup("pod-id-file") == nil {
			return errors.New("unable to lookup values for 'latest', 'all' or 'pod-id-file'")
		}
	}

	specifiedAll, _ := c.Flags().GetBool("all")
	specifiedLatest, _ := c.Flags().GetBool("latest")
	specifiedPodIDFile := false
	if pid, _ := c.Flags().GetStringArray("pod-id-file"); len(pid) > 0 {
		specifiedPodIDFile = true
	}

	if specifiedPodIDFile && (specifiedAll || specifiedLatest) {
		return errors.Errorf("--all, --latest and --pod-id-file cannot be used together")
	} else if specifiedAll && specifiedLatest {
		return errors.Errorf("--all and --latest cannot be used together")
	}

	if (argLen > 0) && specifiedAll {
		return errors.Errorf("no arguments are needed with --all")
	}

	if ignoreArgLen {
		return nil
	}

	if argLen > 0 {
		if specifiedLatest {
			return errors.Errorf("no arguments are needed with --latest")
		} else if withIDFile && (specifiedLatest || specifiedPodIDFile) {
			return errors.Errorf("no arguments are needed with --latest or --pod-id-file")
		}
	}

	if specifiedPodIDFile {
		return nil
	}

	if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedPodIDFile {
		return errors.Errorf("you must provide at least one name or id")
	}
	return nil
}