forked from vmware-archive/atc
/
path_flag.go
44 lines (33 loc) · 817 Bytes
/
path_flag.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
34
35
36
37
38
39
40
41
42
43
44
package atc
import (
"fmt"
"path/filepath"
"strings"
"github.com/jessevdk/go-flags"
)
type PathFlag string
func (path *PathFlag) UnmarshalFlag(value string) error {
if value == "" {
return nil
}
matches, err := filepath.Glob(value)
if err != nil {
return fmt.Errorf("failed to expand path '%s': %s", value, err)
}
if len(matches) == 0 {
return fmt.Errorf("path '%s' does not exist", value)
}
if len(matches) > 1 {
return fmt.Errorf("path '%s' resolves to multiple entries: %s", value, strings.Join(matches, ", "))
}
*path = PathFlag(matches[0])
return nil
}
func (path *PathFlag) Complete(match string) []flags.Completion {
matches, _ := filepath.Glob(match + "*")
comps := make([]flags.Completion, len(matches))
for i, v := range matches {
comps[i].Item = v
}
return comps
}