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
This commit is contained in:
Anis Elleuch
2019-07-04 22:40:56 -07:00
committed by Harshavardhana
parent 6003da8daa
commit 7a730d801e
4 changed files with 53 additions and 4 deletions
+4
View File
@@ -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.
+46 -3
View File
@@ -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
}
+1 -1
View File
@@ -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
+2
View File
@@ -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=