move version.go to main package

This commit is contained in:
MaysWind
2021-06-09 00:00:17 +08:00
parent 0d66f8c943
commit fac4abc194
3 changed files with 48 additions and 54 deletions
+4 -4
View File
@@ -102,16 +102,16 @@ set_build_parameters() {
} }
build_backend() { build_backend() {
backend_extra_arguments="-X github.com/mayswind/ezbookkeeping/pkg/version.Version=$VERSION" backend_build_extra_arguments="-X main.Version=$VERSION"
backend_extra_arguments="$backend_extra_arguments -X github.com/mayswind/ezbookkeeping/pkg/version.CommitHash=$COMMIT_HASH" backend_build_extra_arguments="$backend_build_extra_arguments -X main.CommitHash=$COMMIT_HASH"
if [ "$RELEASE" = "0" ]; then if [ "$RELEASE" = "0" ]; then
backend_extra_arguments="$backend_extra_arguments -X github.com/mayswind/ezbookkeeping/pkg/version.BuildUnixTime=$BUILD_UNIXTIME" backend_build_extra_arguments="$backend_build_extra_arguments -X main.BuildUnixTime=$BUILD_UNIXTIME"
fi fi
echo "Building backend binary file ($RELEASE_TYPE)..." echo "Building backend binary file ($RELEASE_TYPE)..."
CGO_ENABLED=1 go build -a -v -trimpath -ldflags "-w -s -linkmode external -extldflags '-static' $backend_extra_arguments" -o ezbookkeeping ezbookkeeping.go CGO_ENABLED=1 go build -a -v -trimpath -ldflags "-w -s -linkmode external -extldflags '-static' $backend_build_extra_arguments" -o ezbookkeeping ezbookkeeping.go
chmod +x ezbookkeeping chmod +x ezbookkeeping
} }
+44 -2
View File
@@ -1,20 +1,33 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/mayswind/ezbookkeeping/cmd" "github.com/mayswind/ezbookkeeping/cmd"
"github.com/mayswind/ezbookkeeping/pkg/version" "github.com/mayswind/ezbookkeeping/pkg/utils"
)
var (
// Version holds the version of this execution program
Version string
// CommitHash holds the git commit hash of this execution program's source code
CommitHash string
// BuildUnixTime holds the time when starting building this execution program
BuildUnixTime string
) )
func main() { func main() {
app := &cli.App{ app := &cli.App{
Name: "ezBookkeeping", Name: "ezBookkeeping",
Usage: "A lightweight personal bookkeeping app hosted by yourself.", Usage: "A lightweight personal bookkeeping app hosted by yourself.",
Version: version.GetFullVersion(), Version: GetFullVersion(),
Commands: []*cli.Command{ Commands: []*cli.Command{
cmd.WebServer, cmd.WebServer,
cmd.Database, cmd.Database,
@@ -34,3 +47,32 @@ func main() {
log.Fatalf("Failed to run ezBookkeeping with %s: %v", os.Args, err) log.Fatalf("Failed to run ezBookkeeping with %s: %v", os.Args, err)
} }
} }
// GetFullVersion returns the full version
func GetFullVersion() string {
fullVersion := "Local Build"
if Version != "" {
fullVersion = Version
}
additionalInfos := make([]string, 0, 2)
if CommitHash != "" {
additionalInfos = append(additionalInfos, "commit "+CommitHash)
}
if BuildUnixTime != "" {
unixTime, err := utils.StringToInt64(BuildUnixTime)
if unixTime > 0 && err == nil {
additionalInfos = append(additionalInfos, "build time "+utils.FormatUnixTimeToLongDateTimeInServerTimezone(unixTime))
}
}
if len(additionalInfos) > 0 {
fullVersion = fmt.Sprintf("%s (%s)", fullVersion, strings.Join(additionalInfos, ", "))
}
return fullVersion
}
-48
View File
@@ -1,48 +0,0 @@
package version
import (
"fmt"
"strings"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)
var (
// Version holds the version of this execution program
Version string
// CommitHash holds the git commit hash of this execution program's source code
CommitHash string
// BuildUnixTime holds the time when starting building this execution program
BuildUnixTime string
)
// GetFullVersion returns the full version
func GetFullVersion() string {
fullVersion := "Local Build"
if Version != "" {
fullVersion = Version
}
additionalInfos := make([]string, 0, 2)
if CommitHash != "" {
additionalInfos = append(additionalInfos, "commit "+CommitHash)
}
if BuildUnixTime != "" {
unixTime, err := utils.StringToInt64(BuildUnixTime)
if unixTime > 0 && err == nil {
additionalInfos = append(additionalInfos, "build time "+utils.FormatUnixTimeToLongDateTimeInServerTimezone(unixTime))
}
}
if len(additionalInfos) > 0 {
fullVersion = fmt.Sprintf("%s (%s)", fullVersion, strings.Join(additionalInfos, ", "))
}
return fullVersion
}