forked from donny-dont/drone-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
74 lines (61 loc) · 1.75 KB
/
path.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
package path
import (
"net/url"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
// default root directory where the project workspace
// is located. All projects must be cloned to this
// directory or one of its childen.
const DefaultRoot = "/drone/src"
// config represents a simple Yaml config file with the clone
// section and the path attribute. This is used to quickly
// extract only the path.
type config struct {
Clone struct {
Path string
}
}
// Parse parses a yaml file to find the default
// workspace path. If empty, the default uri is
// used to determine the workspace.
func Parse(raw, rawurl string) string {
data := config{}
path := FromUrl(rawurl)
// unarmshal into the temporary Yaml object
yaml.Unmarshal([]byte(raw), &data)
// if no clone path is found, return the default
// workspace path, joined with the root workspace.
if len(data.Clone.Path) != 0 {
path = data.Clone.Path
}
if filepath.HasPrefix(path, DefaultRoot) {
return path
}
// otherwise return the clone path, joined with the
// root workspace. Note that this means the clone
// path must be a relative path.
return filepath.Join(DefaultRoot, path)
}
// FromUrl returns a workspace path from the url.
func FromUrl(rawurl string) string {
url_, err := url.Parse(rawurl)
if err != nil {
return string(filepath.Separator)
}
// uses only the host and port
// section of the url. The query parameter,
// fragment and scheme must be ignored
host := url_.Host
path := url_.Path
// remove the colon from the hostname to avoid
// issues with Docker volumes and caching.
parts := strings.Split(host, ":")
if len(parts) == 2 {
host = parts[0]
}
path = filepath.ToSlash(path) // just for windows
path = filepath.Clean(path)
return filepath.Join(host, path)
}