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