Skip to content

Commit

Permalink
Issue 1529 - fix regression in help templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianofranz committed Apr 2, 2015
1 parent 1512217 commit f33f5c2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
4 changes: 4 additions & 0 deletions hack/test-cmd.sh
Expand Up @@ -205,6 +205,8 @@ echo "templates: ok"
# help for root commands must be consistent
[ "$(openshift | grep 'OpenShift Application Platform')" ]
[ "$(osc | grep 'OpenShift Client')" ]
[ "! $(osc | grep 'Options')" ]
[ "! $(osc | grep 'Global Options')" ]
[ "$(openshift cli | grep 'OpenShift Client')" ]
[ "$(openshift kubectl 2>&1 | grep 'Kubernetes cluster')" ]
[ "$(osadm 2>&1 | grep 'OpenShift Administrative Commands')" ]
Expand All @@ -213,6 +215,8 @@ echo "templates: ok"
# help for root commands with --help flag must be consistent
[ "$(openshift --help 2>&1 | grep 'OpenShift Application Platform')" ]
[ "$(osc --help 2>&1 | grep 'OpenShift Client')" ]
[ "$(osc login --help 2>&1 | grep 'Options')" ]
[ "! $(osc login --help 2>&1 | grep 'Global Options')" ]
[ "$(openshift cli --help 2>&1 | grep 'OpenShift Client')" ]
[ "$(openshift kubectl --help 2>&1 | grep 'Kubernetes cluster')" ]
[ "$(osadm --help 2>&1 | grep 'OpenShift Administrative Commands')" ]
Expand Down
41 changes: 22 additions & 19 deletions pkg/cmd/admin/admin.go
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/openshift/origin/pkg/cmd/cli/cmd"
"github.com/openshift/origin/pkg/cmd/experimental/buildchain"
"github.com/openshift/origin/pkg/cmd/experimental/config"
"github.com/openshift/origin/pkg/cmd/experimental/policy"
Expand All @@ -30,7 +31,7 @@ Note: This is a beta release of OpenShift and may change significantly. See

func NewCommandAdmin(name, fullName string, out io.Writer) *cobra.Command {
// Main command
cmd := &cobra.Command{
cmds := &cobra.Command{
Use: name,
Short: "tools for managing an OpenShift cluster",
Long: fmt.Sprintf(longDesc),
Expand All @@ -40,31 +41,33 @@ func NewCommandAdmin(name, fullName string, out io.Writer) *cobra.Command {
},
}

f := clientcmd.New(cmd.PersistentFlags())
f := clientcmd.New(cmds.PersistentFlags())

templates.UseAdminTemplates(cmd)
templates.UseAdminTemplates(cmds)

cmd.AddCommand(project.NewCmdNewProject(f, fullName, "new-project"))
cmd.AddCommand(policy.NewCommandPolicy(f, fullName, "policy"))
cmd.AddCommand(exrouter.NewCmdRouter(f, fullName, "router", out))
cmd.AddCommand(exregistry.NewCmdRegistry(f, fullName, "registry", out))
cmd.AddCommand(buildchain.NewCmdBuildChain(f, fullName, "build-chain"))
cmd.AddCommand(config.NewCmdConfig(fullName, "config"))
cmds.AddCommand(project.NewCmdNewProject(f, fullName, "new-project"))
cmds.AddCommand(policy.NewCommandPolicy(f, fullName, "policy"))
cmds.AddCommand(exrouter.NewCmdRouter(f, fullName, "router", out))
cmds.AddCommand(exregistry.NewCmdRegistry(f, fullName, "registry", out))
cmds.AddCommand(buildchain.NewCmdBuildChain(f, fullName, "build-chain"))
cmds.AddCommand(config.NewCmdConfig(fullName, "config"))

// TODO: these probably belong in a sub command
cmd.AddCommand(admin.NewCommandCreateKubeConfig(admin.CreateKubeConfigCommandName, fullName+" "+admin.CreateKubeConfigCommandName, out))
cmd.AddCommand(admin.NewCommandCreateBootstrapPolicyFile(admin.CreateBootstrapPolicyFileCommand, fullName+" "+admin.CreateBootstrapPolicyFileCommand, out))
cmd.AddCommand(admin.NewCommandOverwriteBootstrapPolicy(admin.OverwriteBootstrapPolicyCommandName, fullName+" "+admin.OverwriteBootstrapPolicyCommandName, fullName+" "+admin.CreateBootstrapPolicyFileCommand, out))
cmd.AddCommand(admin.NewCommandNodeConfig(admin.NodeConfigCommandName, fullName+" "+admin.NodeConfigCommandName, out))
cmds.AddCommand(admin.NewCommandCreateKubeConfig(admin.CreateKubeConfigCommandName, fullName+" "+admin.CreateKubeConfigCommandName, out))
cmds.AddCommand(admin.NewCommandCreateBootstrapPolicyFile(admin.CreateBootstrapPolicyFileCommand, fullName+" "+admin.CreateBootstrapPolicyFileCommand, out))
cmds.AddCommand(admin.NewCommandOverwriteBootstrapPolicy(admin.OverwriteBootstrapPolicyCommandName, fullName+" "+admin.OverwriteBootstrapPolicyCommandName, fullName+" "+admin.CreateBootstrapPolicyFileCommand, out))
cmds.AddCommand(admin.NewCommandNodeConfig(admin.NodeConfigCommandName, fullName+" "+admin.NodeConfigCommandName, out))
// TODO: these should be rolled up together
cmd.AddCommand(admin.NewCommandCreateMasterCerts(admin.CreateMasterCertsCommandName, fullName+" "+admin.CreateMasterCertsCommandName, out))
cmd.AddCommand(admin.NewCommandCreateClient(admin.CreateClientCommandName, fullName+" "+admin.CreateClientCommandName, out))
cmd.AddCommand(admin.NewCommandCreateServerCert(admin.CreateServerCertCommandName, fullName+" "+admin.CreateServerCertCommandName, out))
cmd.AddCommand(admin.NewCommandCreateSignerCert(admin.CreateSignerCertCommandName, fullName+" "+admin.CreateSignerCertCommandName, out))
cmds.AddCommand(admin.NewCommandCreateMasterCerts(admin.CreateMasterCertsCommandName, fullName+" "+admin.CreateMasterCertsCommandName, out))
cmds.AddCommand(admin.NewCommandCreateClient(admin.CreateClientCommandName, fullName+" "+admin.CreateClientCommandName, out))
cmds.AddCommand(admin.NewCommandCreateServerCert(admin.CreateServerCertCommandName, fullName+" "+admin.CreateServerCertCommandName, out))
cmds.AddCommand(admin.NewCommandCreateSignerCert(admin.CreateSignerCertCommandName, fullName+" "+admin.CreateSignerCertCommandName, out))

if name == fullName {
cmd.AddCommand(version.NewVersionCommand(fullName))
cmds.AddCommand(version.NewVersionCommand(fullName))
}

return cmd
cmds.AddCommand(cmd.NewCmdOptions(f, out))

return cmds
}
2 changes: 1 addition & 1 deletion pkg/cmd/cli/cli.go
Expand Up @@ -82,11 +82,11 @@ func NewCommandCLI(name, fullName string) *cobra.Command {
cmds.AddCommand(cmd.NewCmdExec(fullName, f, os.Stdin, out, os.Stderr))
cmds.AddCommand(cmd.NewCmdPortForward(fullName, f))
cmds.AddCommand(f.NewCmdProxy(out))
cmds.AddCommand(cmd.NewCmdOptions(f, out))
if name == fullName {
cmds.AddCommand(version.NewVersionCommand(fullName))
}
cmds.AddCommand(config.NewCmdConfig(fullName, "config"))
cmds.AddCommand(cmd.NewCmdOptions(f, out))

return cmds
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/templates/templater.go
Expand Up @@ -38,6 +38,15 @@ func (templater *Templater) UsageFunc() func(*cobra.Command) error {
}
return exposed
},
"flagsNotIntersected": func(l *flag.FlagSet, r *flag.FlagSet) *flag.FlagSet {
f := flag.NewFlagSet("notIntersected", flag.ContinueOnError)
l.VisitAll(func(flag *flag.Flag) {
if r.Lookup(flag.Name) == nil {
f.AddFlag(flag)
}
})
return f
},
"flagsUsages": func(f *flag.FlagSet) string {
x := new(bytes.Buffer)

Expand Down
28 changes: 15 additions & 13 deletions pkg/cmd/templates/templates.go
Expand Up @@ -44,7 +44,9 @@ func decorate(template string, trim bool) string {

const (
// TODO: $isRootCmd should be done in code, not in the template
funcs = `{{$isRootCmd := or (and (eq .Name "cli") (eq .Root.Name "openshift")) (eq .Name "osc") (and (eq .Name "admin") (eq .Root.Name "openshift")) (eq .Name "osadm")}}{{define "rootCli"}}{{if eq .Root.Name "osadm"}}osadm{{else}}{{if eq .Root.Name "osc"}}osc{{else}}openshift {{.Name}}{{end}}{{end}}{{end}}`
funcIsRootCmd = `{{$isRootCmd := or (and (eq .Name "cli") (eq .Root.Name "openshift")) (eq .Name "osc") (and (eq .Name "admin") (eq .Root.Name "openshift")) (eq .Name "osadm")}}`
funcRootCli = `{{define "rootCli"}}{{if eq .Root.Name "osadm"}}osadm{{else}}{{if eq .Root.Name "osc"}}osc{{else}}openshift {{.Name}}{{end}}{{end}}{{end}}`
funcs = funcIsRootCmd + funcRootCli

mainHelpTemplate = `{{.Long | trim}}
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
Expand All @@ -62,42 +64,42 @@ Available Commands: {{range .Commands}}{{if .Runnable}}
{{end}}
{{ if .HasLocalFlags}}Options:
{{flagsUsages .LocalFlags}}{{end}}
{{ if .HasAnyPersistentFlags}}Global Options:
{{flagsUsages .AllPersistentFlags}}{{end}}{{ if .HasSubCommands }}
{{ if .HasInheritedFlags}}Global Options:
{{flagsUsages .InheritedFlags}}{{end}}{{ if .HasSubCommands }}
Use "{{.Root.Name}} <command> --help" for more information about a given command.
{{end}}`

cliHelpTemplate = `{{.Long | trim}}
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

cliUsageTemplate = `{{ $cmd := . }}{{$exposedFlags := exposed .}}{{ if .HasSubCommands}}
cliUsageTemplate = `{{ $cmd := . }}{{$explicitlyExposedFlags := exposed .}}{{$localNotPersistentFlags := flagsNotIntersected .LocalFlags .PersistentFlags}}{{ if .HasSubCommands}}
Available Commands: {{range .Commands}}{{if .Runnable}}{{if ne .Name "options"}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}
{{end}}
{{ if or .HasLocalFlags $exposedFlags.HasFlags}}Options:
{{ if .HasLocalFlags}}{{flagsUsages .LocalFlags}}{{end}}{{ if $exposedFlags.HasFlags}}{{flagsUsages $exposedFlags}}{{end}}
{{ if or $localNotPersistentFlags.HasFlags $explicitlyExposedFlags.HasFlags}}Options:
{{ if $localNotPersistentFlags.HasFlags}}{{flagsUsages $localNotPersistentFlags}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{flagsUsages $explicitlyExposedFlags}}{{end}}
{{end}}{{ if not $isRootCmd}}Use "{{template "rootCli" .}} --help" for a list of all commands available in {{template "rootCli" .}}.
{{end}}{{ if .HasSubCommands }}Use "{{template "rootCli" .}} <command> --help" for more information about a given command.
{{end}}{{ if .HasAnyPersistentFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
{{end}}{{ if .HasInheritedFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
{{end}}`

adminHelpTemplate = `{{.Long | trim}}
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

adminUsageTemplate = `{{ $cmd := . }}{{$exposedFlags := exposed .}}{{ if .HasSubCommands}}
adminUsageTemplate = `{{ $cmd := . }}{{$explicitlyExposedFlags := exposed .}}{{$localNotPersistentFlags := flagsNotIntersected .LocalFlags .PersistentFlags}}{{ if .HasSubCommands}}
Available Commands: {{range .Commands}}{{if .Runnable}}{{if ne .Name "options"}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}
{{end}}
{{ if or .HasLocalFlags $exposedFlags.HasFlags}}Options:
{{ if .HasLocalFlags}}{{flagsUsages .LocalFlags}}{{end}}{{ if $exposedFlags.HasFlags}}{{flagsUsages $exposedFlags}}{{end}}
{{ if or $localNotPersistentFlags.HasFlags $explicitlyExposedFlags.HasFlags}}Options:
{{ if $localNotPersistentFlags.HasFlags}}{{flagsUsages $localNotPersistentFlags}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{flagsUsages $explicitlyExposedFlags}}{{end}}
{{end}}{{ if not $isRootCmd}}Use "{{template "rootCli" .}} --help" for a list of all commands available in {{template "rootCli" .}}.
{{end}}{{ if .HasSubCommands }}Use "{{template "rootCli" .}} <command> --help" for more information about a given command.
{{end}}{{ if .HasAnyPersistentFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
{{end}}{{ if .HasInheritedFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
{{end}}`

optionsHelpTemplate = ``

optionsUsageTemplate = `{{ if .HasAnyPersistentFlags}}The following options can be passed to any command:
optionsUsageTemplate = `{{ if .HasInheritedFlags}}The following options can be passed to any command:
{{flagsUsages .AllPersistentFlags}}{{end}}`
{{flagsUsages .InheritedFlags}}{{end}}`
)

0 comments on commit f33f5c2

Please sign in to comment.