feat: add sub-command to generate completions

This commit is contained in:
Markus Pesch 2021-04-10 10:30:37 +02:00
parent 6e6a1cbbb6
commit 93bbc9d6b9
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 37 additions and 2 deletions

34
cli/completion/daemon.go Normal file
View File

@ -0,0 +1,34 @@
package completion
import (
"os"
"github.com/spf13/cobra"
)
// InitCmd initialize all daemon subcommands
func InitCmd(cmd *cobra.Command) error {
completionCmd := &cobra.Command{
Use: "completion",
Short: "Generate completion script",
Example: "flucky completion [bash|zsh|fish]",
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.ExactValidArgs(1),
RunE: run,
}
cmd.AddCommand(completionCmd)
return nil
}
func run(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
}
return nil
}

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"time"
"git.cryptic.systems/volker.raschek/flucky/cli/completion"
"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
imp "git.cryptic.systems/volker.raschek/flucky/cli/imp"
"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
@ -24,7 +25,6 @@ func Execute(version *semver.Version) error {
rootCmd := &cobra.Command{
Use: "flucky",
// Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
PersistentPreRunE: preRunError,
Version: version.String(),
}
@ -38,6 +38,7 @@ func Execute(version *semver.Version) error {
rootCmd.PersistentFlags().String("loglevel", "info", "Set the Loglevel. Possible values: debug, info, warn, error, fatal")
subCommands := []func(cmd *cobra.Command) error{
completion.InitCmd,
daemon.InitCmd,
imp.InitCmd,
sensor.InitCmd,