Skip to content

Commit

Permalink
Handle upload path correctly for windows (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham149 authored Mar 28, 2022
1 parent 338f926 commit 6a80ffa
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 58 deletions.
91 changes: 37 additions & 54 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ steps:
commands:
- ./release/linux/amd64/drone-s3 --help

- name: dryrun
pull: always
image: plugins/docker:linux-amd64
settings:
dockerfile: docker/Dockerfile.linux.amd64
dry_run: true
password:
from_secret: docker_password
repo: plugins/s3
tags: linux-amd64
username:
from_secret: docker_username
when:
event:
- pull_request

- name: publish
pull: always
image: plugins/docker:linux-amd64
Expand Down Expand Up @@ -147,22 +131,6 @@ steps:
commands:
- ./release/linux/arm64/drone-s3 --help

- name: dryrun
pull: always
image: plugins/docker:linux-arm64
settings:
dockerfile: docker/Dockerfile.linux.arm64
dry_run: true
password:
from_secret: docker_password
repo: plugins/s3
tags: linux-arm64
username:
from_secret: docker_username
when:
event:
- pull_request

- name: publish
pull: always
image: plugins/docker:linux-arm64
Expand Down Expand Up @@ -227,22 +195,6 @@ steps:
commands:
- ./release/linux/arm/drone-s3 --help

- name: dryrun
pull: always
image: plugins/docker:linux-arm
settings:
dockerfile: docker/Dockerfile.linux.arm
dry_run: true
password:
from_secret: docker_password
repo: plugins/s3
tags: linux-arm
username:
from_secret: docker_username
when:
event:
- pull_request

- name: publish
pull: always
image: plugins/docker:linux-arm
Expand All @@ -267,6 +219,35 @@ trigger:
depends_on:
- testing

---
kind: pipeline
type: ssh
name: windows-1809-amd64-pull-request

platform:
os: windows

server:
host: windows.1809.amd64.plugins.drone.ci
password:
from_secret: windows_password
user:
from_secret: windows_username

steps:
- name: build
commands:
- go build -o release/windows/amd64/drone-s3.exe
- go test -cover ./...
environment:
CGO_ENABLED: "0"

trigger:
event:
- push

depends_on:
- testing
---
kind: pipeline
type: ssh
Expand All @@ -286,6 +267,7 @@ steps:
- name: build
commands:
- go build -o release/windows/amd64/drone-s3.exe
- go test -cover ./...
- docker login -u $env:USERNAME -p $env:PASSWORD
- |
if (Test-Path env:DRONE_SEMVER_SHORT) {
Expand All @@ -303,9 +285,9 @@ steps:
from_secret: docker_password

trigger:
event:
- push
- tag
ref:
- refs/heads/master
- refs/tags/*

depends_on:
- testing
Expand All @@ -330,6 +312,7 @@ steps:
commands:
- echo $env:DRONE_SEMVER_SHORT
- go build -o release/windows/amd64/drone-s3.exe
- go test -cover ./...
- docker login -u $env:USERNAME -p $env:PASSWORD
- |
if (Test-Path env:DRONE_SEMVER_SHORT) {
Expand All @@ -347,9 +330,9 @@ steps:
from_secret: docker_password

trigger:
event:
- push
- tag
ref:
- refs/heads/master
- refs/tags/*

depends_on:
- testing
Expand Down
16 changes: 12 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ func (p *Plugin) Exec() error {
continue
}

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

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

return credentials.NewCredentials(stsProvider)
}

// resolveKey is a helper function that returns s3 object key where file present at srcPath is uploaded to.
// srcPath is assumed to be in forward slash format
func resolveKey(target, srcPath, stripPrefix string) string {
key := filepath.Join(target, strings.TrimPrefix(srcPath, filepath.ToSlash(stripPrefix)))
key = filepath.ToSlash(key)
if !strings.HasPrefix(key, "/") {
key = "/" + key
}
return key
}
46 changes: 46 additions & 0 deletions plugin_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris

package main

import (
"testing"
)

func TestResolveUnixKey(t *testing.T) {
tests := []struct {
name string
target string
srcPath string
stripPrefix string
expected string
}{
{
name: "target not set",
target: "",
srcPath: "/foo/bar",
stripPrefix: "/foo",
expected: "/bar",
},
{
name: "strip prefix not set",
target: "/hello",
srcPath: "/foo/bar",
stripPrefix: "",
expected: "/hello/foo/bar",
},
{
name: "everything set",
target: "hello",
srcPath: "/foo/bar",
stripPrefix: "/foo",
expected: "/hello/bar",
},
}

for _, tc := range tests {
got := resolveKey(tc.target, tc.srcPath, tc.stripPrefix)
if tc.expected != got {
t.Fatalf("%s: expected error: %v, got: %v", tc.name, tc.expected, got)
}
}
}
60 changes: 60 additions & 0 deletions plugin_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:build windows

package main

import (
"testing"
)

func TestResolveWinKey(t *testing.T) {
tests := []struct {
name string
target string
srcPath string
stripPrefix string
expected string
}{
{
name: "target not set",
target: "",
srcPath: "/foo/bar",
stripPrefix: "/foo",
expected: "/bar",
},
{
name: "strip prefix not set",
target: "/hello",
srcPath: "/foo/bar",
stripPrefix: "",
expected: "/hello/foo/bar",
},
{
name: "everything set",
target: "hello",
srcPath: "/foo/bar",
stripPrefix: "/foo",
expected: "/hello/bar",
},
{
name: "backslash strip prefix",
target: "hello",
srcPath: `foo/bar/world`,
stripPrefix: `foo\bar`,
expected: "/hello/world",
},
{
name: "forward slash strip prefix",
target: "hello",
srcPath: "foo/bar/world",
stripPrefix: `foo/bar`,
expected: "/hello/world",
},
}

for _, tc := range tests {
got := resolveKey(tc.target, tc.srcPath, tc.stripPrefix)
if tc.expected != got {
t.Fatalf("%s: expected error: %v, got: %v", tc.name, tc.expected, got)
}
}
}

0 comments on commit 6a80ffa

Please sign in to comment.