Add interactive mode to itctl

This commit is contained in:
2021-08-21 12:30:16 -07:00
parent 0d70dd9b11
commit 95cf5bfe6b
3 changed files with 40 additions and 0 deletions

View File

@@ -19,6 +19,7 @@
package cmd
import (
"github.com/abiosoft/ishell"
"github.com/spf13/cobra"
)
@@ -26,6 +27,34 @@ import (
var rootCmd = &cobra.Command{
Use: "itctl",
Short: "Control the itd daemon for InfiniTime smartwatches",
Run: func(cmd *cobra.Command, args []string) {
// Create new shell
sh := ishell.New()
sh.SetPrompt("itctl> ")
// For every command in cobra
for _, subCmd := range cmd.Commands() {
// Add top level command to ishell
sh.AddCmd(&ishell.Cmd{
Name: subCmd.Name(),
Help: subCmd.Short,
Aliases: subCmd.Aliases,
LongHelp: subCmd.Long,
Func: func(ctx *ishell.Context) {
// Append name and arguments of command
args := append([]string{ctx.Cmd.Name}, ctx.Args...)
// Set root command arguments
cmd.SetArgs(args)
// Execute root command with new arguments
cmd.Execute()
},
})
}
// Start shell
sh.Run()
},
}
// Execute adds all child commands to the root command and sets flags appropriately.