From 31e5ac02bdbdbaf20a87683925041f406307cfb9 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 11 Jul 2019 10:41:14 -0700 Subject: [PATCH] Fix auto-completion code (#2819) Reverts the code in #2812 Fixes #2816 --- README.md | 4 +-- cmd/cat-main.go | 4 +-- cmd/complete.go | 3 ++- cmd/flags.go | 8 +++--- cmd/globals.go | 16 ++++++++---- cmd/head-main.go | 4 +-- cmd/main.go | 65 ++++++++++++++++-------------------------------- cmd/utils.go | 3 ++- 8 files changed, 43 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index e346dca0..d64a170f 100644 --- a/README.md +++ b/README.md @@ -207,8 +207,7 @@ alias find='mc find' ``` ### Shell autocompletion - -In case you are using bash or zsh, the current shell will be automatically setup to use completion feature to suggest or auto-complete your mc commands. +In case you are using bash, zsh or fish. Shell completion is embedded by default in `mc`, to install auto-completion use `mc --autocompletion`. Restart the shell, mc will auto-complete commands as shown below. ``` mc @@ -224,6 +223,5 @@ cat cp event head mb pipe rm share stat ## Contribute to MinIO Project Please follow MinIO [Contributor's Guide](https://github.com/minio/mc/blob/master/CONTRIBUTING.md) - ## License [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fminio%2Fmc.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fminio%2Fmc?ref=badge_large) diff --git a/cmd/cat-main.go b/cmd/cat-main.go index 6f29a367..5b87fd2b 100644 --- a/cmd/cat-main.go +++ b/cmd/cat-main.go @@ -27,8 +27,6 @@ import ( "unicode" "unicode/utf8" - "golang.org/x/crypto/ssh/terminal" - "github.com/minio/cli" "github.com/minio/mc/pkg/probe" ) @@ -173,7 +171,7 @@ func catOut(r io.Reader, size int64) *probe.Error { // In case of a user showing the object content in a terminal, // avoid printing control and other bad characters to avoid // terminal session corruption - if terminal.IsTerminal(int(os.Stdout.Fd())) { + if isTerminal() { stdout = newPrettyStdout(os.Stdout) } else { stdout = os.Stdout diff --git a/cmd/complete.go b/cmd/complete.go index 8fc7a6af..6793763b 100644 --- a/cmd/complete.go +++ b/cmd/complete.go @@ -17,6 +17,7 @@ package cmd import ( + "os" "path/filepath" "sort" "strings" @@ -254,6 +255,6 @@ func mainComplete() error { GlobalFlags: complFlags, } // Answer to bash completion call - complete.New("mc", mcComplete).Run() + complete.New(filepath.Base(os.Args[0]), mcComplete).Run() return nil } diff --git a/cmd/flags.go b/cmd/flags.go index b3ffc114..70a75685 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -29,6 +29,10 @@ var commandsTree = trie.NewTrie() // Collection of mc flags currently supported var globalFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "autocompletion", + Usage: "install auto-completion for your shell", + }, cli.StringFlag{ Name: "config-dir, C", Value: mustGetMcConfigDir(), @@ -54,10 +58,6 @@ 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/globals.go b/cmd/globals.go index f3de01d5..d07fe5fa 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -19,6 +19,9 @@ package cmd import ( "crypto/x509" + "fmt" + "os" + "path/filepath" "github.com/minio/cli" "github.com/minio/mc/pkg/console" @@ -29,14 +32,17 @@ const ( minGoVersion = ">= 1.7.5" // mc requires at least Go v1.7.5 ) +var ( + globalMCConfigDir = fmt.Sprintf(".%s/", filepath.Base(os.Args[0])) + globalMCConfigWindowsDir = fmt.Sprintf("%s\\", filepath.Base(os.Args[0])) +) + const ( globalMCConfigVersion = "9" - globalMCConfigDir = ".mc/" - globalMCConfigWindowsDir = "mc\\" - globalMCConfigFile = "config.json" - globalMCCertsDir = "certs" - globalMCCAsDir = "CAs" + globalMCConfigFile = "config.json" + globalMCCertsDir = "certs" + globalMCCAsDir = "CAs" // session config and shared urls related constants globalSessionDir = "session" diff --git a/cmd/head-main.go b/cmd/head-main.go index c324c739..db188048 100644 --- a/cmd/head-main.go +++ b/cmd/head-main.go @@ -26,8 +26,6 @@ import ( "strings" "syscall" - "golang.org/x/crypto/ssh/terminal" - "github.com/minio/cli" "github.com/minio/mc/pkg/probe" ) @@ -111,7 +109,7 @@ func headOut(r io.Reader, nlines int64) *probe.Error { // In case of a user showing the object content in a terminal, // avoid printing control and other bad characters to avoid // terminal session corruption - if terminal.IsTerminal(int(os.Stdout.Fd())) { + if isTerminal() { stdout = newPrettyStdout(os.Stdout) } else { stdout = os.Stdout diff --git a/cmd/main.go b/cmd/main.go index f7629a1f..c6108537 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,5 +1,5 @@ /* - * MinIO Client (C) 2014, 2015, 2016, 2017 MinIO, Inc. + * MinIO Client (C) 2014-2019 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package cmd import ( - "bufio" "bytes" "fmt" "os" @@ -34,7 +33,6 @@ 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" ) @@ -57,6 +55,9 @@ COMMANDS: GLOBAL FLAGS: {{range .VisibleFlags}}{{.}} {{end}}{{end}} +TIP: + Use '{{.Name}} --autocompletion' to enable shell autocompletion + VERSION: ` + ReleaseTag + `{{ "\n"}}{{range $key, $value := ExtraInfo}} @@ -68,7 +69,7 @@ VERSION: func Main(args []string) { if len(args) > 1 { switch args[1] { - case "mc", "-install", "-uninstall": + case "mc", filepath.Base(args[0]): mainComplete() return } @@ -221,44 +222,22 @@ 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 - } - +func installAutoCompletion() { if runtime.GOOS == "windows" { + console.Infoln("autocompletion feature is not available for this operating system") return } - if completeinstall.IsInstalled("mc") { + if completeinstall.IsInstalled(filepath.Base(os.Args[0])) || completeinstall.IsInstalled("mc") { + console.Infoln("autocompletion is already enabled in your '$SHELLRC'") 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 - } + err := completeinstall.Install(filepath.Base(os.Args[0])) + if err != nil { + fatalIf(probe.NewError(err), "Unable to install auto-completion.") + } else { + console.Infoln("enabled autocompletion in '$SHELLRC'. Please restart your shell.") } } @@ -281,9 +260,6 @@ func registerBefore(ctx *cli.Context) error { // Check if config can be read. checkConfig() - // Install shell completions - installAutoCompletion(ctx) - return nil } @@ -361,12 +337,6 @@ func registerApp(name string) *cli.App { Usage: "show help", } - cli.BashCompletionFlag = cli.BoolFlag{ - Name: "compgen", - Usage: "enables bash-completion for all commands and subcommands", - Hidden: true, - } - app := cli.NewApp() app.Name = name app.Action = func(ctx *cli.Context) { @@ -374,6 +344,13 @@ func registerApp(name string) *cli.App { // Check for new updates from dl.min.io. checkUpdate(ctx) } + + if ctx.Bool("autocompletion") || ctx.GlobalBool("autocompletion") { + // Install shell completions + installAutoCompletion() + return + } + cli.ShowAppHelp(ctx) } diff --git a/cmd/utils.go b/cmd/utils.go index c83d780d..a658c7fb 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -22,6 +22,7 @@ import ( "io" "math/rand" "os" + "path/filepath" "runtime" "sort" "strings" @@ -132,7 +133,7 @@ func newS3Config(urlStr string, hostCfg *hostConfigV9) *Config { // credentials from the match found in the config file. s3Config := new(Config) - s3Config.AppName = "mc" + s3Config.AppName = filepath.Base(os.Args[0]) s3Config.AppVersion = Version s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH} s3Config.Debug = globalDebug