forked from harness/drone-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
137 lines (119 loc) · 3.66 KB
/
helper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
// "time"
)
func convertWindowsPath(str string) string {
// Split path into Array
var pwds = strings.Split(str, "\\")
// Convert drive "C:" to "/c"
rp := regexp.MustCompile("(^[a-zA-Z]):")
pwds[0] = rp.ReplaceAllString(pwds[0], "/$1")
pwds[0] = strings.ToLower(pwds[0])
// Join path using "/"
return strings.Join(pwds, "/")
}
func parseRepo(str string) (owner, repo string, err error) {
var parts = strings.Split(str, "/")
if len(parts) != 2 {
err = fmt.Errorf("Invalid or missing repository name. " +
"Must use octocat/hello-world format.")
return
}
owner = parts[0]
repo = parts[1]
return
}
func resolvePath(dir string) string {
// attempts to fiture out the project path
// based on the GOPATH if it exists
path := os.Getenv("GOPATH")
path = filepath.Join(path, "src")
if filepath.HasPrefix(dir, path) {
return dir[len(path):]
}
// attempts to figure out the project path
// based on common directory conventions
indexes := gopathExp.FindStringIndex(dir)
if len(indexes) != 0 {
index := indexes[len(indexes)-1]
index = strings.LastIndex(dir, "/src/")
return dir[index+5:]
}
return ""
}
// readInput reads the plaintext secret from a file
// or stdin if inFile is -
func readInput(inFile string) ([]byte, error) {
if inFile == "-" {
return ioutil.ReadAll(os.Stdin)
} else {
return ioutil.ReadFile(inFile)
}
}
var gopathExp = regexp.MustCompile("./src/(github.com/[^/]+/[^/]+|bitbucket.org/[^/]+/[^/]+|code.google.com/[^/]+/[^/]+)")
// // getRepoPath checks the source codes absolute path
// // on the host operating system in an attempt
// // to correctly determine the code's package path. This
// // is Go-specific, since Go code must exist in
// // $GOPATH/src/github.com/{owner}/{name}
// func getRepoPath(dir string) (path string, ok bool) {
// // let's get the package directory based
// // on the path in the host OS
// indexes := gopathExp.FindStringIndex(dir)
// if len(indexes) == 0 {
// return
// }
// index := indexes[len(indexes)-1]
// // if the dir is /home/ubuntu/go/src/github.com/foo/bar
// // the index will start at /src/github.com/foo/bar.
// // We'll need to strip "/src/" which is where the
// // magic number 5 comes from.
// index = strings.LastIndex(dir, "/src/")
// return dir[index+5:], true
// }
// // getParamMap returns a map of enivronment variables that
// // should be injected into the .drone.yml
// func getParamMap(prefix string) map[string]string {
// envs := map[string]string{}
// for _, item := range os.Environ() {
// env := strings.SplitN(item, "=", 2)
// if len(env) != 2 {
// continue
// }
// key := env[0]
// val := env[1]
// if strings.HasPrefix(key, prefix) {
// envs[strings.TrimPrefix(key, prefix)] = val
// }
// }
// return envs
// }
// // prints the time as a human readable string
// func humanizeDuration(d time.Duration) string {
// if seconds := int(d.Seconds()); seconds < 1 {
// return "Less than a second"
// } else if seconds < 60 {
// return fmt.Sprintf("%d seconds", seconds)
// } else if minutes := int(d.Minutes()); minutes == 1 {
// return "About a minute"
// } else if minutes < 60 {
// return fmt.Sprintf("%d minutes", minutes)
// } else if hours := int(d.Hours()); hours == 1 {
// return "About an hour"
// } else if hours < 48 {
// return fmt.Sprintf("%d hours", hours)
// } else if hours < 24*7*2 {
// return fmt.Sprintf("%d days", hours/24)
// } else if hours < 24*30*3 {
// return fmt.Sprintf("%d weeks", hours/24/7)
// } else if hours < 24*365*2 {
// return fmt.Sprintf("%d months", hours/24/30)
// }
// return fmt.Sprintf("%f years", d.Hours()/24/365)
// }