feat: add sub-command to generate completions
This commit is contained in:
		
							
								
								
									
										34
									
								
								cli/completion/daemon.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								cli/completion/daemon.go
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.cryptic.systems/volker.raschek/flucky/cli/completion"
 | 
				
			||||||
	"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
 | 
						"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
 | 
				
			||||||
	imp "git.cryptic.systems/volker.raschek/flucky/cli/imp"
 | 
						imp "git.cryptic.systems/volker.raschek/flucky/cli/imp"
 | 
				
			||||||
	"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
 | 
						"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
 | 
				
			||||||
@@ -24,7 +25,6 @@ func Execute(version *semver.Version) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	rootCmd := &cobra.Command{
 | 
						rootCmd := &cobra.Command{
 | 
				
			||||||
		Use:               "flucky",
 | 
							Use:               "flucky",
 | 
				
			||||||
		// Short:             "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
 | 
					 | 
				
			||||||
		PersistentPreRunE: preRunError,
 | 
							PersistentPreRunE: preRunError,
 | 
				
			||||||
		Version:           version.String(),
 | 
							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")
 | 
						rootCmd.PersistentFlags().String("loglevel", "info", "Set the Loglevel. Possible values: debug, info, warn, error, fatal")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	subCommands := []func(cmd *cobra.Command) error{
 | 
						subCommands := []func(cmd *cobra.Command) error{
 | 
				
			||||||
 | 
							completion.InitCmd,
 | 
				
			||||||
		daemon.InitCmd,
 | 
							daemon.InitCmd,
 | 
				
			||||||
		imp.InitCmd,
 | 
							imp.InitCmd,
 | 
				
			||||||
		sensor.InitCmd,
 | 
							sensor.InitCmd,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user