From 353ef0da17f4e1b59b1608645acb687af42f2822 Mon Sep 17 00:00:00 2001 From: Don Date: Mon, 9 Sep 2019 14:26:56 -0700 Subject: [PATCH] Add SemVer struct --- pkg/plugin/env.go | 21 +++++++++++++++++++++ pkg/plugin/types.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/pkg/plugin/env.go b/pkg/plugin/env.go index 1418d9d..99d2c29 100644 --- a/pkg/plugin/env.go +++ b/pkg/plugin/env.go @@ -88,6 +88,27 @@ const ( 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" ) // StringEnvVar gets the environment variable's value. diff --git a/pkg/plugin/types.go b/pkg/plugin/types.go index ad11a3a..1feed8d 100644 --- a/pkg/plugin/types.go +++ b/pkg/plugin/types.go @@ -99,6 +99,32 @@ type ( 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 + } ) // RepoFromEnv creates a Repo from the environment variables used by Drone. @@ -142,3 +168,17 @@ func StepFromEnv() Step { 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), + } +}