diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..2368ee4 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,57 @@ +--- +kind: pipeline +type: docker +name: testing + +platform: + os: linux + arch: amd64 + +steps: +- name: staticcheck + pull: always + image: golang:1.13 + commands: + - go run honnef.co/go/tools/cmd/staticcheck ./... + volumes: + - name: gopath + path: /go + +- name: lint + pull: always + image: golang:1.13 + commands: + - go run golang.org/x/lint/golint -set_exit_status ./... + volumes: + - name: gopath + path: /go + +- name: vet + pull: always + image: golang:1.13 + commands: + - go vet ./... + volumes: + - name: gopath + path: /go + +- name: test + pull: always + image: golang:1.13 + commands: + - go test -cover -v ./... + volumes: + - name: gopath + path: /go + +volumes: +- name: gopath + temp: {} + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +... diff --git a/renovate.json b/.github/renovate.json similarity index 100% rename from renovate.json rename to .github/renovate.json diff --git a/.gitignore b/.gitignore index f1c181e..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +0,0 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, build with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out diff --git a/.mailmap b/.mailmap new file mode 100644 index 0000000..0ed3600 --- /dev/null +++ b/.mailmap @@ -0,0 +1 @@ +Don Olmstead Don diff --git a/AUTHORS b/AUTHORS index e235e2b..5bd971e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,8 +1,3 @@ -# This is the official list of the Drone Plugins project authors -# for copyright purposes. - -# Names should be added to this file as -# Name or Organization -# Email addresses for individuals are tracked elsewhere to avoid spam. - -Don Olmstead +Bo-Yi Wu +Don Olmstead +Thomas Boerger diff --git a/README.md b/README.md new file mode 100644 index 0000000..6695b94 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# drone-plugin-lib diff --git a/drone/build.go b/drone/build.go new file mode 100644 index 0000000..8620a19 --- /dev/null +++ b/drone/build.go @@ -0,0 +1,62 @@ +// 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 drone + +import ( + "time" +) + +// Build represents a build of a repository. +type Build struct { + // Branch defines the branch name of the build. + Branch string + + // PullRequest number of the build. + PullRequest int + + // Tag of the build. + Tag string + + // SourceBranch for the pull request. + SourceBranch string + + // TargetBranch for the pull request. + TargetBranch string + + // Number for the build. + Number int + + // Parent build number for the build. + Parent int + + // Event that triggered the build. + Event string + + // Action that triggered the build. This value is used to differentiate + // bettween a pull request being opened vs synchronized. + Action string + + // Status of the build. + Status string + + // Created time of the build. + Created time.Time + + // Started time of the build. + Started time.Time + + // Finished time of the build. + Finished time.Time + + // DeployTo the environment. + DeployTo string + + // FailedStages of the build. + FailedStages []string + + // FailedSteps of the build. + FailedSteps []string +} diff --git a/drone/commit.go b/drone/commit.go new file mode 100644 index 0000000..bcae89a --- /dev/null +++ b/drone/commit.go @@ -0,0 +1,43 @@ +// 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 drone + +// Commit represents the current commit being built. +type Commit struct { + // SHA for the current commit. + SHA string + + // Before contains the commit sha before the patch is applied. + Before string + + // After contains the commit sha after the patch is applied. + After string + + // Ref for the current commit. + Ref string + + // Branch target for the push or pull request. This may be empty for + // tag events. + Branch string + + // Link to the commit or object in the source control management system. + Link string + + // Message for the current commit. + Message string + + // Author of the commit. + Author string + + // AuthorName of the commit. + AuthorName string + + // AuthorEmail of the commit. + AuthorEmail string + + // AuthorAvatar of the commit. + AuthorAvatar string +} diff --git a/drone/network.go b/drone/network.go new file mode 100644 index 0000000..a60c89e --- /dev/null +++ b/drone/network.go @@ -0,0 +1,26 @@ +// 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 drone + +import ( + "context" + "net/http" +) + +// Network contains options for connecting to the network. +type Network struct { + // Context for making network requests. + // + // If `trace` logging is requested the context will use `httptrace` to + // capture all network requests. + Context context.Context + + /// Whether SSL verification is skipped + SkipVerify bool + + // Client for making network requests. + Client *http.Client +} diff --git a/drone/pipeline.go b/drone/pipeline.go new file mode 100644 index 0000000..f5f5c2f --- /dev/null +++ b/drone/pipeline.go @@ -0,0 +1,20 @@ +// 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 drone + +// Pipeline being executed. +// +// Represents the full Drone environment that the plugin is executing in. +type Pipeline struct { + Network Network + Build Build + Repo Repo + Commit Commit + Stage Stage + Step Step + SemVer SemVer + System System +} diff --git a/drone/plugin.go b/drone/plugin.go new file mode 100644 index 0000000..1147e65 --- /dev/null +++ b/drone/plugin.go @@ -0,0 +1,32 @@ +// 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 drone + +// Plugin is an interface for a Drone plugin written in Go. +// +// This is a higly opinionated interface for what a Plugin should do. Its +// not required that a plugin author follow it. +type Plugin interface { + // Validate checks the inputs to the Plugin and verifies that the + // configuration is correct before executing. + // + // An error is returned if there are any issues with the current + // configuration, such as missing information or files not being + // present. A Plugin may choose to populate additional information to + // ensure a successful execution, for example if a URL is parsed + // successfully it can be stored off for later use. + // + // Validate needs to be called before Execute. + Validate() error + + // Execute runs the plugin in the current configuration. + // + // An error is returned if the Plugin did not run successfully that + // describes the runtime error. + // + // Execute needs to be called after Validate. + Execute() error +} diff --git a/drone/repo.go b/drone/repo.go new file mode 100644 index 0000000..1bc4516 --- /dev/null +++ b/drone/repo.go @@ -0,0 +1,39 @@ +// 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 drone + +// Repo represents the repository for the build. +type Repo struct { + // Slug for the full name of a repo. + Slug string + + // SCM for the used SCM. + SCM string + + // Owner for the repo owner. + Owner string + + // Name for the repo name. + Name string + + // Link for the link to the repo. + Link string + + // Branch for the default branch of the repo. + Branch string + + // HTTPURL for the clone URL via HTTP. + HTTPURL string + + // SSHURL for the clone URL via SSH + SSHURL string + + // Visbility for the visbility of the repo. + Visibility string + + // Private to show if the repo is private. + Private bool +} diff --git a/drone/semver.go b/drone/semver.go new file mode 100644 index 0000000..1eb70a5 --- /dev/null +++ b/drone/semver.go @@ -0,0 +1,39 @@ +// 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 drone + +// SemVer represents the semantic version of the currently running build. +// +// This value is only applicable for tags. If the tag cannot be parsed into +// a semantic version then SemVer.Error will have the reason. +type SemVer struct { + // Version is the full semantic version. + Version string + + // Major version number. + Major string + + // Minor version number. + Minor string + + // Patch version number. + Patch string + + // Prerelease version. + Prerelease string + + // Build version number. + // + // This is signified by a + at the end of the tag. + Build string + + // Short version of the semantic version string where labels and + // metadata are truncated. + Short string + + // Error is the semantic version parsing error if the tag was invalid. + Error string +} diff --git a/drone/stage.go b/drone/stage.go new file mode 100644 index 0000000..eb78ec7 --- /dev/null +++ b/drone/stage.go @@ -0,0 +1,64 @@ +// 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 drone + +import ( + "time" +) + +// Stage represents a build stage. +type Stage struct { + // Kind is the kind of resource being executed. + // + // This value is sourced from the `kind` attribute in the yaml + // configuration file + Kind string + + // Type is the type of resource being executed. + Type string + + // Name is the name for the current running build stage. + Name string + + // Number is the stage number for the current running build stage. + Number int + + // Machine provides the name of the host machine on which the build + // stage is currently running. + Machine string + + // OS is the target operating system for the current build stage. + OS string + + // Arch is the platform architecture of the current build stage. + Arch string + + // Variant is the target architecture variant for the current build + // stage. + Variant string + + // Version is OS version for the current build stage. + Version string + + // Status is the status for the current running build stage. + // + // If all of the stage's steps are passing, the status defaults to + // success. + Status string + + // Started is the unix timestamp for when a build stage was started by + // the runner. + Started time.Time + + // Finished is the unix timestamp for when the pipeline is finished. + // + // A running pipleine cannot have a finish timestamp, therefore, the + // system aways sets this value to the current timestamp. + Finished time.Time + + // DependsOn is a list of dependencies for the current build stage. + DependsOn []string +} diff --git a/drone/step.go b/drone/step.go new file mode 100644 index 0000000..d944340 --- /dev/null +++ b/drone/step.go @@ -0,0 +1,15 @@ +// 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 drone + +// Step represents the currently running step within the stage. +type Step struct { + // Name for the name of the current step. + Name string + + // Number is the numeric value of the step. + Number int +} diff --git a/drone/system.go b/drone/system.go new file mode 100644 index 0000000..102a05a --- /dev/null +++ b/drone/system.go @@ -0,0 +1,18 @@ +// 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 drone + +// System represents the available system variables. +type System struct { + // Proto for the system protocol. + Proto string + + // Host for the system host name. + Host string + + // Version for the system version. + Version string +} diff --git a/go.sum b/go.sum index 8b7fd44..99a608c 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,8 @@ 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/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/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= @@ -14,7 +11,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV 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/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= diff --git a/internal/environ/environ.go b/internal/environ/environ.go deleted file mode 100644 index ba845b6..0000000 --- a/internal/environ/environ.go +++ /dev/null @@ -1,168 +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 environ - -// List of enviornment variables set by Drone when running a step within a -// build stage. -// -// Multiple values are specified with `,` within the string. If there are -// multiple values this is to support backward compatibility with versions of -// Drone prior to the 1.0 release. - -const ( - // The following environment variables are being ignored currently - // - // * DRONE_COMMIT - Redundant with DRONE_COMMIT_SHA - // * DRONE_GIT_HTTP_URL - // * DRONE_GIT_SSH_URL - // * DRONE_REPO_NAMESPACE - Redundant to DRONE_REPO_OWNER - - //--------------------------------------------------------------------- - // Build Enviornment Variables - //--------------------------------------------------------------------- - - // BuildActionEnvVar corresponds to Build.Action. - BuildActionEnvVar = "DRONE_BUILD_ACTION" - // BuildCreatedEnvVar corresponds to Build.Created. - BuildCreatedEnvVar = "DRONE_BUILD_CREATED" - // BuildDeployToEnvVar corresponds to Build.DeployTo. - BuildDeployToEnvVar = "DRONE_DEPLOY_TO" - // BuildEventEnvVar corresponds to Build.Event. - BuildEventEnvVar = "DRONE_BUILD_EVENT" - // BuildFailedStagesEnvVar corresponds to Build.FailedStages. - BuildFailedStagesEnvVar = "DRONE_FAILED_STAGES" - // BuildFailedStepsEnvVar corresponds to Build.FailedSteps. - BuildFailedStepsEnvVar = "DRONE_FAILED_STEPS" - // BuildFinishedEnvVar corresponds to Build.Finished. - BuildFinishedEnvVar = "DRONE_BUILD_FINISHED" - // BuildNumberEnvVar corresponds to Build.Created. - BuildNumberEnvVar = "DRONE_BUILD_NUMBER" - // BuildParentEnvVar corresponds to Build.Parent. - BuildParentEnvVar = "DRONE_BUILD_PARENT" - // BuildPullRequestEnvVar corresponds to Build.PullRequest. - BuildPullRequestEnvVar = "DRONE_PULL_REQUEST" - // BuildStartedEnvVar corresponds to Build.Started. - BuildStartedEnvVar = "DRONE_BUILD_STARTED" - // BuildStatusEnvVar corresponds to Build.Status. - BuildStatusEnvVar = "DRONE_BUILD_STATUS" - // BuildSourceBranchEnvVar corresponds to Build.SourceBranch. - BuildSourceBranchEnvVar = "DRONE_SOURCE_BRANCH" - // BuildTagEnvVar corresponds to Build.Tag. - BuildTagEnvVar = "DRONE_TAG" - // BuildTargetBranchEnvVar corresponds to Build.TargetBranch. - BuildTargetBranchEnvVar = "DRONE_TARGET_BRANCH" - - //--------------------------------------------------------------------- - // Repo Enviornment Variables - //--------------------------------------------------------------------- - - // RepoDefaultBranchEnvVar corresponds to Repo.DefaultBranch. - RepoDefaultBranchEnvVar = "DRONE_REPO_BRANCH" - // RepoFullNameEnvVar corresponds to Repo.FullName. - RepoFullNameEnvVar = "DRONE_REPO" - // RepoLinkEnvVar corresponds to Repo.Link. - RepoLinkEnvVar = "DRONE_REPO_LINK" - // RepoNameEnvVar corresponds to Repo.Name - RepoNameEnvVar = "DRONE_REPO_NAME" - // RepoOwnerEnvVar corresponds to Repo.Owner. - RepoOwnerEnvVar = "DRONE_REPO_OWNER" - // RepoPrivateEnvVar corresponds to Repo.Private. - RepoPrivateEnvVar = "DRONE_REPO_PRIVATE" - // RepoRemoteURLEnvVar corresponds to Repo.RemoteURL. - RepoRemoteURLEnvVar = "DRONE_REMOTE_URL" - // RepoSCMEnvVar corresponds to Repo.SCM. - RepoSCMEnvVar = "DRONE_REPO_SCM" - // RepoVisibilityEnvVar corresponds to Repo.Visbility. - RepoVisibilityEnvVar = "DRONE_REPO_VISIBILITY" - - //--------------------------------------------------------------------- - // Commit Enviornment Variables - //--------------------------------------------------------------------- - - // CommitAfterEnvVar corresponds to Commit.After. - CommitAfterEnvVar = "DRONE_COMMIT_AFTER" - // CommitAuthorEnvVar corresponds to Commit.Author. - CommitAuthorEnvVar = "DRONE_COMMIT_AUTHOR" - // CommitAuthorAvatarEnvVar corresponds to Commit.AuthorAvatar. - CommitAuthorAvatarEnvVar = "DRONE_COMMIT_AUTHOR_AVATAR" - // CommitAuthorEmailEnvVar corresponds to Commit.AuthorEmail. - CommitAuthorEmailEnvVar = "DRONE_COMMIT_AUTHOR_EMAIL" - // CommitAuthorNameEnvVar corresponds to Commit.AuthorName. - CommitAuthorNameEnvVar = "DRONE_COMMIT_AUTHOR_NAME" - // CommitBeforeEnvVar corresponds to Commit.Before. - CommitBeforeEnvVar = "DRONE_COMMIT_BEFORE" - // CommitBranchEnvVar corresponds to Commit.Branch. - CommitBranchEnvVar = "DRONE_COMMIT_BRANCH" - // CommitLinkEnvVar corresponds to Commit.Link. - CommitLinkEnvVar = "DRONE_COMMIT_LINK" - // CommitMessageEnvVar corresponds to Commit.Message. - CommitMessageEnvVar = "DRONE_COMMIT_MESSAGE" - // CommitRefEnvVar corresponds to Commit.Ref. - CommitRefEnvVar = "DRONE_COMMIT_REF" - // CommitSHAEnvVar corresponds to Commit.SHA. - CommitSHAEnvVar = "DRONE_COMMIT_SHA" - - //--------------------------------------------------------------------- - // Stage Enviornment Variables - //--------------------------------------------------------------------- - - // StageArchEnvVar corresponds to Stage.Arch. - StageArchEnvVar = "DRONE_STAGE_ARCH" - // StageDependsOnEnvVar corresponds to Stage.DependsOn. - StageDependsOnEnvVar = "DRONE_STAGE_DEPENDS_ON" - // StageFinishedEnvVar corresponds to Stage.Finished. - StageFinishedEnvVar = "DRONE_STAGE_FINISHED" - // StageKindEnvVar corresponds Stage.Kind. - StageKindEnvVar = "DRONE_STAGE_KIND" - // StageMachineEnvVar corresponds to Stage.Machine. - StageMachineEnvVar = "DRONE_STAGE_MACHINE" - // StageNameEnvVar corresponds to Stage.Name. - StageNameEnvVar = "DRONE_STAGE_NAME" - // StageNumberEnvVar corresponds to Stage.Number. - StageNumberEnvVar = "DRONE_STAGE_NUMBER" - // StageOSEnvVar corresponds to Stage.OS. - StageOSEnvVar = "DRONE_STAGE_OS" - // StageStartedEnvVar corresponds to Stage.Started. - StageStartedEnvVar = "DRONE_STAGE_STARTED" - // StageStatusEnvVar corresponds to Stage.Status. - StageStatusEnvVar = "DRONE_STAGE_STATUS" - // StageTypeEnvVar corresponds to Stage.Type. - StageTypeEnvVar = "DRONE_STAGE_TYPE" - // StageVariantEnvVar corresponds to Stage.Variant. - StageVariantEnvVar = "DRONE_STAGE_VARIANT" - // StageVersionEnvVar corresponds to Stage.Version. - StageVersionEnvVar = "DRONE_STAGE_VERSION" - - //--------------------------------------------------------------------- - // Step Environment Variables - //--------------------------------------------------------------------- - - // StepNameEnvVar corresponds to Step.Name. - StepNameEnvVar = "DRONE_STEP_NAME" - // StepNumberEnvVar corresponds to Step.Number. - StepNumberEnvVar = "DRONE_STEP_NUMBER" - - //--------------------------------------------------------------------- - // SemVer Variables - //--------------------------------------------------------------------- - - // SemVerBuildEnvVar corresponds to SemVer.Build. - SemVerBuildEnvVar = "DRONE_SEMVER_BUILD" - // SemVerErrorEnvVar corresponds to SemVer.Error. - SemVerErrorEnvVar = "DRONE_SEMVER_ERROR" - // SemVerMajorEnvVar corresponds to SemVer.Major. - SemVerMajorEnvVar = "DRONE_SEMVER_MAJOR" - // SemVerMinorEnvVar corresponds to SemVer.Minor. - SemVerMinorEnvVar = "DRONE_SEMVER_MINOR" - // SemVerPatchEnvVar corresponds to SemVer.Patch. - SemVerPatchEnvVar = "DRONE_SEMVER_PATCH" - // SemVerPrereleaseEnvVar corresponds to SemVer.Prerelease - SemVerPrereleaseEnvVar = "DRONE_SEMVER_PRERELEASE" - // SemVerShortEnvVar corresponds to SemVer.Short. - SemVerShortEnvVar = "DRONE_SEMVER_SHORT" - // SemVerVersionEnvVar corresponds to SemVer.Version - SemVerVersionEnvVar = "DRONE_SEMVER" -) diff --git a/pkg/plugin/interfaces.go b/pkg/plugin/interfaces.go deleted file mode 100644 index a98c5ca..0000000 --- a/pkg/plugin/interfaces.go +++ /dev/null @@ -1,34 +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 plugin - -type ( - // Plugin is an interface for a Drone plugin written in Go. - // - // This is a higly opinionated interface for what a Plugin should do. Its - // not required that a plugin author follow it. - Plugin interface { - // Validate checks the inputs to the Plugin and verifies that the - // configuration is correct before executing. - // - // An error is returned if there are any issues with the current - // configuration, such as missing information or files not being - // present. A Plugin may choose to populate additional information to - // ensure a successful execution, for example if a URL is parsed - // successfully it can be stored off for later use. - // - // Validate needs to be called before Exec. - Validate() error - - // Exec runs the plugin in the current configuration. - // - // An error is returned if the Plugin did not run successfully that - // describes the runtime error. - // - // Exec needs to be called after Validate. - Exec() error - } -) diff --git a/pkg/plugin/types.go b/pkg/plugin/types.go deleted file mode 100644 index daafe24..0000000 --- a/pkg/plugin/types.go +++ /dev/null @@ -1,171 +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 plugin - -import "time" - -type ( - // Pipeline being executed. - // - // Represents the full Drone environment that the plugin is executing in. - Pipeline struct { - Build Build - Repo Repo - Commit Commit - Stage Stage - Step Step - SemVer SemVer - } - - // Build represents a build of a repository. - Build struct { - // Action that triggered the build. This value is used to differentiate - // bettween a pull request being opened vs synchronized. - Action string - // Created time of the build. - Created time.Time - // DeployTo the environment. - DeployTo string - // Event that triggered the build. - Event string - // FailedStages of the build. - FailedStages []string - // FailedSteps of the build. - FailedSteps []string - // Finished time of the build. - Finished time.Time - // Number for the build. - Number int - // Parent build number for the build. - Parent int - // PullRequest number of the build. - PullRequest int - // Started time of the build. - Started time.Time - // Status of the build. - Status string - // SourceBranch for the pull request. - SourceBranch string - // Tag of the build. - Tag string - // TargetBranch for the pull request. - TargetBranch string - } - - // Repo represents the repository for the build. - Repo struct { - DefaultBranch string - FullName string - Link string - Name string - Owner string - Private bool - RemoteURL string - SCM string - Visibility string - } - - // Commit represents the current commit being built. - Commit struct { - // After contains the commit sha after the patch is applied. - After string - // Author of the commit. - Author string - // AuthorAvatar of the commit. - AuthorAvatar string - // AuthorEmail of the commit. - AuthorEmail string - // AuthorName of the commit. - AuthorName string - // Before contains the commit sha before the patch is applied. - Before string - // Branch target for the push or pull request. This may be empty for - // tag events. - Branch string - // Link to the commit or object in the source control management system. - Link string - // Message for the current commit. - Message string - // Ref for the current commit. - Ref string - // SHA for the current commit. - SHA string - } - - // Stage represents a build stage. - Stage struct { - // Arch is the platform architecture of the current build stage. - Arch string - // DependsOn is a list of dependencies for the current build stage. - DependsOn []string - // Finished is the unix timestamp for when the pipeline is finished. - // - // A running pipleine cannot have a finish timestamp, therefore, the - // system aways sets this value to the current timestamp. - Finished time.Time - // Kind is the kind of resource being executed. - // - // This value is sourced from the `kind` attribute in the yaml - // configuration file - Kind string - // Machine provides the name of the host machine on which the build - // stage is currently running. - Machine string - // Name is the name for the current running build stage. - Name string - // Number is the stage number for the current running build stage. - Number int - // OS is the target operating system for the current build stage. - OS string - // Started is the unix timestamp for when a build stage was started by - // the runner. - Started time.Time - // Status is the status for the current running build stage. - // - // If all of the stage's steps are passing, the status defaults to - // success. - Status string - // Type is the type of resource being executed. - Type string - // Variant is the target architecture variant for the current build - // stage. - Variant string - // Version is OS version for the current build stage. - Version string - } - - // Step represents the currently running step within the stage. - Step struct { - Name string - Number int - } - - // SemVer represents the semantic version of the currently running build. - // - // This value is only applicable for tags. If the tag cannot be parsed into - // a semantic version then SemVer.Error will have the reason. - SemVer struct { - // Build version number. - // - // This is signified by a + at the end of the tag. - Build string - // Error is the semantic version parsing error if the tag was invalid. - Error string - // Major version number. - Major string - // Minor version number. - Minor string - // Patch version number. - Patch string - // Prerelease version. - Prerelease string - // Short version of the semantic version string where labels and - // metadata are truncated. - Short string - // Version is the full semantic version. - Version string - } -) diff --git a/pkg/urfave/network.go b/pkg/urfave/network.go deleted file mode 100644 index 3fe5fd0..0000000 --- a/pkg/urfave/network.go +++ /dev/null @@ -1,89 +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 urfave - -import ( - "context" - "crypto/tls" - "net" - "net/http" - "time" - - "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" -) - -//--------------------------------------------------------------------- -// Transport Flags -//--------------------------------------------------------------------- - -// Network contains options for connecting to the network. -type Network struct { - // Context for making network requests. - // - // If `trace` logging is requested the context will use `httptrace` to - // capture all network requests. - Context context.Context - - // Client for making network requests. - Client *http.Client - - /// Whether SSL verification is skipped - SkipVerify bool -} - -const networkSkipVerifyFlag = "transport.skip-verify" - -// networkFlags has the cli.Flags for the Transport. -func networkFlags() []cli.Flag { - return []cli.Flag{ - &cli.BoolFlag{ - Name: networkSkipVerifyFlag, - Usage: "skip ssl verify", - EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, - }, - } -} - -// NetworkFromContext creates a Transport from the cli.Context. -func NetworkFromContext(ctx *cli.Context) Network { - // Create the client - transport := &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - DualStack: true, - }).DialContext, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - } - - skipVerify := ctx.Bool(networkSkipVerifyFlag) - if skipVerify { - logrus.Warning("ssl verification is turned off") - transport.TLSClientConfig = &tls.Config{ - InsecureSkipVerify: true, - } - } - - // Create the context - context := context.Background() - - if ctx.String(logLevelFlag) == logrus.TraceLevel.String() { - context = traceHTTP(context) - } - - return Network{ - Client: &http.Client{ - Transport: transport, - }, - Context: context, - SkipVerify: skipVerify, - } -} diff --git a/pkg/urfave/urfave.go b/pkg/urfave/urfave.go deleted file mode 100644 index 84a8350..0000000 --- a/pkg/urfave/urfave.go +++ /dev/null @@ -1,611 +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 urfave provides helpers for interacting with the `urfave/cli` -// package when creating plugins for use by the Drone CI/CD service. -// -// Drone communicates to plugins by passing in environment variables that have -// information on the currently executing build. The `urfave/cli` package can -// read these environment variables and extract them into structs. -// -// import( -// "github.com/drone-plugins/drone-plugin-lib/pkg/urfave" -// "github.com/urfave/cli" -// ) -// -// func main() { -// app := cli.New() -// app.Name = "my awesome Drone plugin" -// app.Run = run -// app.Flags = []cli.Flags{ -// // All my plugin flags -// } -// app.Flags = append( -// app.Flags, -// urfave.Flags()..., -// ) -// } -// -// func run(ctx *cli.Context) { -// pipeline := urfave.PipelineFromContext(ctx) -// .... -// } -package urfave - -import ( - "time" - - "github.com/urfave/cli/v2" - - "github.com/drone-plugins/drone-plugin-lib/internal/environ" - "github.com/drone-plugins/drone-plugin-lib/pkg/plugin" -) - -//--------------------------------------------------------------------- -// Flags -//--------------------------------------------------------------------- - -// Flags for a urfave cli Drone plugin -func Flags() []cli.Flag { - flags := []cli.Flag{} - - flags = append(flags, pipelineFlags()...) - flags = append(flags, networkFlags()...) - flags = append(flags, loggingFlags()...) - - return flags -} - -//--------------------------------------------------------------------- -// Pipeline flags -//--------------------------------------------------------------------- - -// pipelineFlags has the cli.Flags for the plugin.Pipeline. -func pipelineFlags() []cli.Flag { - flags := []cli.Flag{} - - flags = append(flags, buildFlags()...) - flags = append(flags, repoFlags()...) - flags = append(flags, commitFlags()...) - flags = append(flags, stageFlags()...) - flags = append(flags, stepFlags()...) - flags = append(flags, semVerFlags()...) - - return flags -} - -// PipelineFromContext creates a plugin.Pipeline from the cli.Context. -func PipelineFromContext(ctx *cli.Context) plugin.Pipeline { - return plugin.Pipeline{ - Build: buildFromContext(ctx), - Repo: repoFromContext(ctx), - Commit: commitFromContext(ctx), - Stage: stageFromContext(ctx), - Step: stepFromContext(ctx), - SemVer: semVerFromContext(ctx), - } -} - -//--------------------------------------------------------------------- -// Build Flags -//--------------------------------------------------------------------- - -const ( - buildActionFlag = "build.action" - buildCreatedFlag = "build.created" - buildDeployToFlag = "build.deploy-to" - buildEventFlag = "build.event" - buildFailedStagesFlag = "build.failed-stages" - buildFailedStepsFlag = "build.failed-steps" - buildFinishedFlag = "build.finished" - buildNumberFlag = "build.number" - buildParentFlag = "build.parent" - buildPullRequestFlag = "build.pull-request" - buildSourceBranchFlag = "build.source-branch" - buildStartedFlag = "build.started" - buildStatusFlag = "build.status" - buildTagFlag = "build.tag" - buildTargetBranchFlag = "build.target-branch" -) - -// buildFlags has the cli.Flags for the plugin.Build. -func buildFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: buildActionFlag, - Usage: "build action", - EnvVars: []string{environ.BuildActionEnvVar}, - }, - &cli.StringFlag{ - Name: buildCreatedFlag, - Usage: "build created", - EnvVars: []string{environ.BuildCreatedEnvVar}, - }, - &cli.StringFlag{ - Name: buildDeployToFlag, - Usage: "build deploy to", - EnvVars: []string{environ.BuildDeployToEnvVar}, - }, - &cli.StringFlag{ - Name: buildEventFlag, - Usage: "build event", - EnvVars: []string{environ.BuildEventEnvVar}, - }, - &cli.StringSliceFlag{ - Name: buildFailedStagesFlag, - Usage: "build failed stages", - EnvVars: []string{environ.BuildFailedStagesEnvVar}, - }, - &cli.StringSliceFlag{ - Name: buildFailedStepsFlag, - Usage: "build failed steps", - EnvVars: []string{environ.BuildFailedStepsEnvVar}, - }, - &cli.StringFlag{ - Name: buildFinishedFlag, - Usage: "build finished", - EnvVars: []string{environ.BuildFinishedEnvVar}, - }, - &cli.IntFlag{ - Name: buildNumberFlag, - Usage: "build number", - EnvVars: []string{environ.BuildNumberEnvVar}, - }, - &cli.IntFlag{ - Name: buildParentFlag, - Usage: "build parent", - EnvVars: []string{environ.BuildParentEnvVar}, - }, - &cli.IntFlag{ - Name: buildPullRequestFlag, - Usage: "build pull request", - EnvVars: []string{environ.BuildPullRequestEnvVar}, - }, - &cli.StringFlag{ - Name: buildSourceBranchFlag, - Usage: "build source branch", - EnvVars: []string{environ.BuildSourceBranchEnvVar}, - }, - &cli.StringFlag{ - Name: buildStartedFlag, - Usage: "build started", - EnvVars: []string{environ.BuildStartedEnvVar}, - }, - &cli.StringFlag{ - Name: buildStatusFlag, - Usage: "build status", - EnvVars: []string{environ.BuildStatusEnvVar}, - }, - &cli.StringFlag{ - Name: buildTagFlag, - Usage: "build tag", - EnvVars: []string{environ.BuildTagEnvVar}, - }, - &cli.StringFlag{ - Name: buildTargetBranchFlag, - Usage: "build target branch", - EnvVars: []string{environ.BuildTargetBranchEnvVar}, - }, - } -} - -// buildFromContext creates a plugin.Build from the cli.Context. -func buildFromContext(ctx *cli.Context) plugin.Build { - return plugin.Build{ - Action: ctx.String(buildActionFlag), - Created: time.Unix(ctx.Int64(buildCreatedFlag), 0), - DeployTo: ctx.String(buildDeployToFlag), - Event: ctx.String(buildEventFlag), - FailedStages: ctx.StringSlice(buildFailedStagesFlag), - FailedSteps: ctx.StringSlice(buildFailedStepsFlag), - Finished: time.Unix(ctx.Int64(buildFinishedFlag), 0), - Number: ctx.Int(buildNumberFlag), - Parent: ctx.Int(buildParentFlag), - PullRequest: ctx.Int(buildPullRequestFlag), - SourceBranch: ctx.String(buildSourceBranchFlag), - Started: time.Unix(ctx.Int64(buildStartedFlag), 0), - Status: ctx.String(buildStatusFlag), - Tag: ctx.String(buildTagFlag), - TargetBranch: ctx.String(buildTargetBranchFlag), - } -} - -//--------------------------------------------------------------------- -// Repo Flags -//--------------------------------------------------------------------- - -const ( - repoDefaultBranchFlag = "repo.branch" - repoFullNameFlag = "repo.full-name" - repoLinkFlag = "repo.link" - repoNameFlag = "repo.name" - repoOwnerFlag = "repo.owner" - repoPrivateFlag = "repo.private" - repoRemoteURLFlag = "repo.remote-url" - repoSCMFlag = "repo.scm" - repoVisibilityFlag = "repo.visibility" -) - -// repoFlags has the cli.Flags for the plugin.Repo -func repoFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: repoDefaultBranchFlag, - Usage: "repo default branch", - EnvVars: []string{environ.RepoDefaultBranchEnvVar}, - }, - &cli.StringFlag{ - Name: repoFullNameFlag, - Usage: "repo full name", - EnvVars: []string{environ.RepoFullNameEnvVar}, - }, - &cli.StringFlag{ - Name: repoLinkFlag, - Usage: "repo link", - EnvVars: []string{environ.RepoLinkEnvVar}, - }, - &cli.StringFlag{ - Name: repoNameFlag, - Usage: "repo name", - EnvVars: []string{environ.RepoNameEnvVar}, - }, - &cli.StringFlag{ - Name: repoOwnerFlag, - Usage: "repo owner", - EnvVars: []string{environ.RepoOwnerEnvVar}, - }, - &cli.BoolFlag{ - Name: repoPrivateFlag, - Usage: "repo private", - EnvVars: []string{environ.RepoPrivateEnvVar}, - }, - &cli.StringFlag{ - Name: repoRemoteURLFlag, - Usage: "repo remote url", - EnvVars: []string{environ.RepoRemoteURLEnvVar}, - }, - &cli.StringFlag{ - Name: repoSCMFlag, - Usage: "repo scm", - EnvVars: []string{environ.RepoSCMEnvVar}, - }, - &cli.StringFlag{ - Name: repoVisibilityFlag, - Usage: "repo visibility", - EnvVars: []string{environ.RepoVisibilityEnvVar}, - }, - } -} - -// repoFromContext creates a plugin.Repo from the cli.Context. -func repoFromContext(ctx *cli.Context) plugin.Repo { - return plugin.Repo{ - DefaultBranch: ctx.String(repoDefaultBranchFlag), - FullName: ctx.String(repoFullNameFlag), - Link: ctx.String(repoLinkFlag), - Name: ctx.String(repoNameFlag), - Owner: ctx.String(repoOwnerFlag), - Private: ctx.Bool(repoPrivateFlag), - RemoteURL: ctx.String(repoRemoteURLFlag), - SCM: ctx.String(repoSCMFlag), - Visibility: ctx.String(repoVisibilityFlag), - } -} - -//--------------------------------------------------------------------- -// Commit Flags -//--------------------------------------------------------------------- - -const ( - commitAfterFlag = "commit.after" - commitAuthorFlag = "commit.author" - commitAuthorAvatarFlag = "commit.author-avatar" - commitAuthorEmailFlag = "commit.author-email" - commitAuthorNameFlag = "commit.author-name" - commitBeforeFlag = "commit.before" - commitBranchFlag = "commit.branch" - commitLinkFlag = "commit.link" - commitMessageFlag = "commit.message" - commitRefFlag = "commit.ref" - commitSHAFlag = "commit.sha" -) - -// commitFlags has the cli.Flags for the plugin.Commit. -func commitFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: commitAfterFlag, - Usage: "commit after", - EnvVars: []string{environ.CommitAfterEnvVar}, - }, - &cli.StringFlag{ - Name: commitAuthorFlag, - Usage: "commit author", - EnvVars: []string{environ.CommitAuthorEnvVar}, - }, - &cli.StringFlag{ - Name: commitAuthorAvatarFlag, - Usage: "commit author avatar", - EnvVars: []string{environ.CommitAuthorAvatarEnvVar}, - }, - &cli.StringFlag{ - Name: commitAuthorEmailFlag, - Usage: "commit author email", - EnvVars: []string{environ.CommitAuthorEmailEnvVar}, - }, - &cli.StringFlag{ - Name: commitAuthorNameFlag, - Usage: "commit author name", - EnvVars: []string{environ.CommitAuthorNameEnvVar}, - }, - &cli.StringFlag{ - Name: commitBeforeFlag, - Usage: "commit before", - EnvVars: []string{environ.CommitBeforeEnvVar}, - }, - &cli.StringFlag{ - Name: commitBranchFlag, - Usage: "commit branch", - EnvVars: []string{environ.CommitBranchEnvVar}, - }, - &cli.StringFlag{ - Name: commitLinkFlag, - Usage: "commit link", - EnvVars: []string{environ.CommitLinkEnvVar}, - }, - &cli.StringFlag{ - Name: commitMessageFlag, - Usage: "commit message", - EnvVars: []string{environ.CommitMessageEnvVar}, - }, - &cli.StringFlag{ - Name: commitRefFlag, - Usage: "commit ref", - EnvVars: []string{environ.CommitRefEnvVar}, - }, - &cli.StringFlag{ - Name: commitSHAFlag, - Usage: "commit sha", - EnvVars: []string{environ.CommitSHAEnvVar}, - }, - } -} - -// commitFromContext creates a plugin.Commit from the cli.Context. -func commitFromContext(ctx *cli.Context) plugin.Commit { - return plugin.Commit{ - After: ctx.String(commitAfterFlag), - Author: ctx.String(commitAuthorFlag), - AuthorAvatar: ctx.String(commitAuthorAvatarFlag), - AuthorEmail: ctx.String(commitAuthorEmailFlag), - AuthorName: ctx.String(commitAuthorNameFlag), - Before: ctx.String(commitBeforeFlag), - Branch: ctx.String(commitBranchFlag), - Link: ctx.String(commitLinkFlag), - Message: ctx.String(commitMessageFlag), - Ref: ctx.String(commitRefFlag), - SHA: ctx.String(commitSHAFlag), - } -} - -//--------------------------------------------------------------------- -// Stage Flags -//--------------------------------------------------------------------- - -const ( - stageArchFlag = "stage.arch" - stageDependsOnFlag = "stage.depends-on" - stageFinishedFlag = "stage.finished" - stageKindFlag = "stage.kind" - stageMachineFlag = "stage.machine" - stageNameFlag = "stage.name" - stageNumberFlag = "stage.number" - stageOSFlag = "stage.os" - stageStartedFlag = "stage.started" - stageStatusFlag = "stage.status" - stageTypeFlag = "stage.type" - stageVariantFlag = "stage.variant" - stageVersionFlag = "stage.version" -) - -// stageFlags has the cli.Flags for the plugin.Stage -func stageFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: stageArchFlag, - Usage: "stage arch", - EnvVars: []string{environ.StageArchEnvVar}, - }, - &cli.StringSliceFlag{ - Name: stageDependsOnFlag, - Usage: "stage depends on", - EnvVars: []string{environ.StageDependsOnEnvVar}, - }, - &cli.Int64Flag{ - Name: stageFinishedFlag, - Usage: "stage finished", - EnvVars: []string{environ.StageFinishedEnvVar}, - }, - &cli.StringFlag{ - Name: stageKindFlag, - Usage: "stage kind", - EnvVars: []string{environ.StageKindEnvVar}, - }, - &cli.StringFlag{ - Name: stageMachineFlag, - Usage: "stage machine", - EnvVars: []string{environ.StageMachineEnvVar}, - }, - &cli.StringFlag{ - Name: stageNameFlag, - Usage: "stage name", - EnvVars: []string{environ.StageNameEnvVar}, - }, - &cli.IntFlag{ - Name: stageNumberFlag, - Usage: "stage number", - EnvVars: []string{environ.StageNumberEnvVar}, - }, - &cli.StringFlag{ - Name: stageOSFlag, - Usage: "stage os", - EnvVars: []string{environ.StageOSEnvVar}, - }, - &cli.Int64Flag{ - Name: stageStartedFlag, - Usage: "stage started", - EnvVars: []string{environ.StageStartedEnvVar}, - }, - &cli.StringFlag{ - Name: stageStatusFlag, - Usage: "stage status", - EnvVars: []string{environ.StageStatusEnvVar}, - }, - &cli.StringFlag{ - Name: stageTypeFlag, - Usage: "stage type", - EnvVars: []string{environ.StageTypeEnvVar}, - }, - &cli.StringFlag{ - Name: stageVariantFlag, - Usage: "stage variant", - EnvVars: []string{environ.StageVariantEnvVar}, - }, - &cli.StringFlag{ - Name: stageVersionFlag, - Usage: "stage version", - EnvVars: []string{environ.StageVersionEnvVar}, - }, - } -} - -// stageFromContext creates a plugin.Stage from the cli.Context. -func stageFromContext(ctx *cli.Context) plugin.Stage { - return plugin.Stage{ - Arch: ctx.String(stageArchFlag), - DependsOn: ctx.StringSlice(stageDependsOnFlag), - Finished: time.Unix(ctx.Int64(stageFinishedFlag), 0), - Kind: ctx.String(stageKindFlag), - Machine: ctx.String(stageMachineFlag), - Name: ctx.String(stageNameFlag), - Number: ctx.Int(stageNumberFlag), - OS: ctx.String(stageOSFlag), - Started: time.Unix(ctx.Int64(stageStartedFlag), 0), - Status: ctx.String(stageStatusFlag), - Type: ctx.String(stageTypeFlag), - Variant: ctx.String(stageVariantFlag), - Version: ctx.String(stageVersionFlag), - } -} - -//--------------------------------------------------------------------- -// Step Flags -//--------------------------------------------------------------------- - -const ( - // stepNameFlag corresponds to plugin.Step.Name. - stepNameFlag = "step.name" - // stepNumberFlag corresponds to plugin.Step.Number. - stepNumberFlag = "step.number" -) - -// stepFlags has the cli.Flags for the plugin.Step. -func stepFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: stepNameFlag, - Usage: "step name", - EnvVars: []string{environ.StepNameEnvVar}, - }, - &cli.StringFlag{ - Name: stepNumberFlag, - Usage: "step number", - EnvVars: []string{environ.StepNumberEnvVar}, - }, - } -} - -// stepFromContext creates a plugin.Step from the cli.Context. -func stepFromContext(ctx *cli.Context) plugin.Step { - return plugin.Step{ - Name: ctx.String(stepNameFlag), - Number: ctx.Int(stepNumberFlag), - } -} - -//--------------------------------------------------------------------- -// SemVer Flags -//--------------------------------------------------------------------- - -const ( - semVerBuildFlag = "semver.build" - semVerErrorFlag = "semver.error" - semVerMajorFlag = "semver.major" - semVerMinorFlag = "semver.minor" - semVerPatchFlag = "semver.patch" - semVerPrereleaseFlag = "semver.prerelease" - semVerShortFlag = "semver.short" - semVerVersionFlag = "semver.version" -) - -// semVerFlags has the cli.Flags for the plugin.SemVer. -func semVerFlags() []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: semVerBuildFlag, - Usage: "semver build", - EnvVars: []string{environ.SemVerBuildEnvVar}, - }, - &cli.StringFlag{ - Name: semVerErrorFlag, - Usage: "semver error", - EnvVars: []string{environ.SemVerErrorEnvVar}, - }, - &cli.StringFlag{ - Name: semVerMajorFlag, - Usage: "semver major", - EnvVars: []string{environ.SemVerMajorEnvVar}, - }, - &cli.StringFlag{ - Name: semVerMinorFlag, - Usage: "semver minor", - EnvVars: []string{environ.SemVerMinorEnvVar}, - }, - &cli.StringFlag{ - Name: semVerPatchFlag, - Usage: "semver patch", - EnvVars: []string{environ.SemVerPatchEnvVar}, - }, - &cli.StringFlag{ - Name: semVerPrereleaseFlag, - Usage: "semver prerelease", - EnvVars: []string{environ.SemVerPrereleaseEnvVar}, - }, - &cli.StringFlag{ - Name: semVerShortFlag, - Usage: "semver short", - EnvVars: []string{environ.SemVerShortEnvVar}, - }, - &cli.StringFlag{ - Name: semVerVersionFlag, - Usage: "semver version", - EnvVars: []string{environ.SemVerVersionEnvVar}, - }, - } -} - -// semVerFromContext creates a plugin.Step from the cli.Context. -func semVerFromContext(ctx *cli.Context) plugin.SemVer { - return plugin.SemVer{ - Build: ctx.String(semVerBuildFlag), - Error: ctx.String(semVerErrorFlag), - Major: ctx.String(semVerMajorFlag), - Minor: ctx.String(semVerMinorFlag), - Patch: ctx.String(semVerPatchFlag), - Prerelease: ctx.String(semVerPrereleaseFlag), - Short: ctx.String(semVerShortFlag), - Version: ctx.String(semVerVersionFlag), - } -} diff --git a/pkg/urfave/httptrace.go b/trace/http.go similarity index 95% rename from pkg/urfave/httptrace.go rename to trace/http.go index 129ba9f..c7703fb 100644 --- a/pkg/urfave/httptrace.go +++ b/trace/http.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an Apache 2.0 license that can be // found in the LICENSE file. -package urfave +package trace import ( "context" @@ -14,8 +14,8 @@ import ( "github.com/sirupsen/logrus" ) -// traceHTTP uses httptrace to log all network activity for HTTP requests. -func traceHTTP(ctx context.Context) context.Context { +// HTTP uses httptrace to log all network activity for HTTP requests. +func HTTP(ctx context.Context) context.Context { return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ GetConn: func(hostPort string) { logrus.WithField("host-port", hostPort).Trace("ClientTrace.GetConn") diff --git a/urfave/build.go b/urfave/build.go new file mode 100644 index 0000000..1cdb15b --- /dev/null +++ b/urfave/build.go @@ -0,0 +1,121 @@ +// 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 urfave + +import ( + "time" + + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// buildFlags has the cli.Flags for the drone.Build. +func buildFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "build.branch", + Usage: "build branch", + EnvVars: []string{"DRONE_BRANCH"}, + }, + &cli.IntFlag{ + Name: "build.pull-request", + Usage: "build pull request", + EnvVars: []string{"DRONE_PULL_REQUEST"}, + }, + &cli.StringFlag{ + Name: "build.tag", + Usage: "build tag", + EnvVars: []string{"DRONE_TAG"}, + }, + &cli.StringFlag{ + Name: "build.source-branch", + Usage: "build source branch", + EnvVars: []string{"DRONE_SOURCE_BRANCH"}, + }, + &cli.StringFlag{ + Name: "build.target-branch", + Usage: "build target branch", + EnvVars: []string{"DRONE_TARGET_BRANCH"}, + }, + &cli.IntFlag{ + Name: "build.number", + Usage: "build number", + EnvVars: []string{"DRONE_BUILD_NUMBER"}, + }, + &cli.IntFlag{ + Name: "build.parent", + Usage: "build parent", + EnvVars: []string{"DRONE_BUILD_PARENT"}, + }, + &cli.StringFlag{ + Name: "build.event", + Usage: "build event", + EnvVars: []string{"DRONE_BUILD_EVENT"}, + }, + &cli.StringFlag{ + Name: "build.action", + Usage: "build action", + EnvVars: []string{"DRONE_BUILD_ACTION"}, + }, + &cli.StringFlag{ + Name: "build.status", + Usage: "build status", + EnvVars: []string{"DRONE_BUILD_STATUS"}, + }, + &cli.StringFlag{ + Name: "build.created", + Usage: "build created", + EnvVars: []string{"DRONE_BUILD_CREATED"}, + }, + &cli.StringFlag{ + Name: "build.started", + Usage: "build started", + EnvVars: []string{"DRONE_BUILD_STARTED"}, + }, + &cli.StringFlag{ + Name: "build.finished", + Usage: "build finished", + EnvVars: []string{"DRONE_BUILD_FINISHED"}, + }, + &cli.StringFlag{ + Name: "build.deploy-to", + Usage: "build deploy to", + EnvVars: []string{"DRONE_DEPLOY_TO"}, + }, + &cli.StringSliceFlag{ + Name: "build.failed-stages", + Usage: "build failed stages", + EnvVars: []string{"DRONE_FAILED_STAGES"}, + }, + &cli.StringSliceFlag{ + Name: "build.failed-steps", + Usage: "build failed steps", + EnvVars: []string{"DRONE_FAILED_STEPS"}, + }, + } +} + +// buildFromContext creates a drone.Build from the cli.Context. +func buildFromContext(ctx *cli.Context) drone.Build { + return drone.Build{ + Branch: ctx.String("build.branch"), + PullRequest: ctx.Int("build.pull-request"), + Tag: ctx.String("build.tag"), + SourceBranch: ctx.String("build.source-branch"), + TargetBranch: ctx.String("build.target-branch"), + Number: ctx.Int("build.number"), + Parent: ctx.Int("build.parent"), + Event: ctx.String("build.event"), + Action: ctx.String("build.action"), + Status: ctx.String("build.status"), + Created: time.Unix(ctx.Int64("build.created"), 0), + Started: time.Unix(ctx.Int64("build.started"), 0), + Finished: time.Unix(ctx.Int64("build.finished"), 0), + DeployTo: ctx.String("build.deploy-to"), + FailedStages: ctx.StringSlice("build.failed-stages"), + FailedSteps: ctx.StringSlice("build.failed-steps"), + } +} diff --git a/urfave/commit.go b/urfave/commit.go new file mode 100644 index 0000000..097f24a --- /dev/null +++ b/urfave/commit.go @@ -0,0 +1,111 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// commitFlags has the cli.Flags for the drone.Commit. +func commitFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "commit.sha", + Usage: "commit sha", + EnvVars: []string{ + "DRONE_COMMIT", + "DRONE_COMMIT_SHA", + }, + }, + &cli.StringFlag{ + Name: "commit.before", + Usage: "commit before", + EnvVars: []string{ + "DRONE_COMMIT_BEFORE", + }, + }, + &cli.StringFlag{ + Name: "commit.after", + Usage: "commit after", + EnvVars: []string{ + "DRONE_COMMIT_AFTER", + }, + }, + &cli.StringFlag{ + Name: "commit.ref", + Usage: "commit ref", + EnvVars: []string{ + "DRONE_COMMIT_REF", + }, + }, + &cli.StringFlag{ + Name: "commit.branch", + Usage: "commit branch", + EnvVars: []string{ + "DRONE_COMMIT_BRANCH", + }, + }, &cli.StringFlag{ + Name: "commit.link", + Usage: "commit link", + EnvVars: []string{ + "DRONE_COMMIT_LINK", + }, + }, + &cli.StringFlag{ + Name: "commit.message", + Usage: "commit message", + EnvVars: []string{ + "DRONE_COMMIT_MESSAGE", + }, + }, + &cli.StringFlag{ + Name: "commit.author", + Usage: "commit author", + EnvVars: []string{ + "DRONE_COMMIT_AUTHOR", + }, + }, + &cli.StringFlag{ + Name: "commit.author-name", + Usage: "commit author name", + EnvVars: []string{ + "DRONE_COMMIT_AUTHOR_NAME", + }, + }, + &cli.StringFlag{ + Name: "commit.author-email", + Usage: "commit author email", + EnvVars: []string{ + "DRONE_COMMIT_AUTHOR_EMAIL", + }, + }, + &cli.StringFlag{ + Name: "commit.author-avatar", + Usage: "commit author avatar", + EnvVars: []string{ + "DRONE_COMMIT_AUTHOR_AVATAR", + }, + }, + } +} + +// commitFromContext creates a drone.Commit from the cli.Context. +func commitFromContext(ctx *cli.Context) drone.Commit { + return drone.Commit{ + SHA: ctx.String("commit.sha"), + Before: ctx.String("commit.before"), + After: ctx.String("commit.after"), + Ref: ctx.String("commit.ref"), + Branch: ctx.String("commit.branch"), + Link: ctx.String("commit.link"), + Message: ctx.String("commit.message"), + Author: ctx.String("commit.author"), + AuthorName: ctx.String("commit.author-name"), + AuthorEmail: ctx.String("commit.author-email"), + AuthorAvatar: ctx.String("commit.author-avatar"), + } +} diff --git a/urfave/doc.go b/urfave/doc.go new file mode 100644 index 0000000..3fbb641 --- /dev/null +++ b/urfave/doc.go @@ -0,0 +1,37 @@ +// 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 urfave provides helpers for interacting with the `urfave/cli` +// package when creating plugins for use by the Drone CI/CD service. +// +// Drone communicates to plugins by passing in environment variables that have +// information on the currently executing build. The `urfave/cli` package can +// read these environment variables and extract them into structs. +// +// import ( +// "github.com/drone-plugins/drone-plugin-lib/urfave" +// "github.com/urfave/cli/v2" +// ) +// +// func main() { +// app := cli.NewApp() +// app.Name = "plugin name" +// app.Action = run +// app.Flags = []cli.Flag{ +// // All my plugin flags +// } +// +// app.Flags = append( +// app.Flags, +// urfave.Flags()..., +// ) +// } +// +// func run(ctx *cli.Context) error { +// pipeline := urfave.FromContext(ctx) +// ... +// return nil +// } +package urfave diff --git a/pkg/urfave/logging.go b/urfave/logging.go similarity index 62% rename from pkg/urfave/logging.go rename to urfave/logging.go index 357bbac..eaf3757 100644 --- a/pkg/urfave/logging.go +++ b/urfave/logging.go @@ -10,26 +10,24 @@ import ( "github.com/urfave/cli/v2" ) -const logLevelFlag = "log-level" - +// loggingFlags has the cli.Flags for logging config. func loggingFlags() []cli.Flag { return []cli.Flag{ &cli.StringFlag{ - Name: logLevelFlag, - Usage: "logging-level", + Name: "log-level", + Usage: "log level", EnvVars: []string{"PLUGIN_LOG_LEVEL"}, }, } } -// LoggingFromContext sets the logrus logging level. -func LoggingFromContext(ctx *cli.Context) { - lvl, err := logrus.ParseLevel(ctx.String(logLevelFlag)) +// loggingFromContext sets the logrus logging level. +func loggingFromContext(ctx *cli.Context) { + lvl, err := logrus.ParseLevel(ctx.String("log-level")) if err != nil { lvl = logrus.InfoLevel } logrus.SetLevel(lvl) - logrus.WithField("level", lvl.String()).Info("setup logging") } diff --git a/urfave/network.go b/urfave/network.go new file mode 100644 index 0000000..082aeb5 --- /dev/null +++ b/urfave/network.go @@ -0,0 +1,73 @@ +// 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 urfave + +import ( + "context" + "crypto/tls" + "net" + "net/http" + "time" + + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/drone-plugins/drone-plugin-lib/trace" + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +// networkFlags has the cli.Flags for the drone.Network. +func networkFlags() []cli.Flag { + return []cli.Flag{ + &cli.BoolFlag{ + Name: "transport.skip-verify", + Usage: "skip ssl verify", + EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, + }, + } +} + +// networkFromContext creates a drone.Network from the cli.Context. +func networkFromContext(c *cli.Context) drone.Network { + dialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + } + + transport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: dialer.DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + + ctx := context.Background() + skipVerify := c.Bool("transport.skip-verify") + + if skipVerify { + logrus.Warning("ssl verification is turned off") + + transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, + } + } + + if c.String("log-level") == logrus.TraceLevel.String() { + ctx = trace.HTTP(ctx) + } + + client := &http.Client{ + Transport: transport, + } + + return drone.Network{ + Context: ctx, + SkipVerify: skipVerify, + Client: client, + } +} diff --git a/urfave/repo.go b/urfave/repo.go new file mode 100644 index 0000000..09a2a70 --- /dev/null +++ b/urfave/repo.go @@ -0,0 +1,105 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// repoFlags has the cli.Flags for the drone.Repo +func repoFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "repo.slug", + Usage: "repo slug", + EnvVars: []string{ + "DRONE_REPO", + }, + }, + &cli.StringFlag{ + Name: "repo.scm", + Usage: "repo scm", + EnvVars: []string{ + "DRONE_REPO_SCM", + }, + }, + &cli.StringFlag{ + Name: "repo.owner", + Usage: "repo owner", + EnvVars: []string{ + "DRONE_REPO_OWNER", + "DRONE_REPO_NAMESPACE", + }, + }, + &cli.StringFlag{ + Name: "repo.name", + Usage: "repo name", + EnvVars: []string{ + "DRONE_REPO_NAME", + }, + }, + &cli.StringFlag{ + Name: "repo.link", + Usage: "repo link", + EnvVars: []string{ + "DRONE_REPO_LINK", + }, + }, + &cli.StringFlag{ + Name: "repo.branch", + Usage: "repo branch", + EnvVars: []string{ + "DRONE_REPO_BRANCH", + }, + }, + &cli.StringFlag{ + Name: "repo.http-url", + Usage: "repo http url", + EnvVars: []string{ + "DRONE_REMOTE_URL", + "DRONE_GIT_HTTP_URL", + }, + }, + &cli.StringFlag{ + Name: "repo.ssh-url", + Usage: "repo ssh url", + EnvVars: []string{ + "DRONE_GIT_SSH_URL", + }, + }, + &cli.StringFlag{ + Name: "repo.visibility", + Usage: "repo visibility", + EnvVars: []string{ + "DRONE_REPO_VISIBILITY", + }, + }, + &cli.BoolFlag{ + Name: "repo.private", + Usage: "repo private", + EnvVars: []string{ + "DRONE_REPO_PRIVATE", + }, + }, + } +} + +// repoFromContext creates a drone.Repo from the cli.Context. +func repoFromContext(ctx *cli.Context) drone.Repo { + return drone.Repo{ + Slug: ctx.String("repo.slug"), + SCM: ctx.String("repo.scm"), + Owner: ctx.String("repo.owner"), + Name: ctx.String("repo.name"), + Link: ctx.String("repo.link"), + Branch: ctx.String("repo.branch"), + HTTPURL: ctx.String("repo.http-url"), + SSHURL: ctx.String("repo.ssh-url"), + Visibility: ctx.String("repo.visibility"), + Private: ctx.Bool("repo.private"), + } +} diff --git a/urfave/semver.go b/urfave/semver.go new file mode 100644 index 0000000..f831e0f --- /dev/null +++ b/urfave/semver.go @@ -0,0 +1,87 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// semVerFlags has the cli.Flags for the drone.SemVer. +func semVerFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "semver.version", + Usage: "semver version", + EnvVars: []string{ + "DRONE_SEMVER", + }, + }, + &cli.StringFlag{ + Name: "semver.major", + Usage: "semver major", + EnvVars: []string{ + "DRONE_SEMVER_MAJOR", + }, + }, + &cli.StringFlag{ + Name: "semver.minor", + Usage: "semver minor", + EnvVars: []string{ + "DRONE_SEMVER_MINOR", + }, + }, + &cli.StringFlag{ + Name: "semver.patch", + Usage: "semver patch", + EnvVars: []string{ + "DRONE_SEMVER_PATCH", + }, + }, + &cli.StringFlag{ + Name: "semver.prerelease", + Usage: "semver prerelease", + EnvVars: []string{ + "DRONE_SEMVER_PRERELEASE", + }, + }, + &cli.StringFlag{ + Name: "semver.build", + Usage: "semver build", + EnvVars: []string{ + "DRONE_SEMVER_BUILD", + }, + }, + &cli.StringFlag{ + Name: "semver.short", + Usage: "semver short", + EnvVars: []string{ + "DRONE_SEMVER_SHORT", + }, + }, + &cli.StringFlag{ + Name: "semver.error", + Usage: "semver error", + EnvVars: []string{ + "DRONE_SEMVER_ERROR", + }, + }, + } +} + +// semVerFromContext creates a drone.Step from the cli.Context. +func semVerFromContext(ctx *cli.Context) drone.SemVer { + return drone.SemVer{ + Version: ctx.String("semver.version"), + Major: ctx.String("semver.major"), + Minor: ctx.String("semver.minor"), + Patch: ctx.String("semver.patch"), + Prerelease: ctx.String("semver.prerelease"), + Build: ctx.String("semver.build"), + Short: ctx.String("semver.short"), + Error: ctx.String("semver.error"), + } +} diff --git a/urfave/stage.go b/urfave/stage.go new file mode 100644 index 0000000..4d8b387 --- /dev/null +++ b/urfave/stage.go @@ -0,0 +1,129 @@ +// 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 urfave + +import ( + "time" + + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// stageFlags has the cli.Flags for the drone.Stage. +func stageFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "stage.kind", + Usage: "stage kind", + EnvVars: []string{ + "DRONE_STAGE_KIND", + }, + }, + &cli.StringFlag{ + Name: "stage.type", + Usage: "stage type", + EnvVars: []string{ + "DRONE_STAGE_TYPE", + }, + }, + &cli.StringFlag{ + Name: "stage.name", + Usage: "stage name", + EnvVars: []string{ + "DRONE_STAGE_NAME", + }, + }, + &cli.IntFlag{ + Name: "stage.number", + Usage: "stage number", + EnvVars: []string{ + "DRONE_STAGE_NUMBER", + }, + }, + &cli.StringFlag{ + Name: "stage.machine", + Usage: "stage machine", + EnvVars: []string{ + "DRONE_STAGE_MACHINE", + }, + }, + &cli.StringFlag{ + Name: "stage.os", + Usage: "stage os", + EnvVars: []string{ + "DRONE_STAGE_OS", + }, + }, + &cli.StringFlag{ + Name: "stage.arch", + Usage: "stage arch", + EnvVars: []string{ + "DRONE_STAGE_ARCH", + }, + }, + &cli.StringFlag{ + Name: "stage.variant", + Usage: "stage variant", + EnvVars: []string{ + "DRONE_STAGE_VARIANT", + }, + }, + &cli.StringFlag{ + Name: "stage.version", + Usage: "stage version", + EnvVars: []string{ + "DRONE_STAGE_VERSION", + }, + }, + &cli.StringFlag{ + Name: "stage.status", + Usage: "stage status", + EnvVars: []string{ + "DRONE_STAGE_STATUS", + }, + }, + &cli.Int64Flag{ + Name: "stage.started", + Usage: "stage started", + EnvVars: []string{ + "DRONE_STAGE_STARTED", + }, + }, + &cli.Int64Flag{ + Name: "stage.finished", + Usage: "stage finished", + EnvVars: []string{ + "DRONE_STAGE_FINISHED", + }, + }, + &cli.StringSliceFlag{ + Name: "stage.depends-on", + Usage: "stage depends on", + EnvVars: []string{ + "DRONE_STAGE_DEPENDS_ON", + }, + }, + } +} + +// stageFromContext creates a drone.Stage from the cli.Context. +func stageFromContext(ctx *cli.Context) drone.Stage { + return drone.Stage{ + Kind: ctx.String("stage.kind"), + Type: ctx.String("stage.type"), + Name: ctx.String("stage.name"), + Number: ctx.Int("stage.number"), + Machine: ctx.String("stage.machine"), + OS: ctx.String("stage.os"), + Arch: ctx.String("stage.arch"), + Variant: ctx.String("stage.variant"), + Version: ctx.String("stage.version"), + Status: ctx.String("stage.status"), + Started: time.Unix(ctx.Int64("stage.started"), 0), + Finished: time.Unix(ctx.Int64("stage.finished"), 0), + DependsOn: ctx.StringSlice("stage.depends-on"), + } +} diff --git a/urfave/step.go b/urfave/step.go new file mode 100644 index 0000000..c31642e --- /dev/null +++ b/urfave/step.go @@ -0,0 +1,39 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// stepFlags has the cli.Flags for the drone.Step. +func stepFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "step.name", + Usage: "step name", + EnvVars: []string{ + "DRONE_STEP_NAME", + }, + }, + &cli.IntFlag{ + Name: "step.number", + Usage: "step number", + EnvVars: []string{ + "DRONE_STEP_NUMBER", + }, + }, + } +} + +// stepFromContext creates a drone.Step from the cli.Context. +func stepFromContext(ctx *cli.Context) drone.Step { + return drone.Step{ + Name: ctx.String("step.name"), + Number: ctx.Int("step.number"), + } +} diff --git a/urfave/system.go b/urfave/system.go new file mode 100644 index 0000000..6777531 --- /dev/null +++ b/urfave/system.go @@ -0,0 +1,48 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// systemFlags has the cli.Flags for the drone.System. +func systemFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "system.proto", + Usage: "system proto", + EnvVars: []string{ + "DRONE_SYSTEM_PROTO", + }, + }, + &cli.StringFlag{ + Name: "system.host", + Usage: "system host", + EnvVars: []string{ + "DRONE_SYSTEM_HOST", + "DRONE_SYSTEM_HOSTNAME", + }, + }, + &cli.StringFlag{ + Name: "system.version", + Usage: "system version", + EnvVars: []string{ + "DRONE_SYSTEM_VERSION", + }, + }, + } +} + +// systemFromContext creates a drone.System from the cli.Context. +func systemFromContext(ctx *cli.Context) drone.System { + return drone.System{ + Proto: ctx.String("system.proto"), + Host: ctx.String("system.host"), + Version: ctx.String("system.version"), + } +} diff --git a/urfave/urfave.go b/urfave/urfave.go new file mode 100644 index 0000000..f9bed8b --- /dev/null +++ b/urfave/urfave.go @@ -0,0 +1,44 @@ +// 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 urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" + "github.com/urfave/cli/v2" +) + +// Flags has the cli.Flags for the Drone plugin. +func Flags() []cli.Flag { + flags := []cli.Flag{} + + flags = append(flags, buildFlags()...) + flags = append(flags, repoFlags()...) + flags = append(flags, commitFlags()...) + flags = append(flags, stageFlags()...) + flags = append(flags, stepFlags()...) + flags = append(flags, semVerFlags()...) + flags = append(flags, systemFlags()...) + flags = append(flags, networkFlags()...) + flags = append(flags, loggingFlags()...) + + return flags +} + +// FromContext creates a drone.Pipeline from the cli.Context. +func FromContext(ctx *cli.Context) drone.Pipeline { + loggingFromContext(ctx) + + return drone.Pipeline{ + Network: networkFromContext(ctx), + Build: buildFromContext(ctx), + Repo: repoFromContext(ctx), + Commit: commitFromContext(ctx), + Stage: stageFromContext(ctx), + Step: stepFromContext(ctx), + SemVer: semVerFromContext(ctx), + System: systemFromContext(ctx), + } +}