-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
condition.go
68 lines (60 loc) · 1.64 KB
/
condition.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package condition
import (
"os"
"os/exec"
"runtime"
"strings"
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/util"
)
// Arch returns true if any of the given strings matches `runtime.GOARCH`.
func Arch(s ...string) func(c carapace.Context) bool {
return func(c carapace.Context) bool {
for _, arch := range s {
if arch == runtime.GOARCH {
return true
}
}
return false
}
}
// Arch returns true if any of the given strings matches `runtime.GOOS`.
func Os(s ...string) func(c carapace.Context) bool {
return func(c carapace.Context) bool {
for _, os := range s {
if os == runtime.GOOS {
return true
}
}
return false
}
}
// Excutable returns true if any of the given strings matches an executable in PATH.
func Executable(s ...string) func(c carapace.Context) bool {
return func(c carapace.Context) bool {
for _, executable := range s {
// TODO needs to use Context.Env
if _, err := exec.LookPath(executable); err == nil {
return true
}
}
return false
}
}
// Retuns true if given string is a valid file or directory.
func File(s string) func(c carapace.Context) bool {
return func(c carapace.Context) bool {
if _, err := os.Stat(s); err == nil {
return true
}
return false
}
}
// CompletingPath returns true when `Context.Value` has a path prefix.
func CompletingPath(c carapace.Context) bool {
return util.HasPathPrefix(c.Value)
}
// CompletingPathS is like CompletingPathS but also checks for path separator `/`
func CompletingPathS(c carapace.Context) bool {
return CompletingPath(c) || strings.Contains(c.Value, "/") // TODO support windows backslash at some point?
}