github.com/argoproj/argo-events@v1.9.1/version.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package argoevents
    18  
    19  import (
    20  	"fmt"
    21  	"runtime"
    22  )
    23  
    24  // Version information set by link flags during build. We fall back to these sane
    25  // default values when we build outside the Makefile context (e.g. go build or go test).
    26  var (
    27  	version      = "latest"               // value from VERSION file
    28  	buildDate    = "1970-01-01T00:00:00Z" // output from `date -u +'%Y-%m-%dT%H:%M:%SZ'`
    29  	gitCommit    = ""                     // output from `git rev-parse HEAD`
    30  	gitTag       = ""                     // output from `git describe --exact-match --tags HEAD` (if clean tree state)
    31  	gitTreeState = ""                     // determined from `git status --porcelain`. either 'clean' or 'dirty'
    32  )
    33  
    34  // Version contains Argo-Events version information
    35  type Version struct {
    36  	Version      string
    37  	BuildDate    string
    38  	GitCommit    string
    39  	GitTag       string
    40  	GitTreeState string
    41  	GoVersion    string
    42  	Compiler     string
    43  	Platform     string
    44  }
    45  
    46  // String outputs the version as a string
    47  func (v Version) String() string {
    48  	return v.Version
    49  }
    50  
    51  // GetVersion returns the version information
    52  func GetVersion() Version {
    53  	var versionStr string
    54  	if gitCommit != "" && gitTag != "" && gitTreeState == "clean" {
    55  		// if we have a clean tree state and the current commit is tagged,
    56  		// this is an official release.
    57  		versionStr = gitTag
    58  	} else {
    59  		// otherwise formulate a version string based on as much metadata
    60  		// information we have available.
    61  		versionStr = version
    62  		if len(gitCommit) >= 7 {
    63  			versionStr += "+" + gitCommit[0:7]
    64  			if gitTreeState != "clean" {
    65  				versionStr += ".dirty"
    66  			}
    67  		} else {
    68  			versionStr += "+unknown"
    69  		}
    70  	}
    71  	return Version{
    72  		Version:      versionStr,
    73  		BuildDate:    buildDate,
    74  		GitCommit:    gitCommit,
    75  		GitTag:       gitTag,
    76  		GitTreeState: gitTreeState,
    77  		GoVersion:    runtime.Version(),
    78  		Compiler:     runtime.Compiler,
    79  		Platform:     fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
    80  	}
    81  }