github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/cmd/drone/util.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "regexp" 8 "strings" 9 "time" 10 ) 11 12 // getGoPath checks the source codes absolute path 13 // in reference to the host operating system's GOPATH 14 // to correctly determine the code's package path. This 15 // is Go-specific, since Go code must exist in 16 // $GOPATH/src/github.com/{owner}/{name} 17 func getGoPath(dir string) (string, bool) { 18 path := os.Getenv("GOPATH") 19 if len(path) == 0 { 20 return "", false 21 } 22 // append src to the GOPATH, since 23 // the code will be stored in the src dir 24 path = filepath.Join(path, "src") 25 if !filepath.HasPrefix(dir, path) { 26 return "", false 27 } 28 29 // remove the prefix from the directory 30 // this should leave us with the go package name 31 return dir[len(path):], true 32 } 33 34 var gopathExp = regexp.MustCompile("./src/(github.com/[^/]+/[^/]+|bitbucket.org/[^/]+/[^/]+|code.google.com/[^/]+/[^/]+)") 35 36 // getRepoPath checks the source codes absolute path 37 // on the host operating system in an attempt 38 // to correctly determine the code's package path. This 39 // is Go-specific, since Go code must exist in 40 // $GOPATH/src/github.com/{owner}/{name} 41 func getRepoPath(dir string) (path string, ok bool) { 42 // let's get the package directory based 43 // on the path in the host OS 44 indexes := gopathExp.FindStringIndex(dir) 45 if len(indexes) == 0 { 46 return 47 } 48 49 index := indexes[len(indexes)-1] 50 51 // if the dir is /home/ubuntu/go/src/github.com/foo/bar 52 // the index will start at /src/github.com/foo/bar. 53 // We'll need to strip "/src/" which is where the 54 // magic number 5 comes from. 55 index = strings.LastIndex(dir, "/src/") 56 return dir[index+5:], true 57 } 58 59 // getGitOrigin checks the .git origin in an attempt 60 // to correctly determine the code's package path. This 61 // is Go-specific, since Go code must exist in 62 // $GOPATH/src/github.com/{owner}/{name} 63 func getGitOrigin(dir string) (path string, ok bool) { 64 // TODO 65 return 66 } 67 68 // prints the time as a human readable string 69 func humanizeDuration(d time.Duration) string { 70 if seconds := int(d.Seconds()); seconds < 1 { 71 return "Less than a second" 72 } else if seconds < 60 { 73 return fmt.Sprintf("%d seconds", seconds) 74 } else if minutes := int(d.Minutes()); minutes == 1 { 75 return "About a minute" 76 } else if minutes < 60 { 77 return fmt.Sprintf("%d minutes", minutes) 78 } else if hours := int(d.Hours()); hours == 1 { 79 return "About an hour" 80 } else if hours < 48 { 81 return fmt.Sprintf("%d hours", hours) 82 } else if hours < 24*7*2 { 83 return fmt.Sprintf("%d days", hours/24) 84 } else if hours < 24*30*3 { 85 return fmt.Sprintf("%d weeks", hours/24/7) 86 } else if hours < 24*365*2 { 87 return fmt.Sprintf("%d months", hours/24/30) 88 } 89 return fmt.Sprintf("%f years", d.Hours()/24/365) 90 }