add doc command

This commit is contained in:
Samuel Lorch 2021-09-09 09:19:33 +02:00
parent 03852b0bca
commit 08cb902aad
5 changed files with 112 additions and 2 deletions

71
cmd/completion.go Normal file
View file

@ -0,0 +1,71 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Hidden: true,
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: `To load completions:
Bash:
$ source <(yourprogram completion bash)
# To load completions for each session, execute once:
# Linux:
$ yourprogram completion bash > /etc/bash_completion.d/yourprogram
# macOS:
$ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ yourprogram completion zsh > "${fpath[1]}/_yourprogram"
# You will need to start a new shell for this setup to take effect.
fish:
$ yourprogram completion fish | source
# To load completions for each session, execute once:
$ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish
PowerShell:
PS> yourprogram completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> yourprogram completion powershell > yourprogram.ps1
# and source this file from your PowerShell profile.
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
},
}
func init() {
rootCmd.AddCommand(completionCmd)
}

View file

@ -10,8 +10,8 @@ import (
// configureCmd represents the configure command
var configureCmd = &cobra.Command{
Use: "configure",
Short: "configure saves the provided global flags to the config file",
Long: `configure saves the provided global flags to the config file.
Short: "Configure saves the provided global flags to the Config File",
Long: `Configure saves the provided global flags to the Config File.
this makes using the cli easier as they don't have to be specifed all the time.`,
RunE: func(cmd *cobra.Command, args []string) error {

34
cmd/doc.go Normal file
View file

@ -0,0 +1,34 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
// configureCmd represents the configure command
var genDocCmd = &cobra.Command{
Use: "gendoc",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
docType, err := cmd.Flags().GetString("type")
if err != nil {
return err
}
cmd.DisableAutoGenTag = true
switch docType {
case "markdown":
return doc.GenMarkdownTree(rootCmd, "doc")
case "man":
return doc.GenManTree(rootCmd, nil, "man")
default:
return fmt.Errorf("Unknown type: %v", docType)
}
},
}
func init() {
rootCmd.AddCommand(genDocCmd)
genDocCmd.Flags().StringP("type", "t", "markdown", "what to generate, markdown or man")
}

View file

@ -32,6 +32,8 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config File")
rootCmd.PersistentFlags().Bool("debug", false, "Enable Debug Logging")