From 376ddc8e1b404f3f360e56077423073881f099d1 Mon Sep 17 00:00:00 2001 From: nicksherron Date: Sun, 9 Feb 2020 20:30:10 -0500 Subject: [PATCH] version command --- Dockerfile | 2 +- Makefile | 4 ++-- cmd/version.go | 50 ++++++++++++++++++++++++++++++++++------------ version/version.go | 21 ------------------- 4 files changed, 40 insertions(+), 37 deletions(-) delete mode 100644 version/version.go diff --git a/Dockerfile b/Dockerfile index ef4a150..e1bc562 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN apk update && \ apk add --no-cache gcc g++ musl-dev -RUN go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/version.GitCommit=${GIT_COMMIT} -X github.com/nicksherron/bashhub-server/version.BuildDate=${BUILD_DATE}' -o /go/bin/bashhub-server +RUN go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/cmd.GitCommit=${GIT_COMMIT} -X github.com/nicksherron/bashhub-server/cmd.BuildDate=${BUILD_DATE}' -o /go/bin/bashhub-server # --- diff --git a/Makefile b/Makefile index cbf3c55..4742e82 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ help: build: @echo "building ${BIN_NAME} ${VERSION}" @echo "GOPATH=${GOPATH}" - go build -ldflags "-X github.com/nicksherron/bashhub-server/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/version.BuildDate=${BUILD_DATE}" -o bin/${BIN_NAME} + go build -ldflags "-X github.com/nicksherron/bashhub-server/cmd.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/cmd.BuildDate=${BUILD_DATE}" -o bin/${BIN_NAME} get-deps: dep ensure @@ -40,7 +40,7 @@ docker-build: build-alpine: @echo "building ${BIN_NAME} ${VERSION}" @echo "GOPATH=${GOPATH}" - go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/version.BuildDate=${BUILD_DATE}' -o bin/${BIN_NAME} + go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/cmd.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/cmd.BuildDate=${BUILD_DATE}' -o bin/${BIN_NAME} package: @echo "building image ${BIN_NAME} ${VERSION} $(GIT_COMMIT)" diff --git a/cmd/version.go b/cmd/version.go index eb253a7..f766b3a 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,24 +1,48 @@ +/* + * Copyright © 2020 nicksherron + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package cmd import ( "fmt" - "github.com/nicksherron/bashhub-server/version" + "runtime" + "github.com/spf13/cobra" ) // versionCmd represents the version command -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the version number of generated code example", - Long: `All software has versions. This is generated code example`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Build Date:", version.BuildDate) - fmt.Println("Git Commit:", version.GitCommit) - fmt.Println("Version:", version.Version) - fmt.Println("Go Version:", version.GoVersion) - fmt.Println("OS / Arch:", version.OsArch) - }, -} +var ( + Build string + BuildDate string + Version string + OsArch = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH) + GoVersion = runtime.Version() + versionCmd = &cobra.Command{ + + Use: "version", + Short: "Print the version number and build info", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Build Date:", BuildDate) + fmt.Println("Git Commit:", Build) + fmt.Println("Version:", Version) + fmt.Println("Go Version:", GoVersion) + fmt.Println("OS / Arch:", OsArch) + }, + } +) func init() { rootCmd.AddCommand(versionCmd) diff --git a/version/version.go b/version/version.go deleted file mode 100644 index 0817b7b..0000000 --- a/version/version.go +++ /dev/null @@ -1,21 +0,0 @@ -package version - -import ( - "fmt" - "runtime" -) - -// GitCommit returns the git commit that was compiled. This will be filled in by the compiler. -var GitCommit string - -// Version returns the main version number that is being run at the moment. -const Version = "0.1.0" - -// BuildDate returns the date the binary was built -var BuildDate = "" - -// GoVersion returns the version of the go runtime used to compile the binary -var GoVersion = runtime.Version() - -// OsArch returns the os and arch used to build the binary -var OsArch = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)