github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/command/path.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package cmd 4 5 import ( 6 "os" 7 "path/filepath" 8 "strings" 9 ) 10 11 var ( 12 cwd string 13 // Executable returns the path name for the executable that started the current process 14 ed string 15 ) 16 17 func init() { 18 var ex, err = os.Executable() 19 if err != nil { 20 panic("cannot determine executable directory: " + err.Error()) 21 } 22 ed = filepath.Dir(ex) 23 24 cwd, err = os.Getwd() 25 if err != nil { 26 panic("cannot determine current working directory: " + err.Error()) 27 } 28 29 // TODO::: 3 way to indicate current working directory 30 // fmt.Println(os.Executable()) 31 // fmt.Println(os.Getwd()) 32 // fmt.Println(os.Args[0]) 33 } 34 35 // Cwd returns the current working directory at the time of the first call. 36 func Cwd() string { return cwd } 37 38 // ED or Executable Directory returns the path name for the executable that started the current process 39 func ED() string { return ed } 40 41 // ShortPath returns an absolute or relative name for path, whatever is shorter. 42 func ShortPath(path string) string { 43 if rel, err := filepath.Rel(Cwd(), path); err == nil && len(rel) < len(path) { 44 return rel 45 } 46 return path 47 } 48 49 // RelPaths returns a copy of paths with absolute paths 50 // made relative to the current directory if they would be shorter. 51 func RelPaths(paths []string) []string { 52 var out []string 53 for _, p := range paths { 54 rel, err := filepath.Rel(Cwd(), p) 55 if err == nil && len(rel) < len(p) { 56 p = rel 57 } 58 out = append(out, p) 59 } 60 return out 61 } 62 63 // IsTestFile reports whether the source file is a set of tests and should therefore 64 // be excluded from coverage analysis. 65 func IsTestFile(file string) bool { 66 // We don't cover tests, only the code they test. 67 return strings.HasSuffix(file, "_test.go") 68 }