Skip to content

Commit

Permalink
Merge pull request #25 from c-bata/file-completer
Browse files Browse the repository at this point in the history
Use completer.FilePathCompleter
  • Loading branch information
c-bata authored Jun 24, 2018
2 parents 919059a + 8e74818 commit 330c5d1
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 54 deletions.
214 changes: 198 additions & 16 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

[[constraint]]
name = "github.com/c-bata/go-prompt"
version = "v0.2.0"
branch = "master"

[[constraint]]
name = "k8s.io/client-go"
Expand Down
51 changes: 14 additions & 37 deletions kube/option_arguments.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package kube

import (
"io/ioutil"
"log"
"path/filepath"
"os"
"strings"

"github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt/completer"
)

func init() {
fileListCache = map[string][]prompt.Suggest{}
var yamlFileCompleter = completer.FilePathCompleter{
IgnoreCase: true,
Filter: func(fi os.FileInfo) bool {
if fi.IsDir() {
return true
}
if strings.HasSuffix(fi.Name(), ".yaml") || strings.HasSuffix(fi.Name(), ".yml") {
return true
}
return false
},
}

func getPreviousOption(d prompt.Document) (cmd, option string, found bool) {
Expand All @@ -36,41 +44,10 @@ func completeOptionArguments(d prompt.Document) ([]prompt.Suggest, bool) {
"label", "annotate", "scale", "convert", "autoscale", "top":
switch option {
case "-f", "--filename":
return fileCompleter(d), true
return yamlFileCompleter.Complete(d), true
case "--namespace":
return getNameSpaceSuggestions(), true
}
}
return []prompt.Suggest{}, false
}

/* file list */

var fileListCache map[string][]prompt.Suggest

func fileCompleter(d prompt.Document) []prompt.Suggest {
path := d.GetWordBeforeCursor()
if strings.HasPrefix(path, "./") {
path = path[2:]
}
dir := filepath.Dir(path)
if cached, ok := fileListCache[dir]; ok {
return cached
}

files, err := ioutil.ReadDir(dir)
if err != nil {
log.Print("[ERROR] catch error " + err.Error())
return []prompt.Suggest{}
}
suggests := make([]prompt.Suggest, 0, len(files))
for _, f := range files {
if !f.IsDir() &&
!strings.HasSuffix(f.Name(), ".yml") &&
!strings.HasSuffix(f.Name(), ".yaml") {
continue
}
suggests = append(suggests, prompt.Suggest{Text: filepath.Join(dir, f.Name())})
}
return prompt.FilterHasPrefix(suggests, path, false)
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt/completer"
"github.com/c-bata/kube-prompt/kube"
)

Expand All @@ -22,6 +23,7 @@ func main() {
prompt.OptionTitle("kube-prompt: interactive kubernetes client"),
prompt.OptionPrefix(">>> "),
prompt.OptionInputTextColor(prompt.Yellow),
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
)
p.Run()
}

0 comments on commit 330c5d1

Please sign in to comment.