Skip to content

Commit 8a52daa

Browse files
Merge pull request #17869 from juanvallejo/jvallejo/pick-pr-17536-2nd-attempt
Automatic merge from submit-queue. Pick 17536: prevent references from origin to oc Picks PR #17536 which brings in the last patch needed to eliminate dependencies from origin on the `pkg/oc` subtree. Re-opened from #17566 cc @deads2k
2 parents 18bf801 + 55a44d8 commit 8a52daa

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

hack/import-restrictions.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
]
1717
},
1818

19+
{
20+
"checkedPackageRoots": [
21+
"github.com/openshift/origin/pkg"
22+
],
23+
"ignoredSubTrees": [
24+
"github.com/openshift/origin/pkg/oc"
25+
],
26+
"forbiddenImportPackageRoots": [
27+
"github.com/openshift/origin/pkg/oc"
28+
],
29+
"allowedImportPackageRoots": [
30+
"vendor",
31+
""
32+
]
33+
},
34+
1935
{
2036
"checkedPackageRoots": [
2137
"github.com/openshift/origin/pkg"
@@ -33,7 +49,6 @@
3349
]
3450
},
3551

36-
3752
{
3853
"checkedPackageRoots": [
3954
"github.com/openshift/origin/pkg"

pkg/cmd/openshift/openshift.go

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package openshift
22

33
import (
4+
"flag"
45
"fmt"
6+
"io"
57
"os"
68
"runtime"
79
"strings"
810

911
"github.com/spf13/cobra"
12+
"github.com/spf13/pflag"
1013

14+
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
1115
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1216
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1317

@@ -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,83 @@ func NewCommandOpenShift(name string) *cobra.Command {
104107
}
105108

106109
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
108146
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+
}
109189
}

0 commit comments

Comments
 (0)