2017-05-12 16:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-18 16:11:30 -05:00
|
|
|
"go/build"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2017-05-12 16:17:48 -05:00
|
|
|
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
2017-05-19 03:59:51 -05:00
|
|
|
// predictPackages completes packages in the directory pointed by a.Last
|
|
|
|
// and packages that are one level below that package.
|
2017-05-15 14:44:19 -05:00
|
|
|
func predictPackages(a complete.Args) (prediction []string) {
|
2017-05-19 03:59:51 -05:00
|
|
|
prediction = complete.PredictFilesSet(listPackages(a.Directory())).Predict(a)
|
|
|
|
if len(prediction) != 1 {
|
2017-05-18 16:11:30 -05:00
|
|
|
return
|
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
return complete.PredictFilesSet(listPackages(prediction[0])).Predict(a)
|
|
|
|
}
|
2017-05-12 16:17:48 -05:00
|
|
|
|
2017-05-19 03:59:51 -05:00
|
|
|
// listPackages looks in current pointed dir and in all it's direct sub-packages
|
|
|
|
// and return a list of paths to go packages.
|
|
|
|
func listPackages(dir string) (directories []string) {
|
|
|
|
// add subdirectories
|
2017-05-18 16:11:30 -05:00
|
|
|
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-19 03:59:51 -05:00
|
|
|
|
|
|
|
// build paths array
|
|
|
|
paths := make([]string, 0, len(files)+1)
|
2017-05-18 16:11:30 -05:00
|
|
|
for _, f := range files {
|
2017-05-19 03:59:51 -05:00
|
|
|
if f.IsDir() {
|
|
|
|
paths = append(paths, filepath.Join(dir, f.Name()))
|
2017-05-18 16:11:30 -05:00
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
}
|
|
|
|
paths = append(paths, dir)
|
|
|
|
|
|
|
|
// import packages according to given paths
|
|
|
|
for _, p := range paths {
|
|
|
|
pkg, err := build.ImportDir(p, 0)
|
2017-05-15 14:44:19 -05:00
|
|
|
if err != nil {
|
2017-05-19 03:59:51 -05:00
|
|
|
complete.Log("failed importing directory %s: %s", p, err)
|
2017-05-15 14:44:19 -05:00
|
|
|
continue
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
directories = append(directories, pkg.Dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|