diff --git a/.drone.star b/.drone.star index 1e978db..0b47320 100644 --- a/.drone.star +++ b/.drone.star @@ -1,16 +1,13 @@ -# drone-npm build - def main(ctx): - before = testing() + before = testing(ctx) stages = [ - linux('amd64'), - linux('arm64'), - linux('arm'), - + linux(ctx, 'amd64'), + linux(ctx, 'arm64'), + linux(ctx, 'arm'), ] - after = manifest() + gitter() + after = manifest(ctx) + gitter(ctx) for b in before: for s in stages: @@ -22,7 +19,7 @@ def main(ctx): return before + stages + after -def testing(): +def testing(ctx): return [{ 'kind': 'pipeline', 'type': 'docker', @@ -33,163 +30,165 @@ def testing(): }, 'steps': [ { - 'name': 'vet', + 'name': 'staticcheck', 'image': 'golang:1.13', 'pull': 'always', 'commands': [ - 'go version', - 'go vet ./...' + 'go run honnef.co/go/tools/cmd/staticcheck ./...', ], 'volumes': [ { 'name': 'gopath', - 'path': '/go' - } - ] + 'path': '/go', + }, + ], + }, + { + 'name': 'lint', + 'image': 'golang:1.13', + 'pull': 'always', + 'commands': [ + 'go run golang.org/x/lint/golint -set_exit_status ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], + }, + { + 'name': 'vet', + 'image': 'golang:1.13', + 'pull': 'always', + 'commands': [ + 'go vet ./...', + ], + 'volumes': [ + { + 'name': 'gopath', + 'path': '/go', + }, + ], }, { 'name': 'test', 'image': 'golang:1.13', 'pull': 'always', 'commands': [ - 'go version', - 'go test -cover ./...' + 'go test -cover ./...', ], 'volumes': [ { 'name': 'gopath', - 'path': '/go' - } - ] - } + 'path': '/go', + }, + ], + }, ], 'volumes': [ { 'name': 'gopath', - 'temp': {} - } + 'temp': {}, + }, ], 'trigger': { 'ref': [ 'refs/heads/master', 'refs/tags/**', - 'refs/pull/**' - ] - } + 'refs/pull/**', + ], + }, }] -def linux(arch): +def linux(ctx, arch): + docker = { + 'dockerfile': 'docker/Dockerfile.linux.%s' % (arch), + 'repo': 'plugins/npm', + '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-npm ./cmd/drone-npm' % (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-npm ./cmd/drone-npm' % (ctx.build.commit[0:8], arch), + ] + return { 'kind': 'pipeline', 'type': 'docker', - 'name': 'linux-%s' % arch, + 'name': 'linux-%s' % (arch), 'platform': { 'os': 'linux', 'arch': arch, }, 'steps': [ { - 'name': 'build-push', + 'name': 'environment', 'image': 'golang:1.13', 'pull': 'always', 'environment': { - 'CGO_ENABLED': '0' + 'CGO_ENABLED': '0', }, 'commands': [ 'go version', - 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/%s/drone-npm ./cmd/drone-npm' % arch, + 'go env', ], - 'when': { - 'event': { - 'exclude': [ - 'tag' - ] - } - } }, { - 'name': 'build-tag', + 'name': 'build', 'image': 'golang:1.13', 'pull': 'always', 'environment': { - 'CGO_ENABLED': '0' + 'CGO_ENABLED': '0', }, - 'commands': [ - 'go version', - 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/%s/drone-npm ./cmd/drone-npm' % arch, - ], - 'when': { - 'event': [ - 'tag' - ] - } + 'commands': build, }, { 'name': 'executable', 'image': 'golang:1.13', 'pull': 'always', 'commands': [ - './release/linux/%s/drone-npm --help' % arch - ] + './release/linux/%s/drone-npm --help' % (arch), + ], }, { - 'name': 'dryrun', + 'name': 'docker', 'image': 'plugins/docker', 'pull': 'always', - 'settings': { - 'dry_run': True, - 'tags': 'linux-%s' % arch, - 'dockerfile': 'docker/Dockerfile.linux.%s' % arch, - 'repo': 'plugins/npm', - 'username': { - 'from_secret': 'docker_username' - }, - 'password': { - 'from_secret': 'docker_password' - } - }, - 'when': { - 'event': [ - 'pull_request' - ] - } + 'settings': docker, }, - { - 'name': 'publish', - 'image': 'plugins/docker', - 'pull': 'always', - 'settings': { - 'auto_tag': True, - 'auto_tag_suffix': 'linux-%s' % arch, - 'dockerfile': 'docker/Dockerfile.linux.%s' % arch, - 'repo': 'plugins/npm', - 'username': { - 'from_secret': 'docker_username' - }, - 'password': { - 'from_secret': 'docker_password' - } - }, - 'when': { - 'event': { - 'exclude': [ - 'pull_request' - ] - } - } - } ], 'depends_on': [], 'trigger': { 'ref': [ 'refs/heads/master', 'refs/tags/**', - 'refs/pull/**' - ] - } + 'refs/pull/**', + ], + }, } -def manifest(): +def manifest(ctx): return [{ 'kind': 'pipeline', 'type': 'docker', @@ -202,10 +201,10 @@ def manifest(): 'settings': { 'auto_tag': 'true', 'username': { - 'from_secret': 'docker_username' + 'from_secret': 'docker_username', }, 'password': { - 'from_secret': 'docker_password' + 'from_secret': 'docker_password', }, 'spec': 'docker/manifest.tmpl', 'ignore_missing': 'true', @@ -217,27 +216,27 @@ def manifest(): 'pull': 'always', 'settings': { 'urls': { - 'from_secret': 'microbadger_url' - } + 'from_secret': 'microbadger_url', + }, }, - } + }, ], 'depends_on': [], 'trigger': { 'ref': [ 'refs/heads/master', - 'refs/tags/**' - ] - } + 'refs/tags/**', + ], + }, }] -def gitter(): +def gitter(ctx): return [{ 'kind': 'pipeline', 'type': 'docker', 'name': 'gitter', 'clone': { - 'disable': True + 'disable': True, }, 'steps': [ { @@ -246,22 +245,21 @@ def gitter(): 'pull': 'always', 'settings': { 'webhook': { - 'from_secret': 'gitter_webhook' + 'from_secret': 'gitter_webhook', } }, }, ], 'depends_on': [ - 'manifest' + 'manifest', ], 'trigger': { 'ref': [ 'refs/heads/master', 'refs/tags/**', - 'refs/pull/**' ], 'status': [ - 'failure' - ] - } + 'failure', + ], + }, }] diff --git a/.gitignore b/.gitignore index 47e6940..b2e5f13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,31 +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/ +/drone-npm* coverage.out - -# Executable -release/ -./drone-npm -./drone-npm.exe +.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/cmd/drone-npm/config.go b/cmd/drone-npm/config.go index fe9f2bf..9bcdfba 100644 --- a/cmd/drone-npm/config.go +++ b/cmd/drone-npm/config.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, the Drone Plugins project authors. +// 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. @@ -6,88 +6,66 @@ package main import ( + "github.com/drone-plugins/drone-npm/plugin" "github.com/urfave/cli/v2" - - "github.com/drone-plugins/drone-npm/pkg/npm" -) - -const ( - usernameFlag = "username" - passwordFlag = "password" - emailFlag = "email" - tokenFlag = "token" - registryFlag = "registry" - folderFlag = "folder" - failOnVersionConflictFlag = "fail-on-version-conflict" - tagFlag = "tag" - accessFlag = "access" ) // settingsFlags has the cli.Flags for the plugin.Settings. -func settingsFlags() []cli.Flag { - // Replace below with all the flags required for the plugin's specific - // settings. +func settingsFlags(settings *plugin.Settings) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ - Name: usernameFlag, - Usage: "NPM username", - EnvVars: []string{"PLUGIN_USERNAME", "NPM_USERNAME"}, + Name: "username", + Usage: "NPM username", + EnvVars: []string{"PLUGIN_USERNAME", "NPM_USERNAME"}, + Destination: &settings.Username, }, &cli.StringFlag{ - Name: passwordFlag, - Usage: "NPM password", - EnvVars: []string{"PLUGIN_PASSWORD", "NPM_PASSWORD"}, + Name: "password", + Usage: "NPM password", + EnvVars: []string{"PLUGIN_PASSWORD", "NPM_PASSWORD"}, + Destination: &settings.Password, }, &cli.StringFlag{ - Name: emailFlag, - Usage: "NPM email", - EnvVars: []string{"PLUGIN_EMAIL", "NPM_EMAIL"}, + Name: "email", + Usage: "NPM email", + EnvVars: []string{"PLUGIN_EMAIL", "NPM_EMAIL"}, + Destination: &settings.Email, }, &cli.StringFlag{ - Name: tokenFlag, - Usage: "NPM deploy token", - EnvVars: []string{"PLUGIN_TOKEN", "NPM_TOKEN"}, + Name: "token", + Usage: "NPM deploy token", + EnvVars: []string{"PLUGIN_TOKEN", "NPM_TOKEN"}, + Destination: &settings.Token, }, &cli.StringFlag{ - Name: registryFlag, - Usage: "NPM registry", - EnvVars: []string{"PLUGIN_REGISTRY", "NPM_REGISTRY"}, + Name: "registry", + Usage: "NPM registry", + EnvVars: []string{"PLUGIN_REGISTRY", "NPM_REGISTRY"}, + Destination: &settings.Registry, }, &cli.StringFlag{ - Name: folderFlag, - Usage: "folder containing package.json", - EnvVars: []string{"PLUGIN_FOLDER"}, + Name: "folder", + Usage: "folder containing package.json", + EnvVars: []string{"PLUGIN_FOLDER"}, + Destination: &settings.Folder, }, &cli.BoolFlag{ - Name: failOnVersionConflictFlag, - Usage: "fail NPM publish if version already exists in NPM registry", - EnvVars: []string{"PLUGIN_FAIL_ON_VERSION_CONFLICT"}, + Name: "fail-on-version-conflict", + Usage: "fail NPM publish if version already exists in NPM registry", + EnvVars: []string{"PLUGIN_FAIL_ON_VERSION_CONFLICT"}, + Destination: &settings.FailOnVersionConflict, }, &cli.StringFlag{ - Name: tagFlag, - Usage: "NPM publish tag", - EnvVars: []string{"PLUGIN_TAG"}, + Name: "tag", + Usage: "NPM publish tag", + EnvVars: []string{"PLUGIN_TAG"}, + Destination: &settings.Tag, }, &cli.StringFlag{ - Name: accessFlag, - Usage: "NPM scoped package access", - EnvVars: []string{"PLUGIN_ACCESS"}, + Name: "access", + Usage: "NPM scoped package access", + EnvVars: []string{"PLUGIN_ACCESS"}, + Destination: &settings.Access, }, } } - -// settingsFromContext creates a plugin.Settings from the cli.Context. -func settingsFromContext(ctx *cli.Context) npm.Settings { - // Replace below with the parsing of the - return npm.Settings{ - Username: ctx.String(usernameFlag), - Password: ctx.String(passwordFlag), - Token: ctx.String(tokenFlag), - Email: ctx.String(emailFlag), - Registry: ctx.String(registryFlag), - Folder: ctx.String(folderFlag), - FailOnVersionConflict: ctx.Bool(failOnVersionConflictFlag), - Tag: ctx.String(tagFlag), - Access: ctx.String(accessFlag), - } -} diff --git a/cmd/drone-npm/main.go b/cmd/drone-npm/main.go index 6b79ad7..3c49221 100644 --- a/cmd/drone-npm/main.go +++ b/cmd/drone-npm/main.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, the Drone Plugins project authors. +// 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. @@ -8,51 +8,57 @@ package main import ( - "fmt" "os" - "github.com/drone-plugins/drone-plugin-lib/pkg/urfave" - "github.com/sirupsen/logrus" + "github.com/drone-plugins/drone-npm/plugin" + "github.com/drone-plugins/drone-plugin-lib/errors" + "github.com/drone-plugins/drone-plugin-lib/urfave" "github.com/urfave/cli/v2" - - "github.com/drone-plugins/drone-npm/pkg/npm" ) -var ( - version = "unknown" -) +var version = "unknown" func main() { app := cli.NewApp() - app.Name = "npm plugin" + app.Name = "drone-npm" app.Usage = "pushes a package to a npm repository" - app.Action = run - app.Flags = append(settingsFlags(), urfave.Flags()...) + app.Version = version + + settings := plugin.Settings{} + app.Flags = append(settingsFlags(&settings), urfave.Flags()...) + app.Action = run(&settings) - // Run the application if err := app.Run(os.Args); err != nil { - logrus.Fatal(err) + errors.HandleExit(err) } } -func run(ctx *cli.Context) error { - urfave.LoggingFromContext(ctx) +func run(settings *plugin.Settings) cli.ActionFunc { + return func(ctx *cli.Context) error { + urfave.LoggingFromContext(ctx) - plugin := npm.New( - settingsFromContext(ctx), - urfave.PipelineFromContext(ctx), - urfave.NetworkFromContext(ctx), - ) + plugin := plugin.New( + *settings, + urfave.PipelineFromContext(ctx), + urfave.NetworkFromContext(ctx), + ) - // Validate the settings - if err := plugin.Validate(); err != nil { - return fmt.Errorf("Validation failed %w", err) + 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 } - - // Run the plugin - if err := plugin.Exec(); err != nil { - return fmt.Errorf("Execution failed %w", err) - } - - return nil } diff --git a/docker/Dockerfile.windows.1809 b/docker/Dockerfile.windows.1809 deleted file mode 100644 index 3df5be9..0000000 --- a/docker/Dockerfile.windows.1809 +++ /dev/null @@ -1,10 +0,0 @@ -# escape=` -FROM plugins/base:windows-1809-amd64 - -LABEL maintainer="Drone.IO Community " ` - org.label-schema.name="Drone NPM" ` - org.label-schema.vendor="Drone.IO Community" ` - org.label-schema.schema-version="1.0" - -ADD release/windows/amd64/drone-npm.exe C:/bin/drone-npm.exe -ENTRYPOINT [ "C:\\bin\\drone-npm.exe" ] diff --git a/docker/Dockerfile.windows.1903 b/docker/Dockerfile.windows.1903 deleted file mode 100644 index 01a83e9..0000000 --- a/docker/Dockerfile.windows.1903 +++ /dev/null @@ -1,10 +0,0 @@ -# escape=` -FROM plugins/base:windows-1903-amd64 - -LABEL maintainer="Drone.IO Community " ` - org.label-schema.name="Drone NPM" ` - org.label-schema.vendor="Drone.IO Community" ` - org.label-schema.schema-version="1.0" - -ADD release/windows/amd64/drone-npm.exe C:/bin/drone-npm.exe -ENTRYPOINT [ "C:\\bin\\drone-npm.exe" ] diff --git a/docker/Dockerfile.windows.1909 b/docker/Dockerfile.windows.1909 deleted file mode 100644 index 4559030..0000000 --- a/docker/Dockerfile.windows.1909 +++ /dev/null @@ -1,10 +0,0 @@ -# escape=` -FROM plugins/base:windows-1909-amd64 - -LABEL maintainer="Drone.IO Community " ` - org.label-schema.name="Drone NPM" ` - org.label-schema.vendor="Drone.IO Community" ` - org.label-schema.schema-version="1.0" - -ADD release/windows/amd64/drone-npm.exe C:/bin/drone-npm.exe -ENTRYPOINT [ "C:\\bin\\drone-npm.exe" ] diff --git a/go.mod b/go.mod index 8415e4f..a6ea387 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,7 @@ module github.com/drone-plugins/drone-npm go 1.13 require ( - github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect - github.com/drone-plugins/drone-plugin-lib v0.1.1 - github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect - github.com/sirupsen/logrus v1.4.2 - github.com/urfave/cli/v2 v2.0.0 - golang.org/x/sys v0.0.0-20191218084908-4a24b4065292 // indirect + github.com/drone-plugins/drone-plugin-lib v0.3.1 + github.com/sirupsen/logrus v1.6.0 + github.com/urfave/cli/v2 v2.2.0 ) diff --git a/go.sum b/go.sum index 75b8d24..83100ea 100644 --- a/go.sum +++ b/go.sum @@ -1,32 +1,25 @@ 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/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= -github.com/cpuguy83/go-md2man/v2 v2.0.0/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.1.1 h1:/sU3fdu1sgN/LVHHHiizjuLHZ0OR2KnzFo/ZzWefgsk= -github.com/drone-plugins/drone-plugin-lib v0.1.1/go.mod h1:w8Xb/uFXS+8j6bjlD+sVO0+5SQi1Pk0FaAbfNOsBFYo= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +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/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.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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.0.0 h1:+HU9SCbu8GnEUFtIBfuUNXN39ofWViIEJIp6SURMpCg= -github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +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 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191218084908-4a24b4065292 h1:Y8q0zsdcgAd+JU8VUA8p8Qv2YhuY9zevDG2ORt5qBUI= -golang.org/x/sys v0.0.0-20191218084908-4a24b4065292/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/pkg/npm/plugin.go b/pkg/npm/plugin.go deleted file mode 100644 index 9bf8b7c..0000000 --- a/pkg/npm/plugin.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2019, 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 npm - -import ( - "github.com/drone-plugins/drone-plugin-lib/pkg/plugin" - "github.com/drone-plugins/drone-plugin-lib/pkg/urfave" -) - -type pluginImpl struct { - settings Settings - pipeline plugin.Pipeline - network urfave.Network - npm *npmPackage -} - -// New Plugin from the given Settings, Pipeline, and Network. -func New(settings Settings, pipeline plugin.Pipeline, network urfave.Network) plugin.Plugin { - return &pluginImpl{ - settings: settings, - pipeline: pipeline, - network: network, - } -} diff --git a/pkg/npm/plugin_impl.go b/plugin/impl.go similarity index 94% rename from pkg/npm/plugin_impl.go rename to plugin/impl.go index 6343100..a0a8598 100644 --- a/pkg/npm/plugin_impl.go +++ b/plugin/impl.go @@ -1,9 +1,9 @@ -// Copyright (c) 2019, the Drone Plugins project authors. +// 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 npm +package plugin import ( "encoding/base64" @@ -32,6 +32,8 @@ type ( FailOnVersionConflict bool Tag string Access string + + npm *npmPackage } npmPackage struct { @@ -48,7 +50,8 @@ type ( // globalRegistry defines the default NPM registry. const globalRegistry = "https://registry.npmjs.org/" -func (p *pluginImpl) Validate() error { +// Validate handles the settings validation of the plugin. +func (p *Plugin) Validate() error { // Check authentication options if len(p.settings.Token) == 0 { if len(p.settings.Username) == 0 { @@ -84,12 +87,13 @@ func (p *pluginImpl) Validate() error { return fmt.Errorf("Registry values do not match .drone.yml: %s package.json: %s", p.settings.Registry, npm.Config.Registry) } - p.npm = npm + p.settings.npm = npm return nil } -func (p *pluginImpl) Exec() error { +// Execute provides the implementation of the plugin. +func (p *Plugin) Execute() error { // Write the npmrc file if err := p.writeNpmrc(); err != nil { return fmt.Errorf("Could not create npmrc %w", err) @@ -120,7 +124,7 @@ func (p *pluginImpl) Exec() error { } /// writeNpmrc creates a .npmrc in the folder for authentication -func (p *pluginImpl) writeNpmrc() error { +func (p *Plugin) writeNpmrc() error { var f func(settings Settings) string if len(p.settings.Token) == 0 { logrus.WithFields(logrus.Fields{ @@ -147,8 +151,8 @@ func (p *pluginImpl) writeNpmrc() error { } /// shouldPublishPackage determines if the package should be published -func (p *pluginImpl) shouldPublishPackage() (bool, error) { - cmd := packageVersionsCommand(p.npm.Name) +func (p *Plugin) shouldPublishPackage() (bool, error) { + cmd := packageVersionsCommand(p.settings.npm.Name) cmd.Dir = p.settings.Folder trace(cmd) @@ -177,7 +181,7 @@ func (p *pluginImpl) shouldPublishPackage() (bool, error) { for _, value := range versions { logrus.WithField("version", value).Debug("Found version of package") - if strings.Compare(p.npm.Version, value) == 0 { + if strings.Compare(p.settings.npm.Version, value) == 0 { logrus.Info("Version found in the registry") if p.settings.FailOnVersionConflict { return false, fmt.Errorf("Cannot publish package due to version conflict") @@ -195,7 +199,7 @@ func (p *pluginImpl) shouldPublishPackage() (bool, error) { } /// authenticate atempts to authenticate with the NPM registry. -func (p *pluginImpl) authenticate() error { +func (p *Plugin) authenticate() error { var cmds []*exec.Cmd // Write the version command 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/pkg/npm/plugin_test.go b/plugin/plugin_test.go similarity index 94% rename from pkg/npm/plugin_test.go rename to plugin/plugin_test.go index 30e1a22..74b1a62 100644 --- a/pkg/npm/plugin_test.go +++ b/plugin/plugin_test.go @@ -1,11 +1,13 @@ -// Copyright (c) 2019, the Drone Plugins project authors. +// 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 npm +package plugin -import "testing" +import ( + "testing" +) func TestTokenRCContents(t *testing.T) { settings := Settings{