forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
135 lines (112 loc) · 3.85 KB
/
cmd.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package util
import (
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery/registered"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/runtime"
)
// ErrExit is a marker interface for cli commands indicating that the response has been processed
var ErrExit = fmt.Errorf("exit directly")
func DefaultSubCommandRun(out io.Writer) func(c *cobra.Command, args []string) {
return func(c *cobra.Command, args []string) {
c.SetOutput(out)
if len(args) > 0 {
kcmdutil.CheckErr(kcmdutil.UsageError(c, fmt.Sprintf(`unknown command "%s"`, strings.Join(args, " "))))
}
c.Help()
}
}
// GetDisplayFilename returns the absolute path of the filename as long as there was no error, otherwise it returns the filename as-is
func GetDisplayFilename(filename string) string {
if absName, err := filepath.Abs(filename); err == nil {
return absName
}
return filename
}
// ResolveResource returns the resource type and name of the resourceString.
// If the resource string has no specified type, defaultResource will be returned.
func ResolveResource(defaultResource, resourceString string, mapper meta.RESTMapper) (string, string, error) {
if mapper == nil {
return "", "", errors.New("mapper cannot be nil")
}
var name string
parts := strings.Split(resourceString, "/")
switch len(parts) {
case 1:
name = parts[0]
case 2:
partialResource := unversioned.GroupVersionResource{Resource: parts[0]}
gvrs, err := mapper.ResourcesFor(partialResource)
if err != nil {
return "", "", err
}
if len(gvrs) == 0 {
return gvrs[0].Resource, parts[1], nil
}
groupResource := gvrs[0].GroupResource()
for _, gvr := range gvrs[1:] {
if groupResource != gvr.GroupResource() {
return "", "", &meta.AmbiguousResourceError{PartialResource: partialResource, MatchingResources: gvrs}
}
}
return gvrs[0].Resource, parts[1], nil
default:
return "", "", fmt.Errorf("invalid resource format: %s", resourceString)
}
return defaultResource, name, nil
}
// ConvertItemsForDisplay returns a new list that contains parallel elements that have been converted to the most preferred external version
func ConvertItemsForDisplay(objs []runtime.Object, preferredVersions ...unversioned.GroupVersion) ([]runtime.Object, error) {
ret := []runtime.Object{}
for i := range objs {
obj := objs[i]
kind, err := kapi.Scheme.ObjectKind(obj)
if err != nil {
return nil, err
}
groupMeta, err := registered.Group(kind.Group)
if err != nil {
return nil, err
}
requestedVersion := unversioned.GroupVersion{}
for _, preferredVersion := range preferredVersions {
if preferredVersion.Group == kind.Group {
requestedVersion = preferredVersion
break
}
}
actualOutputVersion := unversioned.GroupVersion{}
for _, externalVersion := range groupMeta.GroupVersions {
if externalVersion == requestedVersion {
actualOutputVersion = externalVersion
break
}
if actualOutputVersion.IsEmpty() {
actualOutputVersion = externalVersion
}
}
convertedObject, err := kapi.Scheme.ConvertToVersion(obj, actualOutputVersion.String())
if err != nil {
return nil, err
}
ret = append(ret, convertedObject)
}
return ret, nil
}
// ConvertItemsForDisplayFromDefaultCommand returns a new list that contains parallel elements that have been converted to the most preferred external version
func ConvertItemsForDisplayFromDefaultCommand(cmd *cobra.Command, objs []runtime.Object) ([]runtime.Object, error) {
requestedOutputVersionString := kcmdutil.GetFlagString(cmd, "output-version")
requestedOutputVersion, err := unversioned.ParseGroupVersion(requestedOutputVersionString)
if err != nil {
return nil, err
}
return ConvertItemsForDisplay(objs, requestedOutputVersion)
}