0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-22 00:20:38 +00:00

Move environment variable definitions to an internal lib

This commit is contained in:
Don 2019-10-10 12:59:12 -07:00
parent 6b33d765ac
commit 99e18f9a03
3 changed files with 62 additions and 242 deletions

View File

@ -3,14 +3,7 @@
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package plugin
import (
"os"
"strconv"
"strings"
"time"
)
package environ
// List of enviornment variables set by Drone when running a step within a
// build stage.
@ -173,84 +166,3 @@ const (
// SemVerVersionEnvVar corresponds to SemVer.Version
SemVerVersionEnvVar = "DRONE_SEMVER"
)
// StringEnvVar gets the environment variable's value.
//
// If the value is not present then this returns the empty string.
func StringEnvVar(envVar string) string {
return envVarValue(envVar)
}
// StringSliceEnvVar gets the environment variable as a string slice.
//
// If the value is not present then this returns an empty slice.
func StringSliceEnvVar(envVar string) []string {
return strings.Split(envVarValue(envVar), ",")
}
// IntEnvVar gets the environment variable as an int.
//
// If the value is not present then this returns 0.
func IntEnvVar(envVar string) int {
return int(Int64EnvVar(envVar))
}
// Int64EnvVar gets the environment variable as an int64.
//
// If the value is not present then this returns 0.
func Int64EnvVar(envVar string) int64 {
value, err := strconv.ParseInt(envVarValue(envVar), 0, 64)
if err != nil {
return 0
}
return value
}
// UintEnvVar gets the environment variable as an uint.
//
// If the value is not present then this returns 0.
func UintEnvVar(envVar string) uint {
return uint(Uint64EnvVar(envVar))
}
// Uint64EnvVar gets the environment variable as an uint64.
//
// If the value is not present then this returns 0.
func Uint64EnvVar(envVar string) uint64 {
value, err := strconv.ParseUint(envVarValue(envVar), 0, 64)
if err != nil {
return 0
}
return value
}
// BoolEnvVar gets the environment variable as a bool.
//
// If the value is not present then this returns false.
func BoolEnvVar(envVar string) bool {
value, err := strconv.ParseBool(envVarValue(envVar))
if err != nil {
return false
}
return value
}
// TimeEnvVar gets the environment variable as a Time created from a unix
// timestamp.
//
// If the value is not present then this returns time.Unix(0).
func TimeEnvVar(envVar string) time.Time {
return time.Unix(Int64EnvVar(envVar), 0)
}
// envVarValue returns the first matched environment variable or the empty
// string if none was found.
func envVarValue(envVar string) string {
return os.Getenv(envVar)
}

View File

@ -157,97 +157,3 @@ type (
Version string
}
)
// BuildFromEnv creates a Build from the environment variables used by Drone.
func BuildFromEnv() Build {
return Build{
Action: StringEnvVar(BuildActionEnvVar),
Created: TimeEnvVar(BuildCreatedEnvVar),
DeployTo: StringEnvVar(BuildDeployToEnvVar),
Event: StringEnvVar(BuildEventEnvVar),
FailedStages: StringSliceEnvVar(BuildFailedStagesEnvVar),
FailedSteps: StringSliceEnvVar(BuildFailedStepsEnvVar),
Finished: TimeEnvVar(BuildFinishedEnvVar),
Number: IntEnvVar(BuildNumberEnvVar),
Parent: IntEnvVar(BuildParentEnvVar),
PullRequest: IntEnvVar(BuildPullRequestEnvVar),
SourceBranch: StringEnvVar(BuildSourceBranchEnvVar),
Started: TimeEnvVar(BuildStartedEnvVar),
Status: StringEnvVar(BuildStatusEnvVar),
Tag: StringEnvVar(BuildTagEnvVar),
TargetBranch: StringEnvVar(BuildTargetBranchEnvVar),
}
}
// RepoFromEnv creates a Repo from the environment variables used by Drone.
func RepoFromEnv() Repo {
return Repo{
DefaultBranch: StringEnvVar(RepoDefaultBranchEnvVar),
FullName: StringEnvVar(RepoFullNameEnvVar),
Link: StringEnvVar(RepoLinkEnvVar),
Name: StringEnvVar(RepoNameEnvVar),
Owner: StringEnvVar(RepoOwnerEnvVar),
Private: BoolEnvVar(RepoPrivateEnvVar),
RemoteURL: StringEnvVar(RepoRemoteURLEnvVar),
SCM: StringEnvVar(RepoSCMEnvVar),
Visibility: StringEnvVar(RepoVisibilityEnvVar),
}
}
// CommitFromEnv creates a Commit from the environment variables used by Drone.
func CommitFromEnv() Commit {
return Commit{
After: StringEnvVar(CommitAfterEnvVar),
Author: StringEnvVar(CommitAuthorEnvVar),
AuthorAvatar: StringEnvVar(CommitAuthorAvatarEnvVar),
AuthorEmail: StringEnvVar(CommitAuthorEmailEnvVar),
AuthorName: StringEnvVar(CommitAuthorNameEnvVar),
Before: StringEnvVar(CommitBeforeEnvVar),
Branch: StringEnvVar(CommitBranchEnvVar),
Link: StringEnvVar(CommitLinkEnvVar),
Message: StringEnvVar(CommitMessageEnvVar),
Ref: StringEnvVar(CommitRefEnvVar),
SHA: StringEnvVar(CommitSHAEnvVar),
}
}
// StageFromEnv creates a Stage from the environment variables used by Drone.
func StageFromEnv() Stage {
return Stage{
Arch: StringEnvVar(StageArchEnvVar),
DependsOn: StringSliceEnvVar(StageDependsOnEnvVar),
Finished: TimeEnvVar(StageFinishedEnvVar),
Kind: StringEnvVar(StageKindEnvVar),
Machine: StringEnvVar(StageMachineEnvVar),
Name: StringEnvVar(StageNameEnvVar),
Number: IntEnvVar(StageNumberEnvVar),
OS: StringEnvVar(StageOSEnvVar),
Started: TimeEnvVar(StageStartedEnvVar),
Status: StringEnvVar(StageStatusEnvVar),
Type: StringEnvVar(StageTypeEnvVar),
Variant: StringEnvVar(StageVariantEnvVar),
Version: StringEnvVar(StageVersionEnvVar),
}
}
// StepFromEnv creates a Step from the environment variables used by Drone.
func StepFromEnv() Step {
return Step{
Name: StringEnvVar(StepNameEnvVar),
Number: IntEnvVar(StepNumberEnvVar),
}
}
// SemVerFromEnv creates a SemVer from the environment variables used by Drone.
func SemVerFromEnv() SemVer {
return SemVer{
Build: StringEnvVar(SemVerBuildEnvVar),
Error: StringEnvVar(SemVerErrorEnvVar),
Major: StringEnvVar(SemVerMajorEnvVar),
Minor: StringEnvVar(SemVerMinorEnvVar),
Patch: StringEnvVar(SemVerPatchEnvVar),
Prerelease: StringEnvVar(SemVerPrereleaseEnvVar),
Short: StringEnvVar(SemVerShortEnvVar),
Version: StringEnvVar(SemVerVersionEnvVar),
}
}

View File

@ -32,8 +32,10 @@ package urfave
import (
"time"
"github.com/drone-plugins/drone-plugin-lib/pkg/plugin"
"github.com/urfave/cli"
"github.com/drone-plugins/drone-plugin-lib/internal/environ"
"github.com/drone-plugins/drone-plugin-lib/pkg/plugin"
)
//---------------------------------------------------------------------
@ -79,91 +81,91 @@ func BuildFlags() []cli.Flag {
cli.StringFlag{
Name: BuildActionFlag,
Usage: "build action",
EnvVar: plugin.BuildActionEnvVar,
EnvVar: environ.BuildActionEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildCreatedFlag,
Usage: "build created",
EnvVar: plugin.BuildCreatedEnvVar,
EnvVar: environ.BuildCreatedEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildDeployToFlag,
Usage: "build deploy to",
EnvVar: plugin.BuildDeployToEnvVar,
EnvVar: environ.BuildDeployToEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildEventFlag,
Usage: "build event",
EnvVar: plugin.BuildEventEnvVar,
EnvVar: environ.BuildEventEnvVar,
Hidden: true,
},
cli.StringSliceFlag{
Name: BuildFailedStagesFlag,
Usage: "build failed stages",
EnvVar: plugin.BuildFailedStagesEnvVar,
EnvVar: environ.BuildFailedStagesEnvVar,
Hidden: true,
},
cli.StringSliceFlag{
Name: BuildFailedStepsFlag,
Usage: "build failed steps",
EnvVar: plugin.BuildFailedStepsEnvVar,
EnvVar: environ.BuildFailedStepsEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildFinishedFlag,
Usage: "build finished",
EnvVar: plugin.BuildFinishedEnvVar,
EnvVar: environ.BuildFinishedEnvVar,
Hidden: true,
},
cli.IntFlag{
Name: BuildNumberFlag,
Usage: "build number",
EnvVar: plugin.BuildNumberEnvVar,
EnvVar: environ.BuildNumberEnvVar,
Hidden: true,
},
cli.IntFlag{
Name: BuildParentFlag,
Usage: "build parent",
EnvVar: plugin.BuildParentEnvVar,
EnvVar: environ.BuildParentEnvVar,
Hidden: true,
},
cli.IntFlag{
Name: BuildPullRequestFlag,
Usage: "build pull request",
EnvVar: plugin.BuildPullRequestEnvVar,
EnvVar: environ.BuildPullRequestEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildSourceBranchFlag,
Usage: "build source branch",
EnvVar: plugin.BuildSourceBranchEnvVar,
EnvVar: environ.BuildSourceBranchEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildStartedFlag,
Usage: "build started",
EnvVar: plugin.BuildStartedEnvVar,
EnvVar: environ.BuildStartedEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildStatusFlag,
Usage: "build status",
EnvVar: plugin.BuildStatusEnvVar,
EnvVar: environ.BuildStatusEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildTagFlag,
Usage: "build tag",
EnvVar: plugin.BuildTagEnvVar,
EnvVar: environ.BuildTagEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: BuildTargetBranchFlag,
Usage: "build target branch",
EnvVar: plugin.BuildTargetBranchEnvVar,
EnvVar: environ.BuildTargetBranchEnvVar,
Hidden: true,
},
}
@ -221,55 +223,55 @@ func RepoFlags() []cli.Flag {
cli.StringFlag{
Name: RepoDefaultBranchFlag,
Usage: "repo default branch",
EnvVar: plugin.RepoDefaultBranchEnvVar,
EnvVar: environ.RepoDefaultBranchEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoFullNameFlag,
Usage: "repo full name",
EnvVar: plugin.RepoFullNameEnvVar,
EnvVar: environ.RepoFullNameEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoLinkFlag,
Usage: "repo link",
EnvVar: plugin.RepoLinkEnvVar,
EnvVar: environ.RepoLinkEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoNameFlag,
Usage: "repo name",
EnvVar: plugin.RepoNameEnvVar,
EnvVar: environ.RepoNameEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoOwnerFlag,
Usage: "repo owner",
EnvVar: plugin.RepoOwnerEnvVar,
EnvVar: environ.RepoOwnerEnvVar,
Hidden: true,
},
cli.BoolFlag{
Name: RepoPrivateFlag,
Usage: "repo private",
EnvVar: plugin.RepoPrivateEnvVar,
EnvVar: environ.RepoPrivateEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoRemoteURLFlag,
Usage: "repo remote url",
EnvVar: plugin.RepoRemoteURLEnvVar,
EnvVar: environ.RepoRemoteURLEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoSCMFlag,
Usage: "repo scm",
EnvVar: plugin.RepoSCMEnvVar,
EnvVar: environ.RepoSCMEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: RepoVisibilityFlag,
Usage: "repo visibility",
EnvVar: plugin.RepoVisibilityEnvVar,
EnvVar: environ.RepoVisibilityEnvVar,
Hidden: true,
},
}
@ -325,67 +327,67 @@ func CommitFlags() []cli.Flag {
cli.StringFlag{
Name: CommitAfterFlag,
Usage: "commit after",
EnvVar: plugin.CommitAfterEnvVar,
EnvVar: environ.CommitAfterEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitAuthorFlag,
Usage: "commit author",
EnvVar: plugin.CommitAuthorEnvVar,
EnvVar: environ.CommitAuthorEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitAuthorAvatarFlag,
Usage: "commit author avatar",
EnvVar: plugin.CommitAuthorAvatarEnvVar,
EnvVar: environ.CommitAuthorAvatarEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitAuthorEmailFlag,
Usage: "commit author email",
EnvVar: plugin.CommitAuthorEmailEnvVar,
EnvVar: environ.CommitAuthorEmailEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitAuthorNameFlag,
Usage: "commit author name",
EnvVar: plugin.CommitAuthorNameEnvVar,
EnvVar: environ.CommitAuthorNameEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitBeforeFlag,
Usage: "commit before",
EnvVar: plugin.CommitBeforeEnvVar,
EnvVar: environ.CommitBeforeEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitBranchFlag,
Usage: "commit branch",
EnvVar: plugin.CommitBranchEnvVar,
EnvVar: environ.CommitBranchEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitLinkFlag,
Usage: "commit link",
EnvVar: plugin.CommitLinkEnvVar,
EnvVar: environ.CommitLinkEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitMessageFlag,
Usage: "commit message",
EnvVar: plugin.CommitMessageEnvVar,
EnvVar: environ.CommitMessageEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitRefFlag,
Usage: "commit ref",
EnvVar: plugin.CommitRefEnvVar,
EnvVar: environ.CommitRefEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: CommitSHAFlag,
Usage: "commit sha",
EnvVar: plugin.CommitSHAEnvVar,
EnvVar: environ.CommitSHAEnvVar,
Hidden: true,
},
}
@ -447,69 +449,69 @@ func StageFlags() []cli.Flag {
cli.StringFlag{
Name: StageArchFlag,
Usage: "stage arch",
EnvVar: plugin.StageArchEnvVar,
EnvVar: environ.StageArchEnvVar,
Hidden: true,
},
cli.StringSliceFlag{
Name: StageDependsOnFlag,
Usage: "stage depends on",
EnvVar: plugin.StageDependsOnEnvVar,
EnvVar: environ.StageDependsOnEnvVar,
Hidden: true,
},
cli.Int64Flag{
Name: StageFinishedFlag,
Usage: "stage finished",
EnvVar: plugin.StageFinishedEnvVar,
EnvVar: environ.StageFinishedEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageKindFlag,
Usage: "stage kind",
EnvVar: plugin.StageKindEnvVar,
EnvVar: environ.StageKindEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageMachineFlag,
Usage: "stage machine",
EnvVar: plugin.StageMachineEnvVar,
EnvVar: environ.StageMachineEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageNameFlag,
Usage: "stage name",
EnvVar: plugin.StageNameEnvVar,
EnvVar: environ.StageNameEnvVar,
Hidden: true,
}, cli.IntFlag{
Name: StageNumberFlag,
Usage: "stage number",
EnvVar: plugin.StageNumberEnvVar,
EnvVar: environ.StageNumberEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageOSFlag,
Usage: "stage os",
EnvVar: plugin.StageOSEnvVar,
EnvVar: environ.StageOSEnvVar,
Hidden: true,
}, cli.Int64Flag{
Name: StageStartedFlag,
Usage: "stage started",
EnvVar: plugin.StageStartedEnvVar,
EnvVar: environ.StageStartedEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageStatusFlag,
Usage: "stage status",
EnvVar: plugin.StageStatusEnvVar,
EnvVar: environ.StageStatusEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageTypeFlag,
Usage: "stage type",
EnvVar: plugin.StageTypeEnvVar,
EnvVar: environ.StageTypeEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageVariantFlag,
Usage: "stage variant",
EnvVar: plugin.StageVariantEnvVar,
EnvVar: environ.StageVariantEnvVar,
Hidden: true,
}, cli.StringFlag{
Name: StageVersionFlag,
Usage: "stage version",
EnvVar: plugin.StageVersionEnvVar,
EnvVar: environ.StageVersionEnvVar,
Hidden: true,
},
}
@ -551,13 +553,13 @@ func StepFlags() []cli.Flag {
cli.StringFlag{
Name: StepNameFlag,
Usage: "step name",
EnvVar: plugin.StepNameEnvVar,
EnvVar: environ.StepNameEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: StepNumberFlag,
Usage: "step number",
EnvVar: plugin.StepNumberEnvVar,
EnvVar: environ.StepNumberEnvVar,
Hidden: true,
},
}
@ -600,49 +602,49 @@ func SemVerFlags() []cli.Flag {
cli.StringFlag{
Name: SemVerBuildFlag,
Usage: "semver build",
EnvVar: plugin.SemVerBuildEnvVar,
EnvVar: environ.SemVerBuildEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerErrorFlag,
Usage: "semver error",
EnvVar: plugin.SemVerErrorEnvVar,
EnvVar: environ.SemVerErrorEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerMajorFlag,
Usage: "semver major",
EnvVar: plugin.SemVerMajorEnvVar,
EnvVar: environ.SemVerMajorEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerMinorFlag,
Usage: "semver minor",
EnvVar: plugin.SemVerMinorEnvVar,
EnvVar: environ.SemVerMinorEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerPatchFlag,
Usage: "semver patch",
EnvVar: plugin.SemVerPatchEnvVar,
EnvVar: environ.SemVerPatchEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerPrereleaseFlag,
Usage: "semver prerelease",
EnvVar: plugin.SemVerPrereleaseEnvVar,
EnvVar: environ.SemVerPrereleaseEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerShortFlag,
Usage: "semver short",
EnvVar: plugin.SemVerShortEnvVar,
EnvVar: environ.SemVerShortEnvVar,
Hidden: true,
},
cli.StringFlag{
Name: SemVerVersionFlag,
Usage: "semver version",
EnvVar: plugin.SemVerVersionEnvVar,
EnvVar: environ.SemVerVersionEnvVar,
Hidden: true,
},
}