Skip to content

Commit 29bacc4

Browse files
Merge pull request #17536 from deads2k/oc-01-restrict
Automatic merge from submit-queue (batch tested with PRs 17536, 17512). prevent references from origin to oc Builds on #17516 and #17357 After this, oc can be deleted from origin during the next kube bump. @juanvallejo missed one. I hacked it in. You'll want to pretty it up eventually. @eparis @smarterclayton after this merges, I think we're good to branch 3.8.
2 parents 3d3b9c5 + c1c9636 commit 29bacc4

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

hack/import-restrictions.json

+16
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"

pkg/cmd/openshift/openshift.go

+84-3
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,84 @@ 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)
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

Comments
 (0)