2022-07-20 19:39:22 +02:00
|
|
|
package completion
|
|
|
|
|
2022-07-25 20:11:54 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alecthomas/kong"
|
|
|
|
)
|
|
|
|
|
2022-07-30 18:32:59 +02:00
|
|
|
// Completion command.
|
2022-07-20 19:39:22 +02:00
|
|
|
type Completion struct {
|
2022-07-25 20:11:54 +02:00
|
|
|
Bash Bash `cmd:"" help:"Generate the autocompletion script for bash"`
|
|
|
|
Zsh Zsh `cmd:"" help:"Generate the autocompletion script for zsh"`
|
2022-07-25 22:21:44 +02:00
|
|
|
Fish Fish `cmd:"" help:"Generate the autocompletion script for fish"`
|
2022-07-20 19:39:22 +02:00
|
|
|
}
|
2022-07-25 20:11:54 +02:00
|
|
|
|
|
|
|
func commandName(cmd *kong.Node) string {
|
|
|
|
commandName := cmd.FullPath()
|
|
|
|
commandName = strings.ReplaceAll(commandName, " ", "_")
|
|
|
|
commandName = strings.ReplaceAll(commandName, ":", "__")
|
|
|
|
return commandName
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasCommands(cmd *kong.Node) bool {
|
|
|
|
for _, c := range cmd.Children {
|
|
|
|
if !c.Hidden {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-30 18:32:59 +02:00
|
|
|
//nolint:deadcode,unused
|
2022-07-25 20:11:54 +02:00
|
|
|
func isArgument(cmd *kong.Node) bool {
|
|
|
|
return cmd.Type == kong.ArgumentNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeString writes a string into a buffer, and checks if the error is not nil.
|
|
|
|
func writeString(b io.StringWriter, s string) {
|
2022-07-30 23:40:33 +02:00
|
|
|
if _, err := b.WriteString(s); err != nil {
|
2022-07-25 20:11:54 +02:00
|
|
|
fmt.Fprintln(os.Stderr, "Error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nonCompletableFlag(flag *kong.Flag) bool {
|
|
|
|
return flag.Hidden
|
|
|
|
}
|
2022-07-25 22:21:44 +02:00
|
|
|
|
|
|
|
func flagPossibleValues(flag *kong.Flag) []string {
|
|
|
|
values := make([]string, 0)
|
|
|
|
for _, enum := range flag.EnumSlice() {
|
|
|
|
if strings.TrimSpace(enum) != "" {
|
|
|
|
values = append(values, enum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|