Skip to content

Commit 6a80ffa

Browse files
authored
Handle upload path correctly for windows (#88)
1 parent 338f926 commit 6a80ffa

File tree

4 files changed

+155
-58
lines changed

4 files changed

+155
-58
lines changed

.drone.yml

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,6 @@ steps:
6767
commands:
6868
- ./release/linux/amd64/drone-s3 --help
6969

70-
- name: dryrun
71-
pull: always
72-
image: plugins/docker:linux-amd64
73-
settings:
74-
dockerfile: docker/Dockerfile.linux.amd64
75-
dry_run: true
76-
password:
77-
from_secret: docker_password
78-
repo: plugins/s3
79-
tags: linux-amd64
80-
username:
81-
from_secret: docker_username
82-
when:
83-
event:
84-
- pull_request
85-
8670
- name: publish
8771
pull: always
8872
image: plugins/docker:linux-amd64
@@ -147,22 +131,6 @@ steps:
147131
commands:
148132
- ./release/linux/arm64/drone-s3 --help
149133

150-
- name: dryrun
151-
pull: always
152-
image: plugins/docker:linux-arm64
153-
settings:
154-
dockerfile: docker/Dockerfile.linux.arm64
155-
dry_run: true
156-
password:
157-
from_secret: docker_password
158-
repo: plugins/s3
159-
tags: linux-arm64
160-
username:
161-
from_secret: docker_username
162-
when:
163-
event:
164-
- pull_request
165-
166134
- name: publish
167135
pull: always
168136
image: plugins/docker:linux-arm64
@@ -227,22 +195,6 @@ steps:
227195
commands:
228196
- ./release/linux/arm/drone-s3 --help
229197

230-
- name: dryrun
231-
pull: always
232-
image: plugins/docker:linux-arm
233-
settings:
234-
dockerfile: docker/Dockerfile.linux.arm
235-
dry_run: true
236-
password:
237-
from_secret: docker_password
238-
repo: plugins/s3
239-
tags: linux-arm
240-
username:
241-
from_secret: docker_username
242-
when:
243-
event:
244-
- pull_request
245-
246198
- name: publish
247199
pull: always
248200
image: plugins/docker:linux-arm
@@ -267,6 +219,35 @@ trigger:
267219
depends_on:
268220
- testing
269221

222+
---
223+
kind: pipeline
224+
type: ssh
225+
name: windows-1809-amd64-pull-request
226+
227+
platform:
228+
os: windows
229+
230+
server:
231+
host: windows.1809.amd64.plugins.drone.ci
232+
password:
233+
from_secret: windows_password
234+
user:
235+
from_secret: windows_username
236+
237+
steps:
238+
- name: build
239+
commands:
240+
- go build -o release/windows/amd64/drone-s3.exe
241+
- go test -cover ./...
242+
environment:
243+
CGO_ENABLED: "0"
244+
245+
trigger:
246+
event:
247+
- push
248+
249+
depends_on:
250+
- testing
270251
---
271252
kind: pipeline
272253
type: ssh
@@ -286,6 +267,7 @@ steps:
286267
- name: build
287268
commands:
288269
- go build -o release/windows/amd64/drone-s3.exe
270+
- go test -cover ./...
289271
- docker login -u $env:USERNAME -p $env:PASSWORD
290272
- |
291273
if (Test-Path env:DRONE_SEMVER_SHORT) {
@@ -303,9 +285,9 @@ steps:
303285
from_secret: docker_password
304286

305287
trigger:
306-
event:
307-
- push
308-
- tag
288+
ref:
289+
- refs/heads/master
290+
- refs/tags/*
309291

310292
depends_on:
311293
- testing
@@ -330,6 +312,7 @@ steps:
330312
commands:
331313
- echo $env:DRONE_SEMVER_SHORT
332314
- go build -o release/windows/amd64/drone-s3.exe
315+
- go test -cover ./...
333316
- docker login -u $env:USERNAME -p $env:PASSWORD
334317
- |
335318
if (Test-Path env:DRONE_SEMVER_SHORT) {
@@ -347,9 +330,9 @@ steps:
347330
from_secret: docker_password
348331

349332
trigger:
350-
event:
351-
- push
352-
- tag
333+
ref:
334+
- refs/heads/master
335+
- refs/tags/*
353336

354337
depends_on:
355338
- testing

plugin.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ func (p *Plugin) Exec() error {
161161
continue
162162
}
163163

164-
target := filepath.Join(p.Target, strings.TrimPrefix(match, p.StripPrefix))
165-
if !strings.HasPrefix(target, "/") {
166-
target = "/" + target
167-
}
164+
target := resolveKey(p.Target, match, p.StripPrefix)
168165

169166
contentType := matchExtension(match, p.ContentType)
170167
contentEncoding := matchExtension(match, p.ContentEncoding)
@@ -310,3 +307,14 @@ func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
310307

311308
return credentials.NewCredentials(stsProvider)
312309
}
310+
311+
// resolveKey is a helper function that returns s3 object key where file present at srcPath is uploaded to.
312+
// srcPath is assumed to be in forward slash format
313+
func resolveKey(target, srcPath, stripPrefix string) string {
314+
key := filepath.Join(target, strings.TrimPrefix(srcPath, filepath.ToSlash(stripPrefix)))
315+
key = filepath.ToSlash(key)
316+
if !strings.HasPrefix(key, "/") {
317+
key = "/" + key
318+
}
319+
return key
320+
}

plugin_unix_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
2+
3+
package main
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func TestResolveUnixKey(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
target string
13+
srcPath string
14+
stripPrefix string
15+
expected string
16+
}{
17+
{
18+
name: "target not set",
19+
target: "",
20+
srcPath: "/foo/bar",
21+
stripPrefix: "/foo",
22+
expected: "/bar",
23+
},
24+
{
25+
name: "strip prefix not set",
26+
target: "/hello",
27+
srcPath: "/foo/bar",
28+
stripPrefix: "",
29+
expected: "/hello/foo/bar",
30+
},
31+
{
32+
name: "everything set",
33+
target: "hello",
34+
srcPath: "/foo/bar",
35+
stripPrefix: "/foo",
36+
expected: "/hello/bar",
37+
},
38+
}
39+
40+
for _, tc := range tests {
41+
got := resolveKey(tc.target, tc.srcPath, tc.stripPrefix)
42+
if tc.expected != got {
43+
t.Fatalf("%s: expected error: %v, got: %v", tc.name, tc.expected, got)
44+
}
45+
}
46+
}

plugin_windows_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build windows
2+
3+
package main
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func TestResolveWinKey(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
target string
13+
srcPath string
14+
stripPrefix string
15+
expected string
16+
}{
17+
{
18+
name: "target not set",
19+
target: "",
20+
srcPath: "/foo/bar",
21+
stripPrefix: "/foo",
22+
expected: "/bar",
23+
},
24+
{
25+
name: "strip prefix not set",
26+
target: "/hello",
27+
srcPath: "/foo/bar",
28+
stripPrefix: "",
29+
expected: "/hello/foo/bar",
30+
},
31+
{
32+
name: "everything set",
33+
target: "hello",
34+
srcPath: "/foo/bar",
35+
stripPrefix: "/foo",
36+
expected: "/hello/bar",
37+
},
38+
{
39+
name: "backslash strip prefix",
40+
target: "hello",
41+
srcPath: `foo/bar/world`,
42+
stripPrefix: `foo\bar`,
43+
expected: "/hello/world",
44+
},
45+
{
46+
name: "forward slash strip prefix",
47+
target: "hello",
48+
srcPath: "foo/bar/world",
49+
stripPrefix: `foo/bar`,
50+
expected: "/hello/world",
51+
},
52+
}
53+
54+
for _, tc := range tests {
55+
got := resolveKey(tc.target, tc.srcPath, tc.stripPrefix)
56+
if tc.expected != got {
57+
t.Fatalf("%s: expected error: %v, got: %v", tc.name, tc.expected, got)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)