gum/completion/command.go
Christian Muehlhaeuser 66993d8ef1
Add soft & hard linter configs & workflows (#44)
* chore: add linter configs & workflows

* fix: ignore certain linter warnings

* fix: mark errors as intentionally ignored

* fix: avoid unnecessary conversion

* fix: handle template/renderer errors

* fix: mark intentionally unused code
2022-07-30 12:32:59 -04:00

62 lines
1.3 KiB
Go

package completion
import (
"fmt"
"io"
"os"
"strings"
"github.com/alecthomas/kong"
)
// Completion command.
type Completion struct {
Bash Bash `cmd:"" help:"Generate the autocompletion script for bash"`
Zsh Zsh `cmd:"" help:"Generate the autocompletion script for zsh"`
Fish Fish `cmd:"" help:"Generate the autocompletion script for fish"`
}
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
}
//nolint:deadcode,unused
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) {
_, err := b.WriteString(s)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}
func nonCompletableFlag(flag *kong.Flag) bool {
return flag.Hidden
}
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
}