version command

This commit is contained in:
nicksherron 2020-02-09 20:30:10 -05:00
parent 1b78ce5746
commit 376ddc8e1b
4 changed files with 40 additions and 37 deletions

View file

@ -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
# ---

View file

@ -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)"

View file

@ -1,24 +1,48 @@
/*
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
*
* 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)

View file

@ -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)