1
2
3
4
5 package base
6
7 import (
8 "os"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "sync"
13 )
14
15 var cwd string
16 var cwdOnce sync.Once
17
18
19
20
21
22 func UncachedCwd() string {
23 wd, err := os.Getwd()
24 if err != nil {
25 Fatalf("cannot determine current directory: %v", err)
26 }
27 return wd
28 }
29
30
31 func Cwd() string {
32 cwdOnce.Do(func() {
33 cwd = UncachedCwd()
34 })
35 return cwd
36 }
37
38
39 func ShortPath(path string) string {
40 if rel, err := filepath.Rel(Cwd(), path); err == nil && len(rel) < len(path) {
41 return rel
42 }
43 return path
44 }
45
46
47
48 func RelPaths(paths []string) []string {
49 var out []string
50 for _, p := range paths {
51 rel, err := filepath.Rel(Cwd(), p)
52 if err == nil && len(rel) < len(p) {
53 p = rel
54 }
55 out = append(out, p)
56 }
57 return out
58 }
59
60
61
62 func IsTestFile(file string) bool {
63
64 return strings.HasSuffix(file, "_test.go")
65 }
66
67
68
69 func IsNull(path string) bool {
70 if path == os.DevNull {
71 return true
72 }
73 if runtime.GOOS == "windows" {
74 if strings.EqualFold(path, "NUL") {
75 return true
76 }
77 }
78 return false
79 }
80
View as plain text