From bf1cda2e7e9a3ad6a4eaf72c1cc184505de0427a Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 21 Aug 2021 12:30:16 -0700 Subject: [PATCH] Add interactive mode to itctl --- cmd/itctl/cmd/root.go | 29 +++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 8 ++++++++ 3 files changed, 40 insertions(+) diff --git a/cmd/itctl/cmd/root.go b/cmd/itctl/cmd/root.go index 0a46b83..610f661 100644 --- a/cmd/itctl/cmd/root.go +++ b/cmd/itctl/cmd/root.go @@ -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. diff --git a/go.mod b/go.mod index 629f708..b0e349d 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,11 @@ go 1.16 require ( github.com/VividCortex/ewma v1.2.0 // indirect + github.com/abiosoft/ishell v2.0.0+incompatible + github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect github.com/cheggaaa/pb/v3 v3.0.8 github.com/fatih/color v1.12.0 // indirect + github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect github.com/fsnotify/fsnotify v1.5.0 // indirect github.com/godbus/dbus/v5 v5.0.4 github.com/mattn/go-isatty v0.0.13 // indirect diff --git a/go.sum b/go.sum index 4c85272..88f5fc3 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,10 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= +github.com/abiosoft/ishell v2.0.0+incompatible h1:zpwIuEHc37EzrsIYah3cpevrIc8Oma7oZPxr03tlmmw= +github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg= +github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8= +github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= @@ -51,8 +55,10 @@ github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqO github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cheggaaa/pb/v3 v3.0.8 h1:bC8oemdChbke2FHIIGy9mn4DPJ2caZYQnfbRqwmdCoA= github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA= +github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= @@ -77,6 +83,8 @@ github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc= github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BMXYYRWTLOJKlh+lOBt6nUQgXAfB7oVIQt5cNreqSLI= +github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.0 h1:NO5hkcB+srp1x6QmwvNZLeaOgbM8cmBTN32THzjvu2k= github.com/fsnotify/fsnotify v1.5.0/go.mod h1:BX0DCEr5pT4jm2CnQdVP1lFV521fcCNcyEeNp4DQQDk=