forked from vmware-archive/atc
/
templatefuncs.go
235 lines (192 loc) · 5.17 KB
/
templatefuncs.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package webhandler
import (
"crypto/md5"
"fmt"
"net/url"
"sync"
"github.com/concourse/atc"
"github.com/concourse/atc/auth"
"github.com/concourse/atc/web"
"github.com/concourse/go-concourse/concourse"
"github.com/tedsuo/rata"
)
type templateFuncs struct {
assetIDs map[string]string
assetsL sync.Mutex
}
func (funcs *templateFuncs) asset(asset string) (string, error) {
funcs.assetsL.Lock()
defer funcs.assetsL.Unlock()
id, found := funcs.assetIDs[asset]
if !found {
hash := md5.New()
contents, err := web.Asset("public/" + asset)
if err != nil {
return "", err
}
_, err = hash.Write(contents)
if err != nil {
return "", err
}
id = fmt.Sprintf("%x", hash.Sum(nil))
}
return funcs.url("Public", asset+"?id="+id)
}
func (funcs *templateFuncs) url(route string, args ...interface{}) (string, error) {
return PathFor(route, args...)
}
func (funcs *templateFuncs) withRedirect(authURLStr string, redirect string) string {
authURL, err := url.Parse(authURLStr)
if err != nil {
return "<malformed>"
}
withRedirect := authURL.Query()
withRedirect["redirect"] = []string{redirect}
authURL.RawQuery = withRedirect.Encode()
return authURL.String()
}
func jobName(x interface{}) string {
switch v := x.(type) {
case string:
return v
case atc.Job:
return v.Name
case atc.JobConfig:
return v.Name
default:
panic(fmt.Sprintf("unexpected arg type"))
}
}
func resourceName(x interface{}) string {
switch v := x.(type) {
case string:
return v
case atc.Resource:
return v.Name
default:
panic(fmt.Sprintf("unexpected arg type"))
}
}
func PathFor(route string, args ...interface{}) (string, error) {
switch route {
case web.TriggerBuild:
switch args[1].(type) {
case atc.Job:
return web.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
"pipeline_name": args[1].(string),
"job": jobName(args[2]),
})
default:
return web.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
"pipeline_name": args[1].(string),
"job": jobName(args[2]),
})
}
case web.GetResource:
baseResourceURL, err := web.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
"pipeline_name": args[1].(string),
"resource": resourceName(args[2]),
})
if err != nil {
return "", err
}
if len(args) > 3 {
page := args[3].(*concourse.Page)
if page.Since != 0 {
baseResourceURL += fmt.Sprintf("?since=%d", page.Since)
} else {
baseResourceURL += fmt.Sprintf("?until=%d", page.Until)
}
}
return baseResourceURL, nil
case web.GetBuilds:
path, err := web.Routes.CreatePathForRoute(route, rata.Params{})
if err != nil {
return "", err
}
if len(args) > 0 {
page := args[0].(*concourse.Page)
if page.Since != 0 {
path += fmt.Sprintf("?since=%d", page.Since)
} else {
path += fmt.Sprintf("?until=%d", page.Until)
}
}
return path, nil
case web.GetBuild:
build := args[1].(atc.Build)
build.JobName = jobName(args[0])
return web.PathForBuild(build), nil
case web.GetJoblessBuild:
return web.PathForBuild(args[0].(atc.Build)), nil
case web.Public:
return web.Routes.CreatePathForRoute(route, rata.Params{
"filename": args[0].(string),
})
case web.GetJob:
baseJobURL, err := web.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
"pipeline_name": args[1].(string),
"job": jobName(args[2]),
})
if err != nil {
return "", err
}
if len(args) > 3 {
page := args[3].(*concourse.Page)
if page.Since != 0 {
baseJobURL += fmt.Sprintf("?since=%d", page.Since)
} else {
baseJobURL += fmt.Sprintf("?until=%d", page.Until)
}
}
return baseJobURL, nil
case atc.BuildEvents:
return atc.Routes.CreatePathForRoute(route, rata.Params{
"build_id": fmt.Sprintf("%d", args[0].(atc.Build).ID),
})
case atc.EnableResourceVersion, atc.DisableResourceVersion:
versionedResource := args[2].(atc.VersionedResource)
return atc.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
"pipeline_name": args[1].(string),
"resource_name": fmt.Sprintf("%s", versionedResource.Resource),
"resource_version_id": fmt.Sprintf("%d", versionedResource.ID),
})
case web.LogIn:
return web.Routes.CreatePathForRoute(route, rata.Params{})
case web.TeamLogIn:
authPath, err := web.Routes.CreatePathForRoute(route, rata.Params{
"team_name": args[0].(string),
})
if err != nil {
return "", err
}
return authPath + "?" + url.Values{
"redirect": {args[1].(string)},
}.Encode(), nil
case atc.DownloadCLI:
path, err := atc.Routes.CreatePathForRoute(route, rata.Params{})
if err != nil {
return "", err
}
return path + "?" + url.Values{
"platform": {args[0].(string)},
"arch": {args[1].(string)},
}.Encode(), nil
case auth.OAuthBegin:
authPath, err := auth.OAuthRoutes.CreatePathForRoute(route, rata.Params{
"provider": args[0].(string),
})
if err != nil {
return "", err
}
return authPath + "?" + url.Values{
"redirect": {args[1].(string)},
}.Encode(), nil
}
return "", fmt.Errorf("unknown route: %s", route)
}