From 9b4d4c618010150ee038ef762a86afc7f02df5ab Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Mon, 20 Mar 2017 00:31:21 +0000 Subject: [PATCH] add version command Closes #72 --- .gitignore | 3 +- main.go | 5 ++++ release.sh | 27 ++++++++++++++++++ version/version.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100755 release.sh create mode 100644 version/version.go diff --git a/.gitignore b/.gitignore index 628b68fc..fd8ceeec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ vendor/ -SCEPCACert.pem -mdm.db +build/ diff --git a/main.go b/main.go index 2fbb6818..c284ff61 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "strings" "text/tabwriter" "time" + + "github.com/micromdm/micromdm/version" ) func init() { @@ -21,6 +23,9 @@ func main() { } var run func([]string) error switch strings.ToLower(os.Args[1]) { + case "version", "-version": + version.Print() + return case "serve": run = serve case "dep-token": diff --git a/release.sh b/release.sh new file mode 100755 index 00000000..906574ef --- /dev/null +++ b/release.sh @@ -0,0 +1,27 @@ +VERSION="$(git describe --tags --always --dirty)" +NAME=micromdm +USER=$(whoami) +BRANCH=$(git rev-parse --abbrev-ref HEAD) +NOW=$(date -u +"%Y-%m-%dT%H:%M:%SZ") +REVISION=$(git rev-parse HEAD) +GOVERSION=$(go version | awk '{print $3}') + +echo "Building $NAME version $VERSION" + +mkdir -p build + +build() { + echo -n "=> $1-$2: " + GOOS=$1 GOARCH=$2 CGO_ENABLED=0 go build -o build/$NAME-$1-$2 -ldflags "\ + -X github.com/micromdm/micromdm/version.version=${VERSION}\ + -X github.com/micromdm/micromdm/version.branch=${BRANCH}\ + -X github.com/micromdm/micromdm/version.buildUser=${USER}\ + -X github.com/micromdm/micromdm/version.buildDate=${NOW}\ + -X github.com/micromdm/micromdm/version.revision=${REVISION}\ + -X github.com/micromdm/micromdm/version.goVersion=${GOVERSION}\ + " ./*.go + du -h build/$NAME-$1-$2 +} + +build "darwin" "amd64" +build "linux" "amd64" diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..9a875469 --- /dev/null +++ b/version/version.go @@ -0,0 +1,69 @@ +// Package version provides utilities for displaying version information. +package version + +import ( + "encoding/json" + "fmt" + "net/http" +) + +// go build -ldflags "-X github.com/micromdm/micromdm/version.version=1.0.0" +var ( + version = "unknown" + branch = "unknown" + revision = "unknown" + goVersion = "unknown" + buildDate = "unknown" + buildUser = "unknown" + appName = "micromdm" +) + +// Info holds version and build info about the app. +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 struct 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 app name and version string. +func Print() { + v := Version() + fmt.Printf("%s version %s\n", appName, v.Version) +} + +// PrintFull outputs the app 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 provides an HTTP Handler which returns JSON formatted version info. +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) + }) +}