Add completions subcommands

This commit is contained in:
Evan Fiordeliso 2023-11-12 18:50:02 -05:00
parent c6e41de5c8
commit 3f7f04e4f1
5 changed files with 200 additions and 0 deletions

124
completions.go Normal file
View File

@ -0,0 +1,124 @@
package cmd
import (
"io"
"os"
"go.fifitido.net/cmd/opt"
)
var outputFileOption = opt.String("output", "o", "", "The file to output the completions to")
func CompletionsSubcommand() *Command {
cmd := New(
"completions",
WithShortDescription("Generate shell completions"),
WithLongDescription("Generate shell completions"),
)
registerBashCompletions(cmd)
registerFishCompletions(cmd)
registerPowerShellCompletions(cmd)
registerZsgCompletions(cmd)
return cmd
}
func getCompletionsOut() io.Writer {
outputFile := outputFileOption.Value()
if outputFile != "" {
var err error
out, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
return out
}
return os.Stdout
}
func registerBashCompletions(parent *Command) *Command {
return New(
"bash",
WithShortDescription("Generate bash completions"),
WithLongDescription("Generate bash completions"),
WithOptions(outputFileOption),
WithParent(parent),
WithRunFunc(func(args []string) {
out := getCompletionsOut()
if fc, ok := out.(io.Closer); ok {
defer fc.Close()
}
if err := WriteBashCompletions(out, parent.Root()); err != nil {
panic(err)
}
}),
)
}
func registerFishCompletions(parent *Command) *Command {
return New(
"fish",
WithShortDescription("Generate fish completions"),
WithLongDescription("Generate fish completions"),
WithOptions(outputFileOption),
WithParent(parent),
WithRunFunc(func(args []string) {
out := getCompletionsOut()
if fc, ok := out.(io.Closer); ok {
defer fc.Close()
}
if err := WriteFishCompletions(out, parent.Root()); err != nil {
panic(err)
}
}),
)
}
func registerPowerShellCompletions(parent *Command) *Command {
return New(
"powershell",
WithShortDescription("Generate powershell completions"),
WithLongDescription("Generate powershell completions"),
WithOptions(outputFileOption),
WithParent(parent),
WithRunFunc(func(args []string) {
out := getCompletionsOut()
if fc, ok := out.(io.Closer); ok {
defer fc.Close()
}
if err := WritePowerShellCompletions(out, parent.Root()); err != nil {
panic(err)
}
}),
)
}
func registerZsgCompletions(parent *Command) *Command {
return New(
"zsh",
WithShortDescription("Generate zsh completions"),
WithLongDescription("Generate zsh completions"),
WithOptions(outputFileOption),
WithParent(parent),
WithRunFunc(func(args []string) {
out := getCompletionsOut()
if fc, ok := out.(io.Closer); ok {
defer fc.Close()
}
if err := WriteZshCompletions(out, parent.Root()); err != nil {
panic(err)
}
}),
)
}

19
completions_bash.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"io"
"text/template"
"go.fifitido.net/cmd/opt"
)
func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
return bashTpl.Execute(out, map[string]any{
"rootCmd": rootCmd,
"globalOpts": opt.Globals(),
})
}
var bashTpl = template.Must(template.New("bash").Parse(`
`))

19
completions_fish.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"io"
"text/template"
"go.fifitido.net/cmd/opt"
)
func WriteFishCompletions(out io.Writer, rootCmd *Command) error {
return fishTpl.Execute(out, map[string]any{
"rootCmd": rootCmd,
"globalOpts": opt.Globals(),
})
}
var fishTpl = template.Must(template.New("fish").Parse(`
`))

19
completions_powershell.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"io"
"text/template"
"go.fifitido.net/cmd/opt"
)
func WritePowerShellCompletions(out io.Writer, rootCmd *Command) error {
return powerShellTpl.Execute(out, map[string]any{
"rootCmd": rootCmd,
"globalOpts": opt.Globals(),
})
}
var powerShellTpl = template.Must(template.New("PowerShell").Parse(`
`))

19
completions_zsh.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"io"
"text/template"
"go.fifitido.net/cmd/opt"
)
func WriteZshCompletions(out io.Writer, rootCmd *Command) error {
return zshTpl.Execute(out, map[string]any{
"rootCmd": rootCmd,
"globalOpts": opt.Globals(),
})
}
var zshTpl = template.Must(template.New("zsh").Parse(`
`))