mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
417f45fc61
#15556 We will need to pay attention when releasing fleet (the github actions were modified to use the local file now). Should be reviewed by commits (first commit is the actual adding of the `version.go` file) - [X] Manual QA for all new/changed functionality Manually tested the following: - `Settings -> My account` on the UI and checked the `/version` endpoint response. (Or also visiting https://localhost:8080/version on a browser). - Ran `make fleetctl fleet`, `./build/fleetctl --version` and `./build/fleet version`.
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
/*
|
|
Package version provides utilities for displaying version information about a Go application.
|
|
|
|
To use this package, a program would set the package variables at build time, using the
|
|
-ldflags go build flag.
|
|
|
|
Example:
|
|
|
|
go build -ldflags "-X github.com/fleetdm/fleet/v4/server/version.version=1.0.0"
|
|
|
|
Available values and defaults to use with ldflags:
|
|
|
|
version = "unknown"
|
|
branch = "unknown"
|
|
revision = "unknown"
|
|
goVersion = "unknown"
|
|
buildDate = "unknown"
|
|
buildUser = "unknown"
|
|
appName = "unknown"
|
|
|
|
This file was copied (on December 2023) from https://github.com/fleetdm/kolide-kit, which is a fork of https://github.com/kolide/kit.
|
|
*/
|
|
package version
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"runtime"
|
|
)
|
|
|
|
// These values are private which ensures they can only be set with the build flags.
|
|
var (
|
|
version = "unknown"
|
|
branch = "unknown"
|
|
revision = "unknown"
|
|
goVersion = runtime.Version()
|
|
buildDate = "unknown"
|
|
buildUser = "unknown"
|
|
appName = "unknown"
|
|
)
|
|
|
|
// Info is a structure with version build information about the current application.
|
|
type Info struct {
|
|
Version string `json:"version"`
|
|
Branch string `json:"branch"`
|
|
Revision string `json:"revision"`
|
|
GoVersion string `json:"go_version"`
|
|
BuildDate string `json:"build_date"`
|
|
BuildUser string `json:"build_user"`
|
|
}
|
|
|
|
// Version returns a structure with the current version information.
|
|
func Version() Info {
|
|
return Info{
|
|
Version: version,
|
|
Branch: branch,
|
|
Revision: revision,
|
|
GoVersion: goVersion,
|
|
BuildDate: buildDate,
|
|
BuildUser: buildUser,
|
|
}
|
|
}
|
|
|
|
// Print outputs the application name and version string.
|
|
func Print() {
|
|
v := Version()
|
|
fmt.Printf("%s version %s\n", appName, v.Version)
|
|
}
|
|
|
|
// PrintFull prints the application name and detailed version information.
|
|
func PrintFull() {
|
|
v := Version()
|
|
fmt.Printf("%s - version %s\n", appName, v.Version)
|
|
fmt.Printf(" branch: \t%s\n", v.Branch)
|
|
fmt.Printf(" revision: \t%s\n", v.Revision)
|
|
fmt.Printf(" build date: \t%s\n", v.BuildDate)
|
|
fmt.Printf(" build user: \t%s\n", v.BuildUser)
|
|
fmt.Printf(" go version: \t%s\n", v.GoVersion)
|
|
}
|
|
|
|
// Handler returns an HTTP Handler which returns JSON formatted version information.
|
|
func Handler() http.Handler {
|
|
v := Version()
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
enc := json.NewEncoder(w)
|
|
enc.SetIndent("", " ")
|
|
enc.Encode(v) //nolint:errcheck
|
|
})
|
|
}
|