diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c13ca3f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +* +!release/ diff --git a/.drone.jsonnet b/.drone.jsonnet deleted file mode 100644 index 8fa9ce1..0000000 --- a/.drone.jsonnet +++ /dev/null @@ -1,14 +0,0 @@ -local pipeline = import 'pipeline.libsonnet'; -local name = 'drone-download'; - -[ - pipeline.test('linux', 'amd64'), - pipeline.build(name, 'linux', 'amd64'), - pipeline.build(name, 'linux', 'arm64'), - pipeline.build(name, 'linux', 'arm'), - pipeline.notifications(depends_on=[ - 'linux-amd64', - 'linux-arm64', - 'linux-arm', - ]), -] diff --git a/.drone.star b/.drone.star new file mode 100644 index 0000000..d043624 --- /dev/null +++ b/.drone.star @@ -0,0 +1,357 @@ +def main(ctx): + before = testing(ctx) + + stages = [ + linux(ctx, 'amd64'), + linux(ctx, 'arm64'), + linux(ctx, 'arm'), + windows(ctx, '1903'), + windows(ctx, '1809'), + ] + + after = manifest(ctx) + gitter(ctx) + + for b in before: + for s in stages: + s['depends_on'].append(b['name']) + + for s in stages: + for a in after: + a['depends_on'].append(s['name']) + + return before + stages + after + +def testing(ctx): + return [{ + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'testing', + 'platform': { + 'os': 'linux', + 'arch': 'amd64', + }, + 'steps': [ + { + 'name': 'staticcheck', + 'image': 'golang:1.14', + 'pull': 'always', + 'commands': [ + 'go run honnef.co/go/tools/cmd/staticcheck ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], + }, + { + 'name': 'lint', + 'image': 'golang:1.14', + 'pull': 'always', + 'commands': [ + 'go run golang.org/x/lint/golint -set_exit_status ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], + }, + { + 'name': 'vet', + 'image': 'golang:1.14', + 'pull': 'always', + 'commands': [ + 'go vet ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], + }, + { + 'name': 'test', + 'image': 'golang:1.14', + 'pull': 'always', + 'commands': [ + 'go test -cover ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], + }, + ], + 'volumes': [ + { + 'name': 'gopath', + 'temp': {}, + }, + ], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + 'refs/pull/**', + ], + }, + }] + +def linux(ctx, arch): + docker = { + 'dockerfile': 'docker/Dockerfile.linux.%s' % (arch), + 'repo': 'plugins/download', + 'username': { + 'from_secret': 'docker_username', + }, + 'password': { + 'from_secret': 'docker_password', + }, + } + + if ctx.build.event == 'pull_request': + docker.update({ + 'dry_run': True, + 'tags': 'linux-%s' % (arch), + }) + else: + docker.update({ + 'auto_tag': True, + 'auto_tag_suffix': 'linux-%s' % (arch), + }) + + if ctx.build.event == 'tag': + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-download ./cmd/drone-download' % (ctx.build.ref.replace("refs/tags/v", ""), arch), + ] + else: + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-download ./cmd/drone-download' % (ctx.build.commit[0:8], arch), + ] + + return { + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'linux-%s' % (arch), + 'platform': { + 'os': 'linux', + 'arch': arch, + }, + 'steps': [ + { + 'name': 'environment', + 'image': 'golang:1.14', + 'pull': 'always', + 'environment': { + 'CGO_ENABLED': '0', + }, + 'commands': [ + 'go version', + 'go env', + ], + }, + { + 'name': 'build', + 'image': 'golang:1.14', + 'pull': 'always', + 'environment': { + 'CGO_ENABLED': '0', + }, + 'commands': build, + }, + { + 'name': 'executable', + 'image': 'golang:1.14', + 'pull': 'always', + 'commands': [ + './release/linux/%s/drone-download --help' % (arch), + ], + }, + { + 'name': 'docker', + 'image': 'plugins/docker', + 'pull': 'always', + 'settings': docker, + }, + ], + 'depends_on': [], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + 'refs/pull/**', + ], + }, + } + +def windows(ctx, version): + docker = [ + 'echo $env:PASSWORD | docker login --username $env:USERNAME --password-stdin', + ] + + if ctx.build.event == 'tag': + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-download.exe ./cmd/drone-download' % (ctx.build.ref.replace("refs/tags/v", "")), + ] + + docker = docker + [ + 'docker build --pull -f docker/Dockerfile.windows.%s -t plugins/download:%s-windows-%s-amd64 .' % (version, ctx.build.ref.replace("refs/tags/v", ""), version), + 'docker run --rm plugins/download:%s-windows-%s-amd64 --help' % (ctx.build.ref.replace("refs/tags/v", ""), version), + 'docker push plugins/download:%s-windows-%s-amd64' % (ctx.build.ref.replace("refs/tags/v", ""), version), + ] + else: + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-download.exe ./cmd/drone-download' % (ctx.build.commit[0:8]), + ] + + docker = docker + [ + 'docker build --pull -f docker/Dockerfile.windows.%s -t plugins/download:windows-%s-amd64 .' % (version, version), + 'docker run --rm plugins/download:windows-%s-amd64 --help' % (version), + 'docker push plugins/download:windows-%s-amd64' % (version), + ] + + return { + 'kind': 'pipeline', + 'type': 'ssh', + 'name': 'windows-%s' % (version), + 'platform': { + 'os': 'windows', + }, + 'server': { + 'host': { + 'from_secret': 'windows_server_%s' % (version), + }, + 'user': { + 'from_secret': 'windows_username', + }, + 'password': { + 'from_secret': 'windows_password', + }, + }, + 'steps': [ + { + 'name': 'environment', + 'environment': { + 'CGO_ENABLED': '0', + }, + 'commands': [ + 'go version', + 'go env', + ], + }, + { + 'name': 'build', + 'environment': { + 'CGO_ENABLED': '0', + }, + 'commands': build, + }, + { + 'name': 'executable', + 'commands': [ + './release/windows/amd64/drone-download.exe --help', + ], + }, + { + 'name': 'docker', + 'environment': { + 'USERNAME': { + 'from_secret': 'docker_username', + }, + 'PASSWORD': { + 'from_secret': 'docker_password', + }, + }, + 'commands': docker, + }, + ], + 'depends_on': [], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + ], + }, + } + +def manifest(ctx): + return [{ + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'manifest', + 'steps': [ + { + 'name': 'manifest', + 'image': 'plugins/manifest', + 'pull': 'always', + 'settings': { + 'auto_tag': 'true', + 'username': { + 'from_secret': 'docker_username', + }, + 'password': { + 'from_secret': 'docker_password', + }, + 'spec': 'docker/manifest.tmpl', + 'ignore_missing': 'true', + }, + }, + { + 'name': 'microbadger', + 'image': 'plugins/webhook', + 'pull': 'always', + 'settings': { + 'urls': { + 'from_secret': 'microbadger_url', + }, + }, + }, + ], + 'depends_on': [], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + ], + }, + }] + +def gitter(ctx): + return [{ + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'gitter', + 'clone': { + 'disable': True, + }, + 'steps': [ + { + 'name': 'gitter', + 'image': 'plugins/gitter', + 'pull': 'always', + 'settings': { + 'webhook': { + 'from_secret': 'gitter_webhook', + } + }, + }, + ], + 'depends_on': [ + 'manifest', + ], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + ], + 'status': [ + 'failure', + ], + }, + }] diff --git a/.drone.windows.jsonnet b/.drone.windows.jsonnet deleted file mode 100644 index bf03895..0000000 --- a/.drone.windows.jsonnet +++ /dev/null @@ -1,9 +0,0 @@ -local pipeline = import 'pipeline.libsonnet'; -local name = 'drone-download'; - -[ - pipeline.test('windows', 'amd64', '1803'), - pipeline.build(name, 'windows', 'amd64', '1803'), - pipeline.build(name, 'windows', 'amd64', '1809'), - pipeline.notifications('windows', 'amd64', '1809', ['windows-1803', 'windows-1809']), -] diff --git a/.drone.windows.yml b/.drone.windows.yml deleted file mode 100644 index edd2823..0000000 --- a/.drone.windows.yml +++ /dev/null @@ -1,273 +0,0 @@ ---- -kind: pipeline -name: testing - -platform: - os: windows - arch: amd64 - version: 1803 - -steps: -- name: vet - pull: always - image: golang:1.11-windowsservercore-1803 - commands: - - go vet ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: C:\\gopath - -- name: test - pull: always - image: golang:1.11-windowsservercore-1803 - commands: - - go test -cover ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: C:\\gopath - -volumes: -- name: gopath - temp: {} - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - ---- -kind: pipeline -name: windows-1803 - -platform: - os: windows - arch: amd64 - version: 1803 - -steps: -- name: build-push - pull: always - image: golang:1.11-windowsservercore-1803 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-download.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.11-windowsservercore-1803 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-download.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.11-windowsservercore-1803 - commands: - - ./release/windows/amd64/drone-download.exe --help - -- name: dryrun - pull: always - image: plugins/docker:windows-1803 - settings: - daemon_off: true - dockerfile: docker/Dockerfile.windows.1803 - dry_run: true - password: - from_secret: docker_password - repo: plugins/download - tags: windows-1803 - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:windows-1803 - settings: - auto_tag: true - auto_tag_suffix: windows-1803 - daemon_off: true - dockerfile: docker/Dockerfile.windows.1803 - password: - from_secret: docker_password - repo: plugins/download - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - exclude: - - pull_request - -volumes: -- name: docker_pipe - host: - path: \\\\.\\pipe\\docker_engine - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: windows-1809 - -platform: - os: windows - arch: amd64 - version: 1809 - -steps: -- name: build-push - pull: always - image: golang:1.11-windowsservercore-1809 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-download.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.11-windowsservercore-1809 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-download.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.11-windowsservercore-1809 - commands: - - ./release/windows/amd64/drone-download.exe --help - -- name: dryrun - pull: always - image: plugins/docker:windows-1809 - settings: - daemon_off: true - dockerfile: docker/Dockerfile.windows.1809 - dry_run: true - password: - from_secret: docker_password - repo: plugins/download - tags: windows-1809 - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:windows-1809 - settings: - auto_tag: true - auto_tag_suffix: windows-1809 - daemon_off: true - dockerfile: docker/Dockerfile.windows.1809 - password: - from_secret: docker_password - repo: plugins/download - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - exclude: - - pull_request - -volumes: -- name: docker_pipe - host: - path: \\\\.\\pipe\\docker_engine - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: notifications - -platform: - os: windows - arch: amd64 - version: 1809 - -steps: -- name: manifest - pull: always - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: docker_password - spec: docker/manifest.tmpl - username: - from_secret: docker_username - -- name: microbadger - pull: always - image: plugins/webhook - settings: - urls: - from_secret: microbadger_url - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - -depends_on: -- windows-1803 -- windows-1809 - -... diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 47d35c5..0000000 --- a/.drone.yml +++ /dev/null @@ -1,332 +0,0 @@ ---- -kind: pipeline -name: testing - -platform: - os: linux - arch: amd64 - -steps: -- name: vet - pull: always - image: golang:1.11 - commands: - - go vet ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: /go - -- name: test - pull: always - image: golang:1.11 - commands: - - go test -cover ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: /go - -volumes: -- name: gopath - temp: {} - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - ---- -kind: pipeline -name: linux-amd64 - -platform: - os: linux - arch: amd64 - -steps: -- name: build-push - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/amd64/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/amd64/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.11 - commands: - - ./release/linux/amd64/drone-download --help - -- name: dryrun - pull: always - image: plugins/docker:linux-amd64 - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.amd64 - dry_run: true - password: - from_secret: docker_password - repo: plugins/download - tags: linux-amd64 - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-amd64 - settings: - auto_tag: true - auto_tag_suffix: linux-amd64 - daemon_off: false - dockerfile: docker/Dockerfile.linux.amd64 - password: - from_secret: docker_password - repo: plugins/download - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: linux-arm64 - -platform: - os: linux - arch: arm64 - -steps: -- name: build-push - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm64/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm64/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.11 - commands: - - ./release/linux/arm64/drone-download --help - -- name: dryrun - pull: always - image: plugins/docker:linux-arm64 - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm64 - dry_run: true - password: - from_secret: docker_password - repo: plugins/download - tags: linux-arm64 - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-arm64 - settings: - auto_tag: true - auto_tag_suffix: linux-arm64 - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm64 - password: - from_secret: docker_password - repo: plugins/download - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: linux-arm - -platform: - os: linux - arch: arm - -steps: -- name: build-push - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.11 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm/drone-download" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.11 - commands: - - ./release/linux/arm/drone-download --help - -- name: dryrun - pull: always - image: plugins/docker:linux-arm - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm - dry_run: true - password: - from_secret: docker_password - repo: plugins/download - tags: linux-arm - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-arm - settings: - auto_tag: true - auto_tag_suffix: linux-arm - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm - password: - from_secret: docker_password - repo: plugins/download - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: notifications - -platform: - os: linux - arch: amd64 - -steps: -- name: manifest - pull: always - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: docker_password - spec: docker/manifest.tmpl - username: - from_secret: docker_username - -- name: microbadger - pull: always - image: plugins/webhook - settings: - urls: - from_secret: microbadger_url - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - -depends_on: -- linux-amd64 -- linux-arm64 -- linux-arm - -... diff --git a/.gitignore b/.gitignore index 9d91c70..ea3d9d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,30 +1,5 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof - -release/ -vendor/ +/release/ +/drone-download* coverage.out -drone-download +.drone.yml diff --git a/LICENSE b/LICENSE index 8f71f43..8dada3e 100644 --- a/LICENSE +++ b/LICENSE @@ -199,4 +199,3 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - diff --git a/README.md b/README.md index 3a8cef8..8c40f76 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ export GOARCH=amd64 export CGO_ENABLED=0 export GO111MODULE=on -go build -v -a -tags netgo -o release/linux/amd64/drone-download +go build -v -a -tags netgo -o release/linux/amd64/drone-download ./cmd/drone-download ``` ## Docker diff --git a/cmd/drone-download/config.go b/cmd/drone-download/config.go new file mode 100644 index 0000000..dcde2c5 --- /dev/null +++ b/cmd/drone-download/config.go @@ -0,0 +1,59 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package main + +import ( + "github.com/drone-plugins/drone-download/plugin" + "github.com/urfave/cli/v2" +) + +// settingsFlags has the cli.Flags for the plugin.Settings. +func settingsFlags(settings *plugin.Settings) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "source", + Usage: "source url for the download", + EnvVars: []string{"PLUGIN_SOURCE"}, + Destination: &settings.Source, + }, + &cli.StringFlag{ + Name: "destination", + Usage: "destination for the download", + EnvVars: []string{"PLUGIN_DESTINATION"}, + Destination: &settings.Destination, + }, + &cli.StringFlag{ + Name: "authorization", + Usage: "value to send in the authorization header", + EnvVars: []string{"PLUGIN_AUTHORIZATION", "DOWNLOAD_AUTHORIZATION"}, + Destination: &settings.Authorization, + }, + &cli.StringFlag{ + Name: "username", + Usage: "username for basic auth", + EnvVars: []string{"PLUGIN_USERNAME", "DOWNLOAD_USERNAME"}, + Destination: &settings.Username, + }, + &cli.StringFlag{ + Name: "password", + Usage: "password for basic auth", + EnvVars: []string{"PLUGIN_PASSWORD", "DOWNLOAD_PASSWORD"}, + Destination: &settings.Password, + }, + &cli.StringFlag{ + Name: "md5-checksum", + Usage: "checksum in md5 format", + EnvVars: []string{"PLUGIN_MD5"}, + Destination: &settings.MD5, + }, + &cli.StringFlag{ + Name: "sha256-checksum", + Usage: "checksum in sha256 format", + EnvVars: []string{"PLUGIN_SHA256", "PLUGIN_SHA265"}, + Destination: &settings.SHA256, + }, + } +} diff --git a/cmd/drone-download/main.go b/cmd/drone-download/main.go new file mode 100644 index 0000000..45c35c0 --- /dev/null +++ b/cmd/drone-download/main.go @@ -0,0 +1,70 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +// DO NOT MODIFY THIS FILE DIRECTLY + +package main + +import ( + "os" + + "github.com/drone-plugins/drone-download/plugin" + "github.com/drone-plugins/drone-plugin-lib/errors" + "github.com/drone-plugins/drone-plugin-lib/urfave" + "github.com/joho/godotenv" + "github.com/urfave/cli/v2" +) + +var version = "unknown" + +func main() { + settings := &plugin.Settings{} + + if _, err := os.Stat("/run/drone/env"); err == nil { + godotenv.Overload("/run/drone/env") + } + + app := &cli.App{ + Name: "drone-download", + Usage: "download a file", + Version: version, + Flags: append(settingsFlags(settings), urfave.Flags()...), + Action: run(settings), + } + + if err := app.Run(os.Args); err != nil { + errors.HandleExit(err) + } +} + +func run(settings *plugin.Settings) cli.ActionFunc { + return func(ctx *cli.Context) error { + urfave.LoggingFromContext(ctx) + + plugin := plugin.New( + *settings, + urfave.PipelineFromContext(ctx), + urfave.NetworkFromContext(ctx), + ) + + if err := plugin.Validate(); err != nil { + if e, ok := err.(errors.ExitCoder); ok { + return e + } + + return errors.ExitMessagef("validation failed: %w", err) + } + + if err := plugin.Execute(); err != nil { + if e, ok := err.(errors.ExitCoder); ok { + return e + } + + return errors.ExitMessagef("execution failed: %w", err) + } + + return nil + } +} diff --git a/docker/Dockerfile.linux.amd64 b/docker/Dockerfile.linux.amd64 index ba2e79b..c9b0d8c 100644 --- a/docker/Dockerfile.linux.amd64 +++ b/docker/Dockerfile.linux.amd64 @@ -6,4 +6,4 @@ LABEL maintainer="Drone.IO Community " \ org.label-schema.schema-version="1.0" ADD release/linux/amd64/drone-download /bin/ -ENTRYPOINT ["/bin/drone-download"] +ENTRYPOINT [ "/bin/drone-download" ] diff --git a/docker/Dockerfile.linux.arm b/docker/Dockerfile.linux.arm index 8a7c9f9..cfd8da0 100644 --- a/docker/Dockerfile.linux.arm +++ b/docker/Dockerfile.linux.arm @@ -6,4 +6,4 @@ LABEL maintainer="Drone.IO Community " \ org.label-schema.schema-version="1.0" ADD release/linux/arm/drone-download /bin/ -ENTRYPOINT ["/bin/drone-download"] +ENTRYPOINT [ "/bin/drone-download" ] diff --git a/docker/Dockerfile.linux.arm64 b/docker/Dockerfile.linux.arm64 index 8020227..4a4b530 100644 --- a/docker/Dockerfile.linux.arm64 +++ b/docker/Dockerfile.linux.arm64 @@ -6,4 +6,4 @@ LABEL maintainer="Drone.IO Community " \ org.label-schema.schema-version="1.0" ADD release/linux/arm64/drone-download /bin/ -ENTRYPOINT ["/bin/drone-download"] +ENTRYPOINT [ "/bin/drone-download" ] diff --git a/docker/Dockerfile.windows.1809 b/docker/Dockerfile.windows.1809 index ce9e8d3..7816728 100644 --- a/docker/Dockerfile.windows.1809 +++ b/docker/Dockerfile.windows.1809 @@ -1,10 +1,10 @@ # escape=` -FROM plugins/base:windows-1809 +FROM plugins/base:windows-1809-amd64 LABEL maintainer="Drone.IO Community " ` org.label-schema.name="Drone Download" ` org.label-schema.vendor="Drone.IO Community" ` org.label-schema.schema-version="1.0" -ADD release\drone-download.exe c:\drone-download.exe -ENTRYPOINT [ "c:\\drone-download.exe" ] +ADD release/windows/amd64/drone-download.exe C:/bin/drone-download.exe +ENTRYPOINT [ "C:\\bin\\drone-download.exe" ] diff --git a/docker/Dockerfile.windows.1903 b/docker/Dockerfile.windows.1903 new file mode 100644 index 0000000..d4597e4 --- /dev/null +++ b/docker/Dockerfile.windows.1903 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-1903-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Download" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-download.exe C:/bin/drone-download.exe +ENTRYPOINT [ "C:\\bin\\drone-download.exe" ] diff --git a/docker/Dockerfile.windows.1909 b/docker/Dockerfile.windows.1909 new file mode 100644 index 0000000..0ecc76b --- /dev/null +++ b/docker/Dockerfile.windows.1909 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-1909-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Download" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-download.exe C:/bin/drone-download.exe +ENTRYPOINT [ "C:\\bin\\drone-download.exe" ] diff --git a/docker/Dockerfile.windows.2004 b/docker/Dockerfile.windows.2004 new file mode 100644 index 0000000..f9c9471 --- /dev/null +++ b/docker/Dockerfile.windows.2004 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-2004-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Download" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-download.exe C:/bin/drone-download.exe +ENTRYPOINT [ "C:\\bin\\drone-download.exe" ] diff --git a/docker/manifest.tmpl b/docker/manifest.tmpl index f9b388a..90fb0c7 100644 --- a/docker/manifest.tmpl +++ b/docker/manifest.tmpl @@ -1,36 +1,43 @@ image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} + {{#if build.tags}} tags: {{#each build.tags}} - {{this}} {{/each}} {{/if}} + manifests: - - - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 platform: architecture: amd64 os: linux - - - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 platform: architecture: arm64 os: linux variant: v8 - - - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm platform: architecture: arm os: linux variant: v7 - - - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1803 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-2004-amd64 platform: architecture: amd64 os: windows - version: 1803 - - - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1809 + version: 2004 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1909-amd64 + platform: + architecture: amd64 + os: windows + version: 1909 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1903-amd64 + platform: + architecture: amd64 + os: windows + version: 1903 + - image: plugins/download:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1809-amd64 platform: architecture: amd64 os: windows diff --git a/go.mod b/go.mod index 9bdedc2..a062ba3 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,11 @@ module github.com/drone-plugins/drone-download +go 1.14 + require ( - github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1 - github.com/pkg/errors v0.8.0 - github.com/urfave/cli v1.20.0 + github.com/drone-plugins/drone-plugin-lib v0.3.1 + github.com/joho/godotenv v1.3.0 + github.com/sirupsen/logrus v1.6.0 + github.com/urfave/cli/v2 v2.2.0 + golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect ) diff --git a/go.sum b/go.sum index ccc0d2c..78aee30 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,28 @@ -github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1 h1:9Xm8CKtMZIXgcopfdWk/qZ1rt0HjMgfMR9nxxSeK6vk= -github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1/go.mod h1:zuHl3Hh+e9P6gmBPvcqR1HjkaWHC/csgyskg6IaFKFo= -github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= -github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/drone-plugins/drone-plugin-lib v0.3.1 h1:Br43wRnot2CpDGKPIKOnIxkTsuII5q4xxKROirs5hxc= +github.com/drone-plugins/drone-plugin-lib v0.3.1/go.mod h1:ZUKtwSoUmeCj7DXMS3WktDdMAWudW7O6dYAZZRISv4M= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= +golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go deleted file mode 100644 index df779f8..0000000 --- a/main.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "log" - "os" - - "github.com/pkg/errors" - "github.com/urfave/cli" -) - -var ( - version = "unknown" -) - -func main() { - app := cli.NewApp() - app.Name = "download plugin" - app.Usage = "download plugin" - app.Action = run - app.Version = version - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "source", - Usage: "source url for the download", - EnvVar: "PLUGIN_SOURCE", - }, - cli.StringFlag{ - Name: "destination", - Usage: "destination for the download", - EnvVar: "PLUGIN_DESTINATION", - }, - cli.StringFlag{ - Name: "authorization", - Usage: "value to send in the authorization header", - EnvVar: "PLUGIN_AUTHORIZATION,DOWNLOAD_AUTHORIZATION", - }, - cli.StringFlag{ - Name: "username", - Usage: "username for basic auth", - EnvVar: "PLUGIN_USERNAME,DOWNLOAD_USERNAME", - }, - cli.StringFlag{ - Name: "password", - Usage: "password for basic auth", - EnvVar: "PLUGIN_PASSWORD,DOWNLOAD_PASSWORD", - }, - cli.BoolFlag{ - Name: "skip-verify", - Usage: "skip ssl verification", - EnvVar: "PLUGIN_SKIP_VERIFY", - }, - cli.StringFlag{ - Name: "md5-checksum", - Usage: "checksum in md5 format", - EnvVar: "PLUGIN_MD5", - }, - cli.StringFlag{ - Name: "sha256-checksum", - Usage: "checksum in sha256 format", - EnvVar: "PLUGIN_SHA256,PLUGIN_SHA265", - }, - } - - if err := app.Run(os.Args); err != nil { - log.Fatal(err) - } -} - -func run(c *cli.Context) error { - plugin := Plugin{ - Config: Config{ - Source: c.String("source"), - Destination: c.String("destination"), - Authorization: c.String("authorization"), - Username: c.String("username"), - Password: c.String("password"), - SkipVerify: c.Bool("skip-verify"), - MD5: c.String("md5-checksum"), - SHA256: c.String("sha256-checksum"), - }, - } - - if plugin.Config.Source == "" { - return errors.New("Missing source URL") - } - - return plugin.Exec() -} diff --git a/plugin.go b/plugin.go deleted file mode 100644 index 354ab7f..0000000 --- a/plugin.go +++ /dev/null @@ -1,160 +0,0 @@ -package main - -import ( - "crypto/md5" - "crypto/sha256" - "crypto/tls" - "fmt" - "io" - "log" - "net/http" - "net/url" - "os" - "path" - "path/filepath" - "time" - - "github.com/jackspirou/syscerts" - "github.com/pkg/errors" -) - -type ( - Config struct { - Source string - Destination string - Authorization string - Username string - Password string - SkipVerify bool - MD5 string - SHA256 string - } - - Plugin struct { - Config Config - } -) - -func (p Plugin) Exec() error { - destination := p.Config.Destination - - u, err := url.Parse(p.Config.Source) - if err != nil { - return errors.Wrap(err, "parsing source failed") - } - - switch { - case destination == "": - destination = path.Base(u.Path) - case destination[len(destination)-1] == filepath.Separator: - destination = destination + path.Base(u.Path) - fallthrough - default: - err = os.MkdirAll(filepath.Dir(destination), os.ModePerm) - if err != nil { - return errors.Wrap(err, "creating directory failed") - } - } - - log.Printf("downloading to %s", destination) - - client := &http.Client{ - Timeout: time.Minute * 5, - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - TLSClientConfig: &tls.Config{ - RootCAs: syscerts.SystemRootsPool(), - InsecureSkipVerify: p.Config.SkipVerify, - }, - }, - CheckRedirect: func(req *http.Request, via []*http.Request) error { - if p.Config.Username != "" && p.Config.Password != "" { - req.SetBasicAuth(p.Config.Username, p.Config.Password) - } - if p.Config.Authorization != "" { - req.Header.Add("Authorization", p.Config.Authorization) - } - return nil - }, - } - - req, err := http.NewRequest( - "GET", - p.Config.Source, - nil, - ) - - if err != nil { - return errors.Wrap(err, "initializing request failed") - } - - if p.Config.Username != "" && p.Config.Password != "" { - req.SetBasicAuth(p.Config.Username, p.Config.Password) - } - - if p.Config.Authorization != "" { - req.Header.Add("Authorization", p.Config.Authorization) - } - - resp, err := client.Do(req) - - if err != nil { - return errors.Wrap(err, "executing request failed") - } - - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return errors.Errorf("request failed, status %s", http.StatusText(resp.StatusCode)) - } - - target, err := os.Create(destination) - - if err != nil { - return errors.Wrap(err, "creating destination failed") - } - - defer target.Close() - - _, err = io.Copy(target, resp.Body) - - if err != nil { - return errors.Wrap(err, "copying destination failed") - } - - if p.Config.MD5 != "" { - h := md5.New() - target.Seek(0, 0) - - if _, err := io.Copy(h, target); err != nil { - defer os.Remove(target.Name()) - return errors.Wrap(err, "failed to compare checksum") - } - - check := fmt.Sprintf("%x", h.Sum(nil)) - - if p.Config.MD5 != check { - defer os.Remove(target.Name()) - return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.MD5) - } - } - - if p.Config.SHA256 != "" { - h := sha256.New() - target.Seek(0, 0) - - if _, err := io.Copy(h, target); err != nil { - defer os.Remove(target.Name()) - return errors.Wrap(err, "failed to compare checksum") - } - - check := fmt.Sprintf("%x", h.Sum(nil)) - - if p.Config.SHA256 != check { - defer os.Remove(target.Name()) - return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.SHA256) - } - } - - return nil -} diff --git a/plugin/impl.go b/plugin/impl.go new file mode 100644 index 0000000..b58b0ea --- /dev/null +++ b/plugin/impl.go @@ -0,0 +1,157 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "crypto/md5" + "crypto/sha256" + "fmt" + "hash" + "io" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "strings" + + "github.com/sirupsen/logrus" +) + +// Settings for the plugin. +type Settings struct { + Source string + Destination string + Authorization string + Username string + Password string + MD5 string + SHA256 string + + destination string +} + +// Validate handles the settings validation of the plugin. +func (p *Plugin) Validate() error { + // Verify the source url + source := p.settings.Source + if source == "" { + return fmt.Errorf("no source provided") + } + + u, err := url.Parse(source) + if err != nil { + return fmt.Errorf("could not parse url %s: %w", source, err) + } + + // Verify the destination + destination := filepath.ToSlash(p.settings.Destination) + if destination == "" { + destination = path.Base(u.Path) + } else if strings.HasSuffix(destination, "/") { + destination = path.Join(destination, path.Base(u.Path)) + } + + destination = filepath.FromSlash(path.Clean(destination)) + err = os.MkdirAll(filepath.Dir(destination), os.ModePerm) + if err != nil { + return fmt.Errorf("creating directory failed: %w", err) + } + p.settings.destination = destination + + return nil +} + +// Execute provides the implementation of the plugin. +func (p *Plugin) Execute() error { + req, err := http.NewRequestWithContext( + p.network.Context, + "GET", + p.settings.Source, + nil, + ) + + if err != nil { + return fmt.Errorf("initializing request failed: %w", err) + } + + p.addAuth(req) + + client := p.network.Client + client.CheckRedirect = func(req *http.Request, via []*http.Request) error { + p.addAuth(req) + return nil + } + + logrus.WithFields(logrus.Fields{ + "source": p.settings.Source, + "destination": p.settings.destination, + }).Info("Downloading file") + + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("executing request failed: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("request failed, status %s", http.StatusText(resp.StatusCode)) + } + + target, err := os.Create(p.settings.destination) + if err != nil { + return fmt.Errorf("creating destination failed: %w", err) + } + defer target.Close() + + _, err = io.Copy(target, resp.Body) + if err != nil { + return fmt.Errorf("copying destination failed: %w", err) + } + + var h hash.Hash + alg := "" + exp := "" + + if p.settings.SHA256 != "" { + exp = p.settings.SHA256 + alg = "SHA256" + h = sha256.New() + } else if p.settings.MD5 != "" { + exp = p.settings.MD5 + alg = "MD5" + h = md5.New() + } + + if exp != "" { + logrus.WithField("hash", alg).Info("Computing checksum") + target.Seek(0, 0) + + if _, err := io.Copy(h, target); err != nil { + defer os.Remove(target.Name()) + return fmt.Errorf("failed to compare checksum: %w", err) + } + + check := fmt.Sprintf("%x", h.Sum(nil)) + + if exp != check { + defer os.Remove(target.Name()) + return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, exp) + } + logrus.WithField("checksum", check).Info("Checksum matched") + } + + return nil +} + +func (p *Plugin) addAuth(req *http.Request) { + if p.settings.Username != "" && p.settings.Password != "" { + req.SetBasicAuth(p.settings.Username, p.settings.Password) + } + if p.settings.Authorization != "" { + req.Header.Add("Authorization", p.settings.Authorization) + } +} diff --git a/plugin/impl_test.go b/plugin/impl_test.go new file mode 100644 index 0000000..badfaa0 --- /dev/null +++ b/plugin/impl_test.go @@ -0,0 +1,18 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "testing" +) + +func TestValidate(t *testing.T) { + t.Skip() +} + +func TestExecute(t *testing.T) { + t.Skip() +} diff --git a/plugin/plugin.go b/plugin/plugin.go new file mode 100644 index 0000000..bb8248b --- /dev/null +++ b/plugin/plugin.go @@ -0,0 +1,26 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" +) + +// Plugin implements drone.Plugin to provide the plugin implementation. +type Plugin struct { + settings Settings + pipeline drone.Pipeline + network drone.Network +} + +// New initializes a plugin from the given Settings, Pipeline, and Network. +func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin { + return &Plugin{ + settings: settings, + pipeline: pipeline, + network: network, + } +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go new file mode 100644 index 0000000..3cb461f --- /dev/null +++ b/plugin/plugin_test.go @@ -0,0 +1,14 @@ +// Copyright (c) 2020, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "testing" +) + +func TestPlugin(t *testing.T) { + t.Skip() +}