github.com/OpsMx/go-app-base@v0.0.24/version/version.go (about)

     1  // Copyright 2022 OpsMx, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package version
    16  
    17  import (
    18  	"fmt"
    19  )
    20  
    21  // Versions can be set linker.
    22  //
    23  // For example, to set `gitBranch`, an ldflags of `-X 'github.com/OpsMx/go-app-base/version.gitBranch=v1.4.2'`
    24  // could be used on the 'go build' command line.
    25  //
    26  // These are generally set in the Makefile or Dockerfile from information obtained from the git repo.
    27  var (
    28  	gitBranch = "dev"
    29  	gitHash   = "dev"
    30  	buildType = "unknown"
    31  )
    32  
    33  // GitBranch will return the envar, compiled-in var, or "dev" if none set.  Often this is
    34  // a tag, so will have a format such as `v1.0.2` or `v1.0.2-5-g12350123` for changes without
    35  // a specific tag.
    36  //
    37  // This can be used for "short" version strings, while VersionString would be more
    38  // exact.
    39  func GitBranch() string {
    40  	return gitBranch
    41  }
    42  
    43  // GitHash will return the envar, compiled-in var, or "dev" if none set.
    44  func GitHash() string {
    45  	return gitHash
    46  }
    47  
    48  // BuildType retuns whatever buildTime is set to by the linker.
    49  func BuildType() string {
    50  	return buildType
    51  }
    52  
    53  // VersionString returns a formatted version, git hash, and build type.
    54  func VersionString() string {
    55  	return fmt.Sprintf("version: %s, hash: %s, buildType: %s", gitBranch, gitHash, buildType)
    56  }