drone-plugin-lib/pkg/plugin/env.go

222 lines
7.6 KiB
Go
Raw Normal View History

2019-09-06 23:32:54 +00:00
// 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
2019-09-09 18:06:21 +00:00
import (
"os"
"strconv"
"strings"
"time"
)
2019-09-06 23:32:54 +00:00
// 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 (
2019-09-09 21:02:00 +00:00
// The following environment variables are being ignored currently
//
2019-09-09 23:16:32 +00:00
// * DRONE_COMMIT - Redundant with DRONE_COMMIT_SHA
2019-09-09 21:02:00 +00:00
// * DRONE_GIT_HTTP_URL
// * DRONE_GIT_SSH_URL
// * DRONE_REPO_NAMESPACE - Redundant to DRONE_REPO_OWNER
//---------------------------------------------------------------------
// 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"
2019-09-09 23:16:32 +00:00
//---------------------------------------------------------------------
// 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"
2019-09-07 00:13:04 +00:00
//---------------------------------------------------------------------
// 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.
2019-09-06 23:32:54 +00:00
StepNameEnvVar = "DRONE_STEP_NAME"
2019-09-07 00:13:04 +00:00
// StepNumberEnvVar corresponds to Step.Number.
2019-09-06 23:32:54 +00:00
StepNumberEnvVar = "DRONE_STEP_NUMBER"
2019-09-09 21:26:56 +00:00
//---------------------------------------------------------------------
// 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"
2019-09-06 23:32:54 +00:00
)
2019-09-09 18:06:21 +00:00
// 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)
}