Fix auto-completion code (#2819)

Reverts the code in #2812

Fixes #2816
This commit is contained in:
Harshavardhana
2019-07-11 10:41:14 -07:00
committed by Dee Koder
parent aa3c9f58df
commit 94667d5d5c
8 changed files with 43 additions and 64 deletions
+1 -3
View File
@@ -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 <TAB>
@@ -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)
+1 -3
View File
@@ -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
+2 -1
View File
@@ -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
}
+4 -4
View File
@@ -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.
+11 -5
View File
@@ -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"
+1 -3
View File
@@ -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
+21 -44
View File
@@ -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)
}
+2 -1
View File
@@ -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