2017-05-12 16:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-18 16:11:30 -05:00
|
|
|
"go/build"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-12 16:17:48 -05:00
|
|
|
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
2017-05-15 14:44:19 -05:00
|
|
|
func predictPackages(a complete.Args) (prediction []string) {
|
2017-05-18 16:11:30 -05:00
|
|
|
for {
|
|
|
|
prediction = complete.PredictFilesSet(listPackages(a)).Predict(a)
|
|
|
|
|
|
|
|
// if the number of prediction is not 1, we either have many results or
|
|
|
|
// have no results, so we return it.
|
|
|
|
if len(prediction) != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the result is only one item, we might want to recursively check
|
|
|
|
// for more accurate results.
|
|
|
|
if prediction[0] == a.Last {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only try deeper, if the one item is a directory
|
|
|
|
if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() {
|
|
|
|
return
|
|
|
|
}
|
2017-05-12 16:17:48 -05:00
|
|
|
|
2017-05-18 16:11:30 -05:00
|
|
|
a.Last = prediction[0]
|
2017-05-15 14:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:11:30 -05:00
|
|
|
func listPackages(a complete.Args) (dirctories []string) {
|
|
|
|
dir := a.Directory()
|
|
|
|
complete.Log("listing packages in %s", dir)
|
|
|
|
// import current directory
|
|
|
|
pkg, err := build.ImportDir(dir, 0)
|
|
|
|
if err != nil {
|
|
|
|
complete.Log("failed importing directory %s: %s", dir, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dirctories = append(dirctories, pkg.Dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
|
2017-05-18 16:11:30 -05:00
|
|
|
// import subdirectories
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
if err != nil {
|
2017-05-18 16:11:30 -05:00
|
|
|
complete.Log("failed reading directory %s: %s", dir, err)
|
2017-05-12 16:17:48 -05:00
|
|
|
return
|
|
|
|
}
|
2017-05-18 16:11:30 -05:00
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pkg, err := build.ImportDir(filepath.Join(dir, f.Name()), 0)
|
2017-05-15 14:44:19 -05:00
|
|
|
if err != nil {
|
2017-05-18 16:11:30 -05:00
|
|
|
complete.Log("failed importing subdirectory %s: %s", filepath.Join(dir, f.Name()), err)
|
2017-05-15 14:44:19 -05:00
|
|
|
continue
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
2017-05-18 16:11:30 -05:00
|
|
|
dirctories = append(dirctories, pkg.Dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|