1
1
package openshift
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
6
+ "io"
5
7
"os"
6
8
"runtime"
7
9
"strings"
8
10
9
11
"github.com/spf13/cobra"
12
+ "github.com/spf13/pflag"
10
13
14
+ kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
11
15
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
12
16
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
13
17
@@ -20,7 +24,6 @@ import (
20
24
"github.com/openshift/origin/pkg/cmd/templates"
21
25
cmdutil "github.com/openshift/origin/pkg/cmd/util"
22
26
cmdversion "github.com/openshift/origin/pkg/cmd/version"
23
- "github.com/openshift/origin/pkg/oc/cli/cmd"
24
27
osversion "github.com/openshift/origin/pkg/version/openshift"
25
28
)
26
29
@@ -95,7 +98,7 @@ func NewCommandOpenShift(name string) *cobra.Command {
95
98
root .AddCommand (startAllInOne )
96
99
root .AddCommand (newCompletionCommand ("completion" , name + " completion" ))
97
100
root .AddCommand (cmdversion .NewCmdVersion (name , osversion .Get (), os .Stdout ))
98
- root .AddCommand (cmd . NewCmdOptions ( out ))
101
+ root .AddCommand (newCmdOptions ( ))
99
102
100
103
// TODO: add groups
101
104
templates .ActsAsRootCommand (root , []string {"options" })
@@ -104,6 +107,83 @@ func NewCommandOpenShift(name string) *cobra.Command {
104
107
}
105
108
106
109
func newCompletionCommand (name , fullName string ) * cobra.Command {
107
- return cmd .NewCmdCompletion (fullName , os .Stdout )
110
+ return NewCmdCompletion (fullName , os .Stdout )
111
+ }
112
+
113
+ // newCmdOptions implements the OpenShift cli options command
114
+ func newCmdOptions () * cobra.Command {
115
+ cmd := & cobra.Command {
116
+ Use : "options" ,
117
+ Run : func (cmd * cobra.Command , args []string ) {
118
+ cmd .Usage ()
119
+ },
120
+ }
121
+
122
+ ktemplates .UseOptionsTemplates (cmd )
123
+
124
+ return cmd
125
+ }
126
+
127
+ // from here down probably deserves some common usage
128
+ var (
129
+ completionLong = ktemplates .LongDesc (`
130
+ This command prints shell code which must be evaluated to provide interactive
131
+ completion of %s commands.` )
132
+
133
+ completionExample = ktemplates .Examples (`
134
+ # Generate the %s completion code for bash
135
+ %s completion bash > bash_completion.sh
136
+ source bash_completion.sh
137
+
138
+ # The above example depends on the bash-completion framework.
139
+ # It must be sourced before sourcing the openshift cli completion,
140
+ # i.e. on the Mac:
141
+
142
+ brew install bash-completion
143
+ source $(brew --prefix)/etc/bash_completion
144
+ %s completion bash > bash_completion.sh
145
+ source bash_completion.sh
108
146
147
+ # In zsh*, the following will load openshift cli zsh completion:
148
+ source <(%s completion zsh)
149
+
150
+ * zsh completions are only supported in versions of zsh >= 5.2` )
151
+ )
152
+
153
+ func NewCmdCompletion (fullName string , out io.Writer ) * cobra.Command {
154
+ cmdHelpName := fullName
155
+
156
+ if strings .HasSuffix (fullName , "completion" ) {
157
+ cmdHelpName = "openshift"
158
+ }
159
+
160
+ cmd := kcmd .NewCmdCompletion (out , "\n " )
161
+ cmd .Long = fmt .Sprintf (completionLong , cmdHelpName )
162
+ cmd .Example = fmt .Sprintf (completionExample , cmdHelpName , cmdHelpName , cmdHelpName , cmdHelpName )
163
+ // mark all statically included flags as hidden to prevent them appearing in completions
164
+ cmd .PreRun = func (c * cobra.Command , _ []string ) {
165
+ pflag .CommandLine .VisitAll (func (flag * pflag.Flag ) {
166
+ flag .Hidden = true
167
+ })
168
+ hideGlobalFlags (c .Root (), flag .CommandLine )
169
+ }
170
+ return cmd
171
+ }
172
+
173
+ // hideGlobalFlags marks any flag that is in the global flag set as
174
+ // hidden to prevent completion from varying by platform due to conditional
175
+ // includes. This means that some completions will not be possible unless
176
+ // they are registered in cobra instead of being added to flag.CommandLine.
177
+ func hideGlobalFlags (c * cobra.Command , fs * flag.FlagSet ) {
178
+ fs .VisitAll (func (flag * flag.Flag ) {
179
+ if f := c .PersistentFlags ().Lookup (flag .Name ); f != nil {
180
+ f .Hidden = true
181
+ }
182
+ if f := c .LocalFlags ().Lookup (flag .Name ); f != nil {
183
+ f .Hidden = true
184
+ }
185
+ })
186
+ for _ , child := range c .Commands () {
187
+ hideGlobalFlags (child , fs )
188
+ }
109
189
}
0 commit comments