github.com/kaydxh/golang@v0.0.131/pkg/webserver/app/version.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package app
    23  
    24  import (
    25  	"fmt"
    26  	"runtime"
    27  )
    28  
    29  type Version struct {
    30  	GitVersion   string `json:"gitVersion"`
    31  	GitCommit    string `json:"gitCommit"`
    32  	GitTreeState string `json:"gitTreeState"`
    33  	BuildDate    string `json:"buildDate"`
    34  	GoVersion    string `json:"goVersion"`
    35  	Compiler     string `json:"compiler"`
    36  	Platform     string `json:"platform"`
    37  	AppName      string `json:"appName"`
    38  }
    39  
    40  var (
    41  	// semantic version, derived by build scripts (see
    42  	// https://git.k8s.io/community/contributors/design-proposals/release/versioning.md
    43  	// for a detailed discussion of this field)
    44  	//
    45  	// TODO: This field is still called "gitVersion" for legacy
    46  	// reasons. For prerelease versions, the build metadata on the
    47  	// semantic version is a git hash, but the version itself is no
    48  	// longer the direct output of "git describe", but a slight
    49  	// translation to be semver compliant.
    50  
    51  	// NOTE: The $Format strings are replaced during 'git archive' thanks to the
    52  	// companion .gitattributes file containing 'export-subst' in this same
    53  	// directory.  See also https://git-scm.com/docs/gitattributes
    54  	gitVersion   string = "v0.0.0-master+$Format:%H$"
    55  	gitCommit    string = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
    56  	gitTreeState string = ""            // state of git tree, either "clean" or "dirty"
    57  
    58  	buildDate string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
    59  	appName   string = ""                     // app name
    60  )
    61  
    62  func GetVersion() Version {
    63  	// These variables typically come from -ldflags settings and in
    64  	// their absence fallback to the settings in pkg/version/base.go
    65  	return Version{
    66  		GitVersion:   gitVersion,
    67  		GitCommit:    gitCommit,
    68  		GitTreeState: gitTreeState,
    69  		BuildDate:    buildDate,
    70  		GoVersion:    runtime.Version(),
    71  		Compiler:     runtime.Compiler,
    72  		Platform:     fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
    73  		AppName:      appName,
    74  	}
    75  }