forked from sourcegraph/srclib
/
misc.go
33 lines (28 loc) · 773 Bytes
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package src
import (
"log"
"os"
"path/filepath"
"sourcegraph.com/sourcegraph/go-flags"
)
// Directory is flags.Completer that provides directory name completion.
//
// TODO(sqs): this is annoying. it only completes the dir name and doesn't let
// you keep typing the arg.
type Directory string
// Complete implements flags.Completer and returns a list of existing
// directories with the given prefix.
func (d Directory) Complete(match string) []flags.Completion {
names, err := filepath.Glob(match + "*")
if err != nil {
log.Println(err)
return nil
}
var dirs []flags.Completion
for _, name := range names {
if fi, err := os.Stat(name); err == nil && fi.Mode().IsDir() {
dirs = append(dirs, flags.Completion{Item: name + "/"})
}
}
return dirs
}