From df9320134bfb01e5b661ee1e78c23af154622055 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 5 Jul 2019 06:40:56 +0100 Subject: [PATCH] autocompletion: Add control flag and a prompt (#2812) *) Add --no-autocompletion to disable the automatic install of the auto completion *) Add a prompt to ask for permission to install automatic auto completion when not installed yet --- cmd/flags.go | 4 ++++ cmd/main.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/cmd/flags.go b/cmd/flags.go index 944ba8e5..b3ffc114 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -54,6 +54,10 @@ var globalFlags = []cli.Flag{ Name: "insecure", Usage: "disable SSL certificate verification", }, + cli.BoolFlag{ + Name: "no-autocompletion", + Usage: "disable automatic install of mc auto-completion", + }, } // Flags common across all I/O commands such as cp, mirror, stat, pipe etc. diff --git a/cmd/main.go b/cmd/main.go index d6d24623..f7629a1f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ package cmd import ( + "bufio" "bytes" "fmt" "os" @@ -33,6 +34,7 @@ import ( "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/words" "github.com/pkg/profile" + "golang.org/x/crypto/ssh/terminal" completeinstall "github.com/posener/complete/cmd/install" ) @@ -194,9 +196,6 @@ func initMC() { } } - // Install mc completion, ignore any error for now - _ = completeinstall.Install("mc") - // Check if mc session directory exists. if !isSessionDirExists() { fatalIf(createSessionDir().Trace(), "Unable to create session config directory.") @@ -222,6 +221,47 @@ func initMC() { } +func installAutoCompletion(ctx *cli.Context) { + if ctx.Bool("no-autocompletion") || ctx.GlobalBool("no-autocompletion") { + return + } + + if globalQuiet || globalJSON || !terminal.IsTerminal(int(os.Stdout.Fd())) { + return + } + + if runtime.GOOS == "windows" { + return + } + + if completeinstall.IsInstalled("mc") { + return + } + + for { + fmt.Printf("Install mc auto-completion in your shell ? (y/n): ") + reader := bufio.NewReader(os.Stdin) + char, _, err := reader.ReadRune() + if err != nil { + continue + } + + switch char { + case 'y', 'Y': + // Install mc completion, ignore any error for now + err := completeinstall.Install("mc") + if err != nil { + errorIf(probe.NewError(err), "Unable to install mc auto-completion.") + } else { + console.Infoln("Auto-completion installed! Kindly restart your shell to load it.") + } + fallthrough + case 'n', 'N': + return + } + } +} + func registerBefore(ctx *cli.Context) error { // Check if mc was compiled using a supported version of Golang. checkGoVersion() @@ -241,6 +281,9 @@ func registerBefore(ctx *cli.Context) error { // Check if config can be read. checkConfig() + // Install shell completions + installAutoCompletion(ctx) + return nil } diff --git a/go.mod b/go.mod index be3ff4ab..53e6c38f 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/profile v1.3.0 github.com/pkg/xattr v0.4.1 - github.com/posener/complete v1.2.2-0.20190529084822-e1dacfd84468 + github.com/posener/complete v1.2.2-0.20190702141536-6ffe496ea953 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect github.com/rjeczalik/notify v0.9.2 github.com/segmentio/go-prompt v1.2.1-0.20161017233205-f0d19b6901ad diff --git a/go.sum b/go.sum index 404bafe5..0fe833c5 100644 --- a/go.sum +++ b/go.sum @@ -503,6 +503,8 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E= github.com/posener/complete v1.2.2-0.20190529084822-e1dacfd84468 h1:u7MSvwu/HBo7+ySc1z2nkkuY07Mm52ugSJROgwumIYc= github.com/posener/complete v1.2.2-0.20190529084822-e1dacfd84468/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E= +github.com/posener/complete v1.2.2-0.20190702141536-6ffe496ea953 h1:oBvgW8IvwF278gJ3R4hH0gD3ZeJxjwBXVIScRR0dRc8= +github.com/posener/complete v1.2.2-0.20190702141536-6ffe496ea953/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E= github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/pquerna/otp v1.1.0/go.mod h1:Zad1CMQfSQZI5KLpahDiSUX4tMMREnXw98IvL1nhgMk= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=