forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenshift.go
163 lines (131 loc) · 4.58 KB
/
openshift.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package openshift
import (
"flag"
"fmt"
"io"
"os"
"runtime"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/wait"
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/cmd/flagtypes"
"github.com/openshift/origin/pkg/cmd/server/start"
"github.com/openshift/origin/pkg/cmd/templates"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
cmdversion "github.com/openshift/origin/pkg/cmd/version"
osversion "github.com/openshift/origin/pkg/version"
)
var (
openshiftLong = ktemplates.LongDesc(`
%[2]s
The %[3]s helps you build, deploy, and manage containerized applications.`)
)
// CommandFor returns the appropriate command for this base name,
// or the global OpenShift command
func CommandFor(basename string) *cobra.Command {
var cmd *cobra.Command
// Make case-insensitive and strip executable suffix if present
if runtime.GOOS == "windows" {
basename = strings.ToLower(basename)
basename = strings.TrimSuffix(basename, ".exe")
}
switch basename {
default:
cmd = NewCommandOpenShift("openshift")
}
if cmd.UsageFunc() == nil {
templates.ActsAsRootCommand(cmd, []string{"options"})
}
flagtypes.GLog(cmd.PersistentFlags())
return cmd
}
// NewCommandOpenShift creates the standard OpenShift command
func NewCommandOpenShift(name string) *cobra.Command {
out, errout := os.Stdout, os.Stderr
root := &cobra.Command{
Use: name,
Short: "Build, deploy, and manage your cloud applications",
Long: fmt.Sprintf(openshiftLong, name, cmdutil.GetPlatformName(name), cmdutil.GetDistributionName(name)),
Run: kcmdutil.DefaultSubCommandRun(out),
}
root.AddCommand(start.NewCommandStart(name, out, errout, wait.NeverStop))
root.AddCommand(newCompletionCommand("completion", name+" completion"))
root.AddCommand(cmdversion.NewCmdVersion(name, osversion.Get(), os.Stdout))
root.AddCommand(newCmdOptions())
// TODO: add groups
templates.ActsAsRootCommand(root, []string{"options"})
return root
}
func newCompletionCommand(name, fullName string) *cobra.Command {
return NewCmdCompletion(fullName, os.Stdout)
}
// newCmdOptions implements the OpenShift cli options command
func newCmdOptions() *cobra.Command {
cmd := &cobra.Command{
Use: "options",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
ktemplates.UseOptionsTemplates(cmd)
return cmd
}
// from here down probably deserves some common usage
var (
completionLong = ktemplates.LongDesc(`
This command prints shell code which must be evaluated to provide interactive
completion of %s commands.`)
completionExample = ktemplates.Examples(`
# Generate the %s completion code for bash
%s completion bash > bash_completion.sh
source bash_completion.sh
# The above example depends on the bash-completion framework.
# It must be sourced before sourcing the openshift cli completion,
# i.e. on the Mac:
brew install bash-completion
source $(brew --prefix)/etc/bash_completion
%s completion bash > bash_completion.sh
source bash_completion.sh
# In zsh*, the following will load openshift cli zsh completion:
source <(%s completion zsh)
* zsh completions are only supported in versions of zsh >= 5.2`)
)
// NewCmdCompletion creates a completion command.
func NewCmdCompletion(fullName string, out io.Writer) *cobra.Command {
cmdHelpName := fullName
if strings.HasSuffix(fullName, "completion") {
cmdHelpName = "openshift"
}
cmd := kcmd.NewCmdCompletion(out, "\n")
cmd.Long = fmt.Sprintf(completionLong, cmdHelpName)
cmd.Example = fmt.Sprintf(completionExample, cmdHelpName, cmdHelpName, cmdHelpName, cmdHelpName)
// mark all statically included flags as hidden to prevent them appearing in completions
cmd.PreRun = func(c *cobra.Command, _ []string) {
pflag.CommandLine.VisitAll(func(flag *pflag.Flag) {
flag.Hidden = true
})
hideGlobalFlags(c.Root(), flag.CommandLine)
}
return cmd
}
// hideGlobalFlags marks any flag that is in the global flag set as
// hidden to prevent completion from varying by platform due to conditional
// includes. This means that some completions will not be possible unless
// they are registered in cobra instead of being added to flag.CommandLine.
func hideGlobalFlags(c *cobra.Command, fs *flag.FlagSet) {
fs.VisitAll(func(flag *flag.Flag) {
if f := c.PersistentFlags().Lookup(flag.Name); f != nil {
f.Hidden = true
}
if f := c.LocalFlags().Lookup(flag.Name); f != nil {
f.Hidden = true
}
})
for _, child := range c.Commands() {
hideGlobalFlags(child, fs)
}
}