diff --git a/build.sh b/build.sh index b21f4fdb..6381a372 100755 --- a/build.sh +++ b/build.sh @@ -102,16 +102,16 @@ set_build_parameters() { } build_backend() { - backend_extra_arguments="-X github.com/mayswind/ezbookkeeping/pkg/version.Version=$VERSION" - backend_extra_arguments="$backend_extra_arguments -X github.com/mayswind/ezbookkeeping/pkg/version.CommitHash=$COMMIT_HASH" + backend_build_extra_arguments="-X main.Version=$VERSION" + backend_build_extra_arguments="$backend_build_extra_arguments -X main.CommitHash=$COMMIT_HASH" 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 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 } diff --git a/ezbookkeeping.go b/ezbookkeeping.go index 65a973b0..6a8fa6b5 100644 --- a/ezbookkeeping.go +++ b/ezbookkeeping.go @@ -1,20 +1,33 @@ package main import ( + "fmt" "log" "os" + "strings" "github.com/urfave/cli/v2" "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() { app := &cli.App{ Name: "ezBookkeeping", Usage: "A lightweight personal bookkeeping app hosted by yourself.", - Version: version.GetFullVersion(), + Version: GetFullVersion(), Commands: []*cli.Command{ cmd.WebServer, cmd.Database, @@ -34,3 +47,32 @@ func main() { 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 +} diff --git a/pkg/version/version.go b/pkg/version/version.go deleted file mode 100644 index 7270f854..00000000 --- a/pkg/version/version.go +++ /dev/null @@ -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 -}