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,43 +25,47 @@ 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{
|
||||||
Annotations: map[string]string{
|
Use: root.Name() + " [--silent|-v|--verbose] [--[no-]color] [-h|--help] [--version]",
|
||||||
_c.ContextKeyRuntimeIndex: "joao",
|
Annotations: map[string]string{
|
||||||
},
|
_c.ContextKeyRuntimeIndex: root.Name(),
|
||||||
DisableAutoGenTag: true,
|
},
|
||||||
SilenceUsage: true,
|
Short: root.Summary,
|
||||||
SilenceErrors: true,
|
Long: root.Description,
|
||||||
ValidArgs: []string{""},
|
DisableAutoGenTag: true,
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
SilenceUsage: true,
|
||||||
err := cobra.OnlyValidArgs(cmd, args)
|
SilenceErrors: true,
|
||||||
if err != nil {
|
ValidArgs: []string{""},
|
||||||
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
|
err := cobra.OnlyValidArgs(cmd, args)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
suggestions := []string{}
|
suggestions := []string{}
|
||||||
bold := color.New(color.Bold)
|
bold := color.New(color.Bold)
|
||||||
for _, l := range cmd.SuggestionsFor(args[len(args)-1]) {
|
for _, l := range cmd.SuggestionsFor(args[len(args)-1]) {
|
||||||
suggestions = append(suggestions, bold.Sprint(l))
|
suggestions = append(suggestions, bold.Sprint(l))
|
||||||
|
}
|
||||||
|
errMessage := fmt.Sprintf("Unknown subcommand %s", bold.Sprint(strings.Join(args, " ")))
|
||||||
|
if len(suggestions) > 0 {
|
||||||
|
errMessage += ". Perhaps you meant " + strings.Join(suggestions, ", ") + "?"
|
||||||
|
}
|
||||||
|
return errors.NotFound{Msg: errMessage, Group: []string{}}
|
||||||
}
|
}
|
||||||
errMessage := fmt.Sprintf("Unknown subcommand %s", bold.Sprint(strings.Join(args, " ")))
|
return nil
|
||||||
if len(suggestions) > 0 {
|
},
|
||||||
errMessage += ". Perhaps you meant " + strings.Join(suggestions, ", ") + "?"
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
if ok, err := cmd.Flags().GetBool("version"); err == nil && ok {
|
||||||
|
_, err := cmd.OutOrStdout().Write([]byte(cmd.Root().Annotations["version"]))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return errors.NotFound{Msg: "No subcommand provided", Group: []string{}}
|
||||||
}
|
}
|
||||||
return errors.NotFound{Msg: errMessage, Group: []string{}}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if len(args) == 0 {
|
|
||||||
if ok, err := cmd.Flags().GetBool("version"); err == nil && ok {
|
|
||||||
_, err := cmd.OutOrStdout().Write([]byte(cmd.Root().Annotations["version"]))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return errors.NotFound{Msg: "No subcommand provided", Group: []string{}}
|
|
||||||
}
|
|
||||||
|
|
||||||
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