allow setting root command config
This commit is contained in:
parent
26e00210aa
commit
4eec8e55a1
@ -82,9 +82,9 @@ var TemplateFuncs = template.FuncMap{
|
||||
// TemplateCommandHelp holds a template for rendering command help.
|
||||
var TemplateCommandHelp *template.Template
|
||||
|
||||
func HelpTemplate() *template.Template {
|
||||
func HelpTemplate(executableName string) *template.Template {
|
||||
if TemplateCommandHelp == nil {
|
||||
TemplateCommandHelp = template.Must(template.New("help").Funcs(TemplateFuncs).Parse(helpTemplateText))
|
||||
TemplateCommandHelp = template.Must(template.New("help").Funcs(TemplateFuncs).Parse(strings.ReplaceAll(helpTemplateText, "@chinampa@", executableName)))
|
||||
}
|
||||
|
||||
return TemplateCommandHelp
|
||||
|
@ -1,5 +1,5 @@
|
||||
{{- if not .HTMLOutput }}
|
||||
# {{ if and (not (eq .Spec.FullName "joao")) (not (eq .Command.Name "help")) }}joao {{ end }}{{ .Spec.FullName }}{{if eq .Command.Name "help"}} help{{end}}
|
||||
# {{ if and (not .Spec.IsRoot) (not (eq .Command.Name "help")) }}@chinampa@ {{ end }}{{ .Spec.FullName }}{{if eq .Command.Name "help"}} help{{end}}
|
||||
{{- else }}
|
||||
---
|
||||
description: {{ .Command.Short }}
|
||||
@ -10,7 +10,7 @@ description: {{ .Command.Short }}
|
||||
|
||||
## Usage
|
||||
|
||||
﹅{{ replace .Command.UseLine " [flags]" "" }}{{if .Command.HasAvailableSubCommands}} SUBCOMMAND{{end}}﹅
|
||||
`{{ replace .Command.UseLine " [flags]" "" }}{{if .Command.HasAvailableSubCommands}} SUBCOMMAND{{end}}`
|
||||
|
||||
{{ if .Command.HasAvailableSubCommands -}}
|
||||
## Subcommands
|
||||
@ -37,7 +37,7 @@ description: {{ .Command.Short }}
|
||||
{{- end -}}
|
||||
|
||||
|
||||
{{ if and (eq .Spec.FullName "joao") (not (eq .Command.Name "help")) }}
|
||||
{{ if and .Spec.IsRoot (not (eq .Command.Name "help")) }}
|
||||
## Description
|
||||
|
||||
{{ .Spec.Description }}
|
||||
@ -55,7 +55,7 @@ description: {{ .Command.Short }}
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if not (eq .Spec.FullName "joao") }}
|
||||
{{- if not .Spec.IsRoot }}
|
||||
## Description
|
||||
|
||||
{{ if not (eq .Command.Long "") }}{{ .Command.Long }}{{ else }}{{ .Spec.Description }}{{end}}
|
||||
|
@ -25,11 +25,14 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var ccRoot = &cobra.Command{
|
||||
Use: "joao [--silent|-v|--verbose] [--[no-]color] [-h|--help] [--version]",
|
||||
func newCobraRoot(root *command.Command) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: root.Name() + " [--silent|-v|--verbose] [--[no-]color] [-h|--help] [--version]",
|
||||
Annotations: map[string]string{
|
||||
_c.ContextKeyRuntimeIndex: "joao",
|
||||
_c.ContextKeyRuntimeIndex: root.Name(),
|
||||
},
|
||||
Short: root.Summary,
|
||||
Long: root.Description,
|
||||
DisableAutoGenTag: true,
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
@ -63,6 +66,7 @@ var ccRoot = &cobra.Command{
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func toCobra(cmd *command.Command, globalOptions command.Options) *cobra.Command {
|
||||
localName := cmd.Name()
|
||||
|
@ -134,8 +134,7 @@ func ChildrenNames() []string {
|
||||
|
||||
func Execute(version string) error {
|
||||
cmdRoot := command.Root
|
||||
ccRoot.Short = cmdRoot.Summary
|
||||
ccRoot.Long = cmdRoot.Description
|
||||
ccRoot := newCobraRoot(command.Root)
|
||||
ccRoot.Annotations["version"] = version
|
||||
ccRoot.CompletionOptions.HiddenDefaultCmd = true
|
||||
ccRoot.Flags().AddFlagSet(cmdRoot.FlagSet())
|
||||
@ -153,7 +152,7 @@ func Execute(version string) error {
|
||||
container := ccRoot
|
||||
for idx, cp := range cmd.Path {
|
||||
if idx == len(cmd.Path)-1 {
|
||||
// logrus.Debugf("adding command %s to %s", leaf.Name(), cmd.Path[0:idx])
|
||||
logrus.Debugf("adding command %s to %s", leaf.Name(), container.Name())
|
||||
container.AddCommand(leaf)
|
||||
break
|
||||
}
|
||||
|
18
main.go
18
main.go
@ -15,12 +15,24 @@ package chinampa
|
||||
import (
|
||||
"git.rob.mx/nidito/chinampa/internal/registry"
|
||||
"git.rob.mx/nidito/chinampa/pkg/command"
|
||||
"git.rob.mx/nidito/chinampa/pkg/runtime"
|
||||
)
|
||||
|
||||
func Register(cmd *command.Command) {
|
||||
registry.Register(cmd)
|
||||
registry.Register(cmd.SetBindings())
|
||||
}
|
||||
|
||||
func Execute(version string) error {
|
||||
return registry.Execute(version)
|
||||
type Config struct {
|
||||
Name string
|
||||
Version string
|
||||
Summary string
|
||||
Description string
|
||||
}
|
||||
|
||||
func Execute(config Config) error {
|
||||
runtime.Executable = config.Name
|
||||
command.Root.Summary = config.Summary
|
||||
command.Root.Description = config.Description
|
||||
command.Root.Path = []string{runtime.Executable}
|
||||
return registry.Execute(config.Version)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.rob.mx/nidito/chinampa/internal/errors"
|
||||
"git.rob.mx/nidito/chinampa/pkg/runtime"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
@ -42,6 +43,10 @@ type Command struct {
|
||||
Cobra *cobra.Command
|
||||
}
|
||||
|
||||
func (cmd *Command) IsRoot() bool {
|
||||
return cmd.FullName() == runtime.Executable
|
||||
}
|
||||
|
||||
func (cmd *Command) SetBindings() *Command {
|
||||
ptr := cmd
|
||||
for _, opt := range cmd.Options {
|
||||
@ -114,7 +119,7 @@ func (cmd *Command) ParseInput(cc *cobra.Command, args []string) error {
|
||||
|
||||
logrus.Debug("Validating flags")
|
||||
if err := cmd.Options.AreValid(); err != nil {
|
||||
logrus.Debug("Invalid flags for %s: %w", cmd.FullName(), err)
|
||||
logrus.Debugf("Invalid flags for %s: %w", cmd.FullName(), err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ func (cmd *Command) ShowHelp(globalOptions Options, args []string) ([]byte, erro
|
||||
GlobalOptions: globalOptions,
|
||||
HTMLOutput: runtime.UnstyledHelpEnabled(),
|
||||
}
|
||||
err := _c.HelpTemplate().Execute(&buf, c)
|
||||
err := _c.HelpTemplate(runtime.Executable).Execute(&buf, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ import (
|
||||
)
|
||||
|
||||
var Root = &Command{
|
||||
Summary: "Helps organize config for roberto",
|
||||
Description: `﹅joao﹅ makes yaml, json, 1password and vault play along nicely.`,
|
||||
Path: []string{"joao"},
|
||||
Summary: "Replace me with chinampa.Configure",
|
||||
Description: "",
|
||||
Path: []string{runtime.Executable},
|
||||
Options: Options{
|
||||
_c.HelpCommandName: &Option{
|
||||
ShortName: "h",
|
||||
|
@ -19,6 +19,8 @@ import (
|
||||
_c "git.rob.mx/nidito/chinampa/internal/constants"
|
||||
)
|
||||
|
||||
var Executable = "chinampa"
|
||||
|
||||
var falseIshValues = []string{
|
||||
"",
|
||||
"0",
|
||||
|
Loading…
Reference in New Issue
Block a user