diff --git a/cmd/shell/main.go b/cmd/shell/main.go index 7bf5352ff..a3a46a896 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -6,7 +6,10 @@ import ( "github.com/NathanBaulch/protoc-gen-cobra/client" "github.com/NathanBaulch/protoc-gen-cobra/naming" + "github.com/c-bata/go-prompt" + "github.com/inancgumus/screen" "github.com/pactus-project/pactus/cmd" + "github.com/pactus-project/pactus/util/shell" pb "github.com/pactus-project/pactus/www/grpc/gen/go" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -18,10 +21,13 @@ const ( defaultResponseFormat = "prettyjson" ) +var _prefix string + func main() { var ( - username string - password string + serverAddr string + username string + password string ) rootCmd := &cobra.Command{ @@ -30,11 +36,42 @@ func main() { Long: `pactus-shell is a command line tool for interacting with the Pactus blockchain using gRPC`, } + sh := shell.New(rootCmd, nil, + prompt.OptionSuggestionBGColor(prompt.Black), + prompt.OptionSuggestionTextColor(prompt.Green), + prompt.OptionDescriptionBGColor(prompt.Black), + prompt.OptionDescriptionTextColor(prompt.White), + prompt.OptionLivePrefix(livePrefix), + ) + client.RegisterFlagBinder(func(fs *pflag.FlagSet, namer naming.Namer) { fs.StringVar(&username, namer("auth-username"), "", "username for gRPC basic authentication") fs.StringVar(&password, namer("auth-password"), "", "password for gRPC basic authentication") }) + sh.Flags().StringVar(&serverAddr, "server-addr", defaultServerAddr, "gRPC server address") + sh.Flags().StringVar(&username, "auth-username", "", + "username for gRPC basic authentication") + + sh.Flags().StringVar(&password, "auth-password", "", + "username for gRPC basic authentication") + + sh.PreRun = func(_ *cobra.Command, _ []string) { + cls() + cmd.PrintInfoMsgf("Welcome to PactusBlockchain shell\n\n- Home: https//pactus.org\n- " + + "Docs: https://docs.pactus.org") + cmd.PrintLine() + _prefix = fmt.Sprintf("pactus@%s > ", serverAddr) + } + + sh.PersistentPreRun = func(cmd *cobra.Command, _ []string) { + setAuthContext(cmd, username, password) + } + + rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) { + setAuthContext(cmd, username, password) + } + changeDefaultParameters := func(c *cobra.Command) *cobra.Command { _ = c.PersistentFlags().Lookup("server-addr").Value.Set(defaultServerAddr) c.PersistentFlags().Lookup("server-addr").DefValue = defaultServerAddr @@ -45,24 +82,43 @@ func main() { return c } - rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { - if username != "" && password != "" { - auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) - md := metadata.Pairs("authorization", "Basic "+auth) - ctx := metadata.NewOutgoingContext(cmd.Context(), md) - cmd.SetContext(ctx) - } - - return nil - } - rootCmd.AddCommand(changeDefaultParameters(pb.BlockchainClientCommand())) rootCmd.AddCommand(changeDefaultParameters(pb.NetworkClientCommand())) rootCmd.AddCommand(changeDefaultParameters(pb.TransactionClientCommand())) rootCmd.AddCommand(changeDefaultParameters(pb.WalletClientCommand())) + rootCmd.AddCommand(clearScreen()) + rootCmd.AddCommand(sh) err := rootCmd.Execute() if err != nil { cmd.PrintErrorMsgf("%s", err) } } + +func livePrefix() (string, bool) { + return _prefix, true +} + +func clearScreen() *cobra.Command { + return &cobra.Command{ + Use: "clear", + Short: "clear screen", + Run: func(_ *cobra.Command, _ []string) { + cls() + }, + } +} + +func cls() { + screen.MoveTopLeft() + screen.Clear() +} + +func setAuthContext(c *cobra.Command, username, password string) { + if username != "" && password != "" { + auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) + md := metadata.Pairs("authorization", "Basic "+auth) + ctx := metadata.NewOutgoingContext(c.Context(), md) + c.SetContext(ctx) + } +} diff --git a/go.mod b/go.mod index e30fcd5a7..1102af5d7 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,10 @@ go 1.22.2 require ( github.com/NathanBaulch/protoc-gen-cobra v1.2.1 github.com/beevik/ntp v1.4.3 + github.com/c-bata/go-prompt v0.2.6 github.com/fxamacker/cbor/v2 v2.7.0 github.com/gofrs/flock v0.9.0 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.6.0 github.com/gorilla/handlers v1.5.2 github.com/gorilla/mux v1.8.1 @@ -14,6 +16,8 @@ require ( github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 github.com/hashicorp/golang-lru/v2 v2.0.7 + github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 + github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 github.com/kilic/bls12-381 v0.1.0 github.com/libp2p/go-libp2p v0.35.1 github.com/libp2p/go-libp2p-kad-dht v0.25.2 @@ -25,6 +29,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.19.1 github.com/rs/zerolog v1.33.0 + github.com/schollz/progressbar/v3 v3.14.4 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 @@ -33,6 +38,7 @@ require ( go.nanomsg.org/mangos/v3 v3.4.2 golang.org/x/crypto v0.24.0 golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 + golang.org/x/term v0.22.0 google.golang.org/grpc v1.65.0 google.golang.org/protobuf v1.34.2 gopkg.in/natefinch/lumberjack.v2 v2.2.1 @@ -80,7 +86,6 @@ require ( github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect github.com/jbenet/goprocess v0.1.4 // indirect - github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/koron/go-ssdp v0.0.4 // indirect @@ -100,6 +105,8 @@ require ( github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.9 // indirect + github.com/mattn/go-tty v0.0.3 // indirect github.com/miekg/dns v1.1.61 // indirect github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect @@ -137,6 +144,7 @@ require ( github.com/pion/transport/v2 v2.2.5 // indirect github.com/pion/turn/v2 v2.1.6 // indirect github.com/pion/webrtc/v3 v3.2.43 // indirect + github.com/pkg/term v1.2.0-beta.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/polydawn/refmt v0.89.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect @@ -147,7 +155,6 @@ require ( github.com/quic-go/webtransport-go v0.8.0 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/schollz/progressbar/v3 v3.14.4 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect @@ -165,7 +172,6 @@ require ( golang.org/x/net v0.26.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.22.0 // indirect - golang.org/x/term v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect golang.org/x/tools v0.22.0 // indirect gonum.org/v1/gonum v0.15.0 // indirect diff --git a/go.sum b/go.sum index 7ae004190..22c537394 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= +github.com/c-bata/go-prompt v0.2.6 h1:POP+nrHE+DfLYx370bedwNhsqmpCUynWPxuHi0C5vZI= +github.com/c-bata/go-prompt v0.2.6/go.mod h1:/LMAke8wD2FsNu9EXNdHxNLbd9MedkPnCdfpU9wwHfY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -158,6 +160,8 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 h1:e+8XbKB6IMn8A4OAyZccO4pYfB3s7bt6azNIPE7AnPg= github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -196,6 +200,8 @@ github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc= github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8= github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= +github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk= +github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/ipfs/boxo v0.21.0 h1:XpGXb+TQQ0IUdYaeAxGzWjSs6ow/Lce148A/2IbRDVE= @@ -290,13 +296,23 @@ github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYt github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd/go.mod h1:QuCEs1Nt24+FYQEqAAncTDPJIuGs+LxK1MCiFL25pMU= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI= +github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= @@ -424,6 +440,8 @@ github.com/pion/webrtc/v3 v3.2.43/go.mod h1:M1RAe3TNTD1tzyvqHrbVODfwdPGSXOUo/Ogp github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw= +github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/polydawn/refmt v0.89.0 h1:ADJTApkvkeBZsN0tBTx8QjpD9JkmxbKp0cxfr9qszm4= @@ -662,18 +680,24 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190316082340-a2f829d7f35f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -702,8 +726,6 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -792,8 +814,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= -google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= diff --git a/util/shell/shell.go b/util/shell/shell.go new file mode 100644 index 000000000..a94c44aeb --- /dev/null +++ b/util/shell/shell.go @@ -0,0 +1,250 @@ +package shell + +import ( + "bytes" + "fmt" + "os" + "sort" + "strings" + + "github.com/c-bata/go-prompt" + "github.com/google/shlex" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "golang.org/x/term" +) + +type lexer struct { + root *cobra.Command + refresh func() *cobra.Command + cache map[string][]prompt.Suggest + stdin *term.State +} + +// New creates a Cobra CLI command named "shell" which runs an interactive shell prompt for the root command. +func New(root *cobra.Command, refresh func() *cobra.Command, opts ...prompt.Option) *cobra.Command { + sh := &lexer{ + root: root, + refresh: refresh, + cache: make(map[string][]prompt.Suggest), + } + + prefix := fmt.Sprintf("> %s ", root.Name()) + opts = append(opts, prompt.OptionPrefix(prefix), prompt.OptionShowCompletionAtStart()) + + return &cobra.Command{ + Use: "shell", + Short: "Start an interactive shell.", + Run: func(cmd *cobra.Command, _ []string) { + sh.saveStdin() + + sh.editCommandTree(cmd) + prompt.New(sh.executor, sh.completer, opts...).Run() + + sh.restoreStdin() + }, + } +} + +func (s *lexer) editCommandTree(shell *cobra.Command) { + s.root.RemoveCommand(shell) + + // Hide the "completion" command + if cmd, _, err := s.root.Find([]string{"completion"}); err == nil { + // TODO: Remove this command + cmd.Hidden = true + } + + s.root.AddCommand(&cobra.Command{ + Use: "exit", + Short: "Exit the interactive shell.", + Run: func(*cobra.Command, []string) { + // TODO: Exit cleanly without help from the os package + os.Exit(0) + }, + }) + + initDefaultHelpFlag(s.root) +} + +func initDefaultHelpFlag(cmd *cobra.Command) { + cmd.InitDefaultHelpFlag() + + for _, subcommand := range cmd.Commands() { + initDefaultHelpFlag(subcommand) + } +} + +func (s *lexer) saveStdin() { + state, err := term.GetState(int(os.Stdin.Fd())) + if err != nil { + return + } + s.stdin = state +} + +func (s *lexer) executor(line string) { + // Allow command to read from stdin + s.restoreStdin() + + args, _ := shlex.Split(line) + _ = execute(s.root, args) + + if s.refresh != nil { + s.root = s.refresh() + s.editCommandTree(s.root) + } else { + if cmd, _, err := s.root.Find(args); err == nil { + cmd.Flags().VisitAll(func(flag *pflag.Flag) { + flag.Changed = false + }) + } + } + + s.cache = make(map[string][]prompt.Suggest) +} + +func (s *lexer) restoreStdin() { + if s.stdin != nil { + _ = term.Restore(int(os.Stdin.Fd()), s.stdin) + } +} + +func (s *lexer) completer(d prompt.Document) []prompt.Suggest { + args, err := buildCompletionArgs(d.CurrentLine()) + if err != nil { + return nil + } + + if !isFlag(args[len(args)-1]) { + // Clear partial strings to generate all possible completions + args[len(args)-1] = "" + } + key := strings.Join(args, " ") + + suggestions, ok := s.cache[key] + if !ok { + out, err := readCommandOutput(s.root, args) + if err != nil { + return nil + } + suggestions = parseSuggestions(out) + s.cache[key] = suggestions + } + + return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true) +} + +func buildCompletionArgs(input string) ([]string, error) { + args, err := shlex.Split(input) + + args = append([]string{"__complete"}, args...) + if input == "" || input[len(input)-1] == ' ' { + args = append(args, "") + } + + return args, err +} + +func readCommandOutput(cmd *cobra.Command, args []string) (string, error) { + buf := new(bytes.Buffer) + + stdout := cmd.OutOrStdout() + stderr := os.Stderr + + cmd.SetOut(buf) + _, os.Stderr, _ = os.Pipe() + + err := execute(cmd, args) + + cmd.SetOut(stdout) + os.Stderr = stderr + + return buf.String(), err +} + +func execute(cmd *cobra.Command, args []string) error { + if c, _, err := cmd.Find(args); err == nil { + // Reset flag values between runs due to a limitation in Cobra + c.Flags().VisitAll(func(flag *pflag.Flag) { + if val, ok := flag.Value.(pflag.SliceValue); ok { + _ = val.Replace([]string{}) + } else { + _ = flag.Value.Set(flag.DefValue) + } + + _ = c.Flags().SetAnnotation(flag.Name, cobra.BashCompOneRequiredFlag, []string{"false"}) + }) + + c.InitDefaultHelpFlag() + } + + cmd.SetArgs(args) + + return cmd.Execute() +} + +func parseSuggestions(out string) []prompt.Suggest { + suggestions := make([]prompt.Suggest, 0) + + x := strings.Split(out, "\n") + if len(x) < 2 { + return nil + } + + for _, line := range x[:len(x)-2] { + l := strings.SplitN(line, "\t", 2) + + if isShorthandFlag(l[0]) { + continue + } + + suggestion := prompt.Suggest{Text: escapeSpecialCharacters(l[0])} + if len(l) > 1 { + suggestion.Description = l[1] + } + + suggestions = append(suggestions, suggestion) + } + + sort.Slice(suggestions, func(i, j int) bool { + it := suggestions[i].Text + jt := suggestions[j].Text + + if isFlag(it) && isFlag(jt) { + return it < jt + } + + if isFlag(it) { + return false + } + + if isFlag(jt) { + return true + } + + return it < jt + }) + + return suggestions +} + +func escapeSpecialCharacters(val string) string { + for _, c := range []string{`\`, `"`, "$", "`", "!"} { + val = strings.ReplaceAll(val, c, `\`+c) + } + + if strings.ContainsAny(val, " #&*;<>?[]|~") { + val = fmt.Sprintf("%q", val) + } + + return val +} + +func isFlag(arg string) bool { + return strings.HasPrefix(arg, "-") +} + +func isShorthandFlag(arg string) bool { + return isFlag(arg) && !strings.HasPrefix(arg, "--") +} diff --git a/util/shell/shell_test.go b/util/shell/shell_test.go new file mode 100644 index 000000000..dfce17551 --- /dev/null +++ b/util/shell/shell_test.go @@ -0,0 +1,150 @@ +package shell + +import ( + "errors" + "testing" + + "github.com/c-bata/go-prompt" + "github.com/spf13/cobra" + "github.com/stretchr/testify/require" +) + +func TestBuildCompletionArgs_Empty(t *testing.T) { + args, err := buildCompletionArgs("") + require.NoError(t, err) + + expected := []string{"__complete", ""} + require.Equal(t, expected, args) +} + +func TestBuildCompletionArgs_CurrentArg(t *testing.T) { + args, err := buildCompletionArgs("a b") + require.NoError(t, err) + + expected := []string{"__complete", "a", "b"} + require.Equal(t, expected, args) +} + +func TestBuildCompletionArgs_MultiwordString(t *testing.T) { + args, err := buildCompletionArgs(`a "b c"`) + require.NoError(t, err) + + expected := []string{"__complete", "a", "b c"} + require.Equal(t, expected, args) +} + +func TestBuildCompletionArgs_NextArg(t *testing.T) { + args, err := buildCompletionArgs("a b ") + require.NoError(t, err) + + expected := []string{"__complete", "a", "b", ""} + require.Equal(t, expected, args) +} + +func TestReadCommandOutput_Stdout(t *testing.T) { + cmd := &cobra.Command{ + Use: "command", + Run: func(cmd *cobra.Command, _ []string) { + cmd.Print("out") + }, + } + + out, err := readCommandOutput(cmd, []string{}) + require.NoError(t, err) + require.Equal(t, "out", out) +} + +func TestReadCommandOutput_Stderr(t *testing.T) { + cmd := &cobra.Command{ + Use: "command", + Run: func(cmd *cobra.Command, _ []string) { + cmd.PrintErr("out") + }, + } + + out, err := readCommandOutput(cmd, []string{}) + require.NoError(t, err) + require.Empty(t, out) +} + +func TestReadCommandOutput_Err(t *testing.T) { + cmd := &cobra.Command{ + Use: "command", + RunE: func(_ *cobra.Command, _ []string) error { + return errors.New("err") + }, + } + + _, err := readCommandOutput(cmd, []string{}) + require.Error(t, err) +} + +func TestParseSuggestions_WithDescription(t *testing.T) { + out := `command-with-description description +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp` + expected := []prompt.Suggest{{Text: "command-with-description", Description: "description"}} + require.Equal(t, expected, parseSuggestions(out)) +} + +func TestParseSuggestions_WithoutDescription(t *testing.T) { + out := `command-without-description +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp` + expected := []prompt.Suggest{{Text: "command-without-description"}} + require.Equal(t, expected, parseSuggestions(out)) +} + +func TestParseSuggestions_HideShorthandFlags(t *testing.T) { + out := `--flag A flag. +-f A flag. +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp` + expected := []prompt.Suggest{{Text: "--flag", Description: "A flag."}} + require.Equal(t, expected, parseSuggestions(out)) +} + +func TestParseSuggestions_Sort(t *testing.T) { + out := `b +a +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp` + expected := []prompt.Suggest{{Text: "a"}, {Text: "b"}} + require.Equal(t, expected, parseSuggestions(out)) +} + +func TestEscapeSpecialCharacters_Spaces(t *testing.T) { + require.Equal(t, `"string with spaces"`, escapeSpecialCharacters("string with spaces")) +} + +func TestEscapeSpecialCharacters_All(t *testing.T) { + require.Equal(t, "\\\\\\\"\\$\\`\\!", escapeSpecialCharacters("\\\"$`!")) +} + +func TestEditCommandTree_RemoveShell(t *testing.T) { + root := &cobra.Command{} + sh := &cobra.Command{Use: "lexer"} + root.AddCommand(sh) + + s := &lexer{root: root} + s.editCommandTree(sh) + require.False(t, hasSubcommand(root, "lexer")) +} + +func TestEditCommandTree_AddExit(t *testing.T) { + root := &cobra.Command{} + + s := &lexer{root: root} + s.editCommandTree(nil) + require.True(t, hasSubcommand(root, "exit")) +} + +func hasSubcommand(cmd *cobra.Command, name string) bool { + for _, subcommand := range cmd.Commands() { + if subcommand.Name() == name { + return true + } + } + + return false +}