forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
79 lines (66 loc) · 2.32 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
package util
import (
"errors"
"fmt"
"io"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var commaSepVarsPattern = regexp.MustCompile(".*=.*,.*=.*")
// ReplaceCommandName recursively processes the examples in a given command to change a hardcoded
// command name (like 'kubectl' to the appropriate target name). It returns c.
func ReplaceCommandName(from, to string, c *cobra.Command) *cobra.Command {
c.Example = strings.Replace(c.Example, from, to, -1)
for _, sub := range c.Commands() {
ReplaceCommandName(from, to, sub)
}
return c
}
// 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 schema.GroupResource, resourceString string, mapper meta.RESTMapper) (schema.GroupResource, string, error) {
if mapper == nil {
return schema.GroupResource{}, "", errors.New("mapper cannot be nil")
}
var name string
parts := strings.Split(resourceString, "/")
switch len(parts) {
case 1:
name = parts[0]
case 2:
name = parts[1]
// Allow specifying the group the same way kubectl does, as "resource.group.name"
groupResource := schema.ParseGroupResource(parts[0])
// normalize resource case
groupResource.Resource = strings.ToLower(groupResource.Resource)
gvr, err := mapper.ResourceFor(groupResource.WithVersion(""))
if err != nil {
return schema.GroupResource{}, "", err
}
return gvr.GroupResource(), name, nil
default:
return schema.GroupResource{}, "", fmt.Errorf("invalid resource format: %s", resourceString)
}
return defaultResource, name, nil
}
func WarnAboutCommaSeparation(errout io.Writer, values []string, flag string) {
if errout == nil {
return
}
for _, value := range values {
if commaSepVarsPattern.MatchString(value) {
fmt.Fprintf(errout, "warning: %s no longer accepts comma-separated lists of values. %q will be treated as a single key-value pair.\n", flag, value)
}
}
}