1
2
3
4
5 package base
6
7 import (
8 "os"
9 "path/filepath"
10 "strings"
11 "sync"
12 )
13
14 var cwd string
15 var cwdOnce sync.Once
16
17
18 func Cwd() string {
19 cwdOnce.Do(func() {
20 var err error
21 cwd, err = os.Getwd()
22 if err != nil {
23 Fatalf("cannot determine current directory: %v", err)
24 }
25 })
26 return cwd
27 }
28
29
30 func ShortPath(path string) string {
31 if rel, err := filepath.Rel(Cwd(), path); err == nil && len(rel) < len(path) {
32 return rel
33 }
34 return path
35 }
36
37
38
39 func RelPaths(paths []string) []string {
40 var out []string
41 for _, p := range paths {
42 rel, err := filepath.Rel(Cwd(), p)
43 if err == nil && len(rel) < len(p) {
44 p = rel
45 }
46 out = append(out, p)
47 }
48 return out
49 }
50
51
52
53 func IsTestFile(file string) bool {
54
55 return strings.HasSuffix(file, "_test.go")
56 }
57
View as plain text