mirror of
https://github.com/thegeeklab/drone-plugin-lib.git
synced 2024-11-05 23:00:39 +00:00
144 lines
4.2 KiB
Go
144 lines
4.2 KiB
Go
// 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 (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 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 (
|
|
//---------------------------------------------------------------------
|
|
// 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"
|
|
)
|
|
|
|
// 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)
|
|
}
|