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