From 60345dc6db8d1247fef2fb456d2f18f3300651ac Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 19:11:16 +0300 Subject: [PATCH 1/4] fmt --- gocomplete/complete.go | 4 ++-- install/home.go | 4 ++-- install/root.go | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/gocomplete/complete.go b/gocomplete/complete.go index 60a9835..159d79f 100644 --- a/gocomplete/complete.go +++ b/gocomplete/complete.go @@ -8,8 +8,8 @@ var ( predictEllipsis = complete.PredictSet("./...") goFilesOrPackages = complete.PredictFiles("**.go"). - Or(complete.PredictDirs("./")). - Or(predictEllipsis) + Or(complete.PredictDirs("./")). + Or(predictEllipsis) ) func main() { diff --git a/install/home.go b/install/home.go index 1850a05..825bdb7 100644 --- a/install/home.go +++ b/install/home.go @@ -5,10 +5,10 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" "os/user" "path/filepath" - "io/ioutil" ) type home struct{} @@ -150,4 +150,4 @@ func copyFile(src string, dst string) error { defer out.Close() _, err = io.Copy(out, in) return err -} \ No newline at end of file +} diff --git a/install/root.go b/install/root.go index b128a0d..66e23b3 100644 --- a/install/root.go +++ b/install/root.go @@ -25,6 +25,5 @@ func (root) Uninstall(cmd string, bin string) error { } func getBashCompletionDLink(cmd string) string { - return "/etc/bash_completion.d/"+cmd + return "/etc/bash_completion.d/" + cmd } - From dc4c327ae8cd5602ae10eeabde9bdf6fa5624286 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 19:47:27 +0300 Subject: [PATCH 2/4] Use local completion files and directories according to typed command --- command.go | 8 +++--- gocomplete/complete.go | 30 ++++++++++----------- gocomplete/tests.go | 2 +- predicate.go | 61 +++++++++++++++++++++++++++--------------- run_test.go | 36 ++++++++++++++----------- 5 files changed, 79 insertions(+), 58 deletions(-) diff --git a/command.go b/command.go index 7bc0fc5..fc0e28b 100644 --- a/command.go +++ b/command.go @@ -17,11 +17,11 @@ func (c *Command) options(args []string) (options []Option, only bool) { // remove the first argument, which is the command name args = args[1:] - + last := last(args) // if prev has something that needs to follow it, // it is the most relevant completion - if predicate, ok := c.Flags[last(args)]; ok && predicate != nil { - return predicate.predict(), true + if predicate, ok := c.Flags[last]; ok && predicate != nil { + return predicate.predict(last), true } sub, options, only := c.searchSub(args) @@ -41,7 +41,7 @@ func (c *Command) options(args []string) (options []Option, only bool) { } // add additional expected argument of the command - options = append(options, c.Args.predict()...) + options = append(options, c.Args.predict(last)...) return } diff --git a/gocomplete/complete.go b/gocomplete/complete.go index 159d79f..94586db 100644 --- a/gocomplete/complete.go +++ b/gocomplete/complete.go @@ -8,7 +8,7 @@ var ( predictEllipsis = complete.PredictSet("./...") goFilesOrPackages = complete.PredictFiles("**.go"). - Or(complete.PredictDirs("./")). + Or(complete.PredictDirs). Or(predictEllipsis) ) @@ -34,7 +34,7 @@ func main() { "-installsuffix": complete.PredictAnything, "-ldflags": complete.PredictAnything, "-linkshared": complete.PredictNothing, - "-pkgdir": complete.PredictDirs("./"), + "-pkgdir": complete.PredictDirs, "-tags": complete.PredictAnything, "-toolexec": complete.PredictAnything, }, @@ -45,7 +45,7 @@ func main() { Flags: complete.Flags{ "-exec": complete.PredictAnything, }, - Args: complete.PredictFiles("**.go"), + Args: complete.PredictFiles("*.go"), } test := complete.Command{ @@ -59,23 +59,23 @@ func main() { "-count": complete.PredictAnything, "-cover": complete.PredictNothing, "-covermode": complete.PredictSet("set", "count", "atomic"), - "-coverpkg": complete.PredictDirs("./"), + "-coverpkg": complete.PredictDirs, "-cpu": complete.PredictAnything, "-run": predictTest("test"), "-short": complete.PredictNothing, "-timeout": complete.PredictAnything, "-benchmem": complete.PredictNothing, - "-blockprofile": complete.PredictFiles("**.out"), + "-blockprofile": complete.PredictFiles("*.out"), "-blockprofilerate": complete.PredictAnything, - "-coverprofile": complete.PredictFiles("**.out"), - "-cpuprofile": complete.PredictFiles("**.out"), - "-memprofile": complete.PredictFiles("**.out"), + "-coverprofile": complete.PredictFiles("*.out"), + "-cpuprofile": complete.PredictFiles("*.out"), + "-memprofile": complete.PredictFiles("*.out"), "-memprofilerate": complete.PredictAnything, - "-mutexprofile": complete.PredictFiles("**.out"), + "-mutexprofile": complete.PredictFiles("*.out"), "-mutexprofilefraction": complete.PredictAnything, - "-outputdir": complete.PredictDirs("./"), - "-trace": complete.PredictFiles("**.out"), + "-outputdir": complete.PredictDirs, + "-trace": complete.PredictFiles("*.out"), }, Args: goFilesOrPackages, } @@ -115,7 +115,7 @@ func main() { "-n": complete.PredictNothing, "-x": complete.PredictNothing, }, - Args: complete.PredictDirs("./"), + Args: complete.PredictDirs, } list := complete.Command{ @@ -124,7 +124,7 @@ func main() { "-f": complete.PredictAnything, "-json": complete.PredictNothing, }, - Args: complete.PredictDirs("./"), + Args: complete.PredictDirs, } tool := complete.Command{ @@ -141,7 +141,7 @@ func main() { "-n": complete.PredictNothing, "-x": complete.PredictNothing, }, - Args: complete.PredictDirs("./"), + Args: complete.PredictDirs, } env := complete.Command{ @@ -152,7 +152,7 @@ func main() { version := complete.Command{} fix := complete.Command{ - Args: complete.PredictDirs("./"), + Args: complete.PredictDirs, } // commands that also accepts the build flags diff --git a/gocomplete/tests.go b/gocomplete/tests.go index 0ab99ec..865058f 100644 --- a/gocomplete/tests.go +++ b/gocomplete/tests.go @@ -13,7 +13,7 @@ import ( func predictTest(testType string) *complete.Predicate { return &complete.Predicate{ - Predictor: func() []complete.Option { + Predictor: func(last string) []complete.Option { tests := testNames(testType) options := make([]complete.Option, len(tests)) for i := range tests { diff --git a/predicate.go b/predicate.go index 0740cfb..a5b760e 100644 --- a/predicate.go +++ b/predicate.go @@ -9,7 +9,7 @@ import ( type Predicate struct { // Predictor is function that returns list of arguments that can // come after the flag/command - Predictor func() []Option + Predictor func(last string) []Option } // Or unions two predicate struct, so that the result predicate @@ -19,25 +19,26 @@ func (p *Predicate) Or(other *Predicate) *Predicate { return nil } return &Predicate{ - Predictor: func() []Option { return append(p.predict(), other.predict()...) }, + Predictor: func(last string) []Option { return append(p.predict(last), other.predict(last)...) }, } } -func (p *Predicate) predict() []Option { +func (p *Predicate) predict(last string) []Option { if p == nil || p.Predictor == nil { return nil } - return p.Predictor() + return p.Predictor(last) } var ( PredictNothing *Predicate = nil PredictAnything = &Predicate{} + PredictDirs = &Predicate{Predictor: dirs} ) func PredictSet(options ...string) *Predicate { return &Predicate{ - Predictor: func() []Option { + Predictor: func(last string) []Option { ret := make([]Option, len(options)) for i := range options { ret[i] = Arg(options[i]) @@ -51,29 +52,29 @@ func PredictFiles(pattern string) *Predicate { return &Predicate{Predictor: glob(pattern)} } -func PredictDirs(path string) *Predicate { - return &Predicate{Predictor: dirs(path)} +func dirs(last string) (options []Option) { + dir := dirFromLast(last) + return dirsAt(dir) } -func dirs(path string) func() []Option { - return func() (options []Option) { - dirs := []string{} - filepath.Walk(path, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - dirs = append(dirs, path) - } - return nil - }) - if !filepath.IsAbs(path) { - filesToRel(dirs) +func dirsAt(path string) []Option { + dirs := []string{} + filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + dirs = append(dirs, path) } - return filesToOptions(dirs) + return nil + }) + if !filepath.IsAbs(path) { + filesToRel(dirs) } + return filesToOptions(dirs) } -func glob(pattern string) func() []Option { - return func() []Option { - files, err := filepath.Glob(pattern) +func glob(pattern string) func(last string) []Option { + return func(last string) []Option { + dir := dirFromLast(last) + files, err := filepath.Glob(filepath.Join(dir, pattern)) if err != nil { Log("failed glob operation with pattern '%s': %s", pattern, err) } @@ -83,6 +84,7 @@ func glob(pattern string) func() []Option { return filesToOptions(files) } } + func filesToRel(files []string) { wd, err := os.Getwd() if err != nil { @@ -97,6 +99,9 @@ func filesToRel(files []string) { if err != nil { continue } + if rel == "." { + rel = "" + } files[i] = "./" + rel } return @@ -109,3 +114,15 @@ func filesToOptions(files []string) []Option { } return options } + +// dirFromLast gives the directory of the current written +// last argument if it represents a file name being written. +// in case that it is not, we fall back to the current directory. +func dirFromLast(last string) string { + dir := filepath.Dir(last) + _, err := os.Stat(dir) + if err != nil { + return "./" + } + return dir +} diff --git a/run_test.go b/run_test.go index 9800b6a..0fe52d6 100644 --- a/run_test.go +++ b/run_test.go @@ -12,6 +12,10 @@ func TestCompleter_Complete(t *testing.T) { if testing.Verbose() { os.Setenv(envDebug, "1") } + err := os.Chdir("./tests") + if err != nil { + t.Fatal(err) + } c := Command{ Sub: map[string]Command{ @@ -26,13 +30,13 @@ func TestCompleter_Complete(t *testing.T) { "-flag2": PredictNothing, "-flag3": PredictSet("opt1", "opt2", "opt12"), }, - Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")), + Args: PredictDirs.Or(PredictFiles("*.md")), }, }, Flags: map[string]*Predicate{ "-h": PredictNothing, "-global1": PredictAnything, - "-o": PredictFiles("./tests/*.txt"), + "-o": PredictFiles("*.txt"), }, } @@ -44,7 +48,7 @@ func TestCompleter_Complete(t *testing.T) { allGlobals = append(allGlobals, flag) } - testTXTFiles := []string{"./tests/a.txt", "./tests/b.txt", "./tests/c.txt"} + testTXTFiles := []string{"./a.txt", "./b.txt", "./c.txt"} tests := []struct { args string @@ -84,19 +88,19 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "sub2 ", - want: []string{"./tests", "-flag2", "-flag3", "-h", "-global1", "-o"}, + want: []string{"./", "./dir", "./readme.md", "-flag2", "-flag3", "-h", "-global1", "-o"}, }, { - args: "sub2 tests", - want: []string{"./tests", "./tests/readme.md", "./tests/dir"}, + args: "sub2 ./", + want: []string{"./", "./readme.md", "./dir"}, }, { - args: "sub2 tests/re", - want: []string{"./tests/readme.md"}, + args: "sub2 re", + want: []string{"./readme.md"}, }, { args: "sub2 -flag2 ", - want: []string{"./tests", "-flag2", "-flag3", "-h", "-global1", "-o"}, + want: []string{"./", "./dir", "./readme.md", "-flag2", "-flag3", "-h", "-global1", "-o"}, }, { args: "sub1 -fl", @@ -132,30 +136,30 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "-o ", - want: []string{}, + want: testTXTFiles, }, { - args: "-o ./tes", + args: "-o ./no-su", want: []string{}, }, { - args: "-o tests/", + args: "-o ./", want: testTXTFiles, }, { - args: "-o tests", + args: "-o ", want: testTXTFiles, }, { - args: "-o ./compl", + args: "-o ./read", want: []string{}, }, { - args: "-o ./complete.go", + args: "-o ./readme.md", want: []string{}, }, { - args: "-o ./complete.go ", + args: "-o ./readme.md ", want: allGlobals, }, { From f46c5f8a2808c5ade2f0b805a473765960250fe4 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 20:07:50 +0300 Subject: [PATCH 3/4] Change Predicate to be of function type --- command.go | 4 ++-- gocomplete/tests.go | 18 ++++++++--------- predicate.go | 48 +++++++++++++++++---------------------------- run_test.go | 11 +++++++---- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/command.go b/command.go index fc0e28b..eac9dde 100644 --- a/command.go +++ b/command.go @@ -2,13 +2,13 @@ package complete type Commands map[string]Command -type Flags map[string]*Predicate +type Flags map[string]Predicate type Command struct { Name string Sub Commands Flags Flags - Args *Predicate + Args Predicate } // options returns all available complete options for the given command diff --git a/gocomplete/tests.go b/gocomplete/tests.go index 865058f..60218a5 100644 --- a/gocomplete/tests.go +++ b/gocomplete/tests.go @@ -11,16 +11,14 @@ import ( "github.com/posener/complete" ) -func predictTest(testType string) *complete.Predicate { - return &complete.Predicate{ - Predictor: func(last string) []complete.Option { - tests := testNames(testType) - options := make([]complete.Option, len(tests)) - for i := range tests { - options[i] = complete.Arg(tests[i]) - } - return options - }, +func predictTest(testType string) complete.Predicate { + return func(last string) []complete.Option { + tests := testNames(testType) + options := make([]complete.Option, len(tests)) + for i := range tests { + options[i] = complete.Arg(tests[i]) + } + return options } } diff --git a/predicate.go b/predicate.go index a5b760e..f975e27 100644 --- a/predicate.go +++ b/predicate.go @@ -6,53 +6,41 @@ import ( ) // Predicate determines what terms can follow a command or a flag -type Predicate struct { - // Predictor is function that returns list of arguments that can - // come after the flag/command - Predictor func(last string) []Option -} +type Predicate func(last string) []Option // Or unions two predicate struct, so that the result predicate // returns the union of their predication -func (p *Predicate) Or(other *Predicate) *Predicate { +func (p Predicate) Or(other Predicate) Predicate { if p == nil || other == nil { return nil } - return &Predicate{ - Predictor: func(last string) []Option { return append(p.predict(last), other.predict(last)...) }, - } + return func(last string) []Option { return append(p.predict(last), other.predict(last)...) } } -func (p *Predicate) predict(last string) []Option { - if p == nil || p.Predictor == nil { +func (p Predicate) predict(last string) []Option { + if p == nil { return nil } - return p.Predictor(last) + return p(last) } var ( - PredictNothing *Predicate = nil - PredictAnything = &Predicate{} - PredictDirs = &Predicate{Predictor: dirs} + PredictNothing Predicate = nil ) -func PredictSet(options ...string) *Predicate { - return &Predicate{ - Predictor: func(last string) []Option { - ret := make([]Option, len(options)) - for i := range options { - ret[i] = Arg(options[i]) - } - return ret - }, - } -} +func PredictAnything(last string) []Option { return nil } -func PredictFiles(pattern string) *Predicate { - return &Predicate{Predictor: glob(pattern)} +func PredictSet(options ...string) Predicate { + return func(last string) []Option { + ret := make([]Option, len(options)) + for i := range options { + ret[i] = Arg(options[i]) + } + return ret + } } -func dirs(last string) (options []Option) { +func PredictDirs(last string) (options []Option) { dir := dirFromLast(last) return dirsAt(dir) } @@ -71,7 +59,7 @@ func dirsAt(path string) []Option { return filesToOptions(dirs) } -func glob(pattern string) func(last string) []Option { +func PredictFiles(pattern string) Predicate { return func(last string) []Option { dir := dirFromLast(last) files, err := filepath.Glob(filepath.Join(dir, pattern)) diff --git a/run_test.go b/run_test.go index 0fe52d6..4cbf36d 100644 --- a/run_test.go +++ b/run_test.go @@ -9,9 +9,12 @@ import ( func TestCompleter_Complete(t *testing.T) { t.Parallel() + // Set debug environment variable so logs will be printed if testing.Verbose() { os.Setenv(envDebug, "1") } + + // Change to tests directory for testing completion of files and directories err := os.Chdir("./tests") if err != nil { t.Fatal(err) @@ -20,20 +23,20 @@ func TestCompleter_Complete(t *testing.T) { c := Command{ Sub: map[string]Command{ "sub1": { - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-flag1": PredictAnything, "-flag2": PredictNothing, }, }, "sub2": { - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-flag2": PredictNothing, "-flag3": PredictSet("opt1", "opt2", "opt12"), }, - Args: PredictDirs.Or(PredictFiles("*.md")), + Args: Predicate(PredictDirs).Or(PredictFiles("*.md")), }, }, - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-h": PredictNothing, "-global1": PredictAnything, "-o": PredictFiles("*.txt"), From 9963a854946be0603f9e79ccba0a8b2688b20053 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 20:58:01 +0300 Subject: [PATCH 4/4] Fix travis, remove go.test.sh --- .travis.yml | 2 +- go.test.sh | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100755 go.test.sh diff --git a/.travis.yml b/.travis.yml index 965e6ad..7fef76c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ before_install: - go get -u -t ./... script: - - ./go.test.sh + - go test -v -race -coverprofile=coverage.txt -covermode=atomic after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/go.test.sh b/go.test.sh deleted file mode 100755 index 41fdfc8..0000000 --- a/go.test.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -set -e -echo "" > coverage.txt - -for d in $(go list ./... | grep -v vendor); do - go test -count 20 -v -race -coverprofile=profile.out -covermode=atomic $d - if [ -f profile.out ]; then - cat profile.out >> coverage.txt - rm profile.out - fi -done