-
Notifications
You must be signed in to change notification settings - Fork 33
/
parse_args.go
49 lines (41 loc) · 1.27 KB
/
parse_args.go
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
package parse
import (
"strings"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/resources"
"github.com/sirupsen/logrus"
)
type UnmanagedFlagError string
func (u UnmanagedFlagError) Error() string {
return string(u)
}
func ParseFlagAndResources(cmdVerb string, cmdArgs []string) (resourceType resources.ResourceType, flagCompletion FlagCompletion, err error) {
resourceType = resources.ResourceTypeUnknown
flagCompletion = CheckFlagManaged(cmdArgs)
if flagCompletion == FlagUnmanaged {
logrus.Infof("Flag is unmanaged in %s, bailing out", cmdArgs)
err = UnmanagedFlagError(strings.Join(cmdArgs, " "))
return
}
logrus.Infof("Flag parsed: %s", flagCompletion.String())
if flagCompletion == FlagNamespace {
resourceType = resources.ResourceTypeNamespace
return
}
resourceType = resources.GetResourceType(cmdVerb, cmdArgs)
if resourceType == resources.ResourceTypeUnknown {
err = resources.UnknownResourceError{ResourceStr: strings.Join(cmdArgs, " ")}
return
}
return
}
func ParseNamespaceFromArgs(args []string) *string {
for k, arg := range args {
if (arg == "-n" || arg == "--namespace") && len(args) > k+1 && args[k+1] != " " {
return &args[k+1]
}
if strings.HasPrefix(arg, "--namespace=") {
return &strings.Split(arg, "=")[1]
}
}
return nil
}