Add -v/--version flag to print version string (#3883)

* Add `-v/--version` flag to print version string

- Created a new flag `-v/--version` in the command-line interface to display the version number and exit.
- Moved all version-related functions inside the config package to the new file `manager/config/version.go` to avoid circular dependencies.
- Added a new `GetVersionString()` function to generate a formatted version string.
- Updated references to the moved version functions.
- Updated references in the `Makefile`.

* Move version embeds to build package

* Remove githash var

---------

Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
Csaba Maulis
2023-07-11 13:54:42 +08:00
committed by GitHub
parent 969af2ab69
commit 0c0ba19a23
8 changed files with 79 additions and 64 deletions

View File

@@ -11,7 +11,6 @@ import (
"net/http"
"os"
"path"
"regexp"
"runtime/debug"
"strconv"
"strings"
@@ -30,6 +29,7 @@ import (
"github.com/go-chi/cors"
"github.com/go-chi/httplog"
"github.com/stashapp/stash/internal/api/loaders"
"github.com/stashapp/stash/internal/build"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/fsutil"
@@ -46,10 +46,6 @@ const (
playgroundEndpoint = "/playground"
)
var version string
var buildstamp string
var githash string
var uiBox = ui.UIBox
var loginUIBox = ui.LoginUIBox
@@ -270,7 +266,7 @@ func Start() error {
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
printVersion()
logger.Infof("stash version: %s\n", build.VersionString())
go printLatestVersion(context.TODO())
logger.Infof("stash is listening on " + address)
if tlsConfig != nil {
@@ -390,49 +386,6 @@ func customLocalesHandler(c *config.Instance) func(w http.ResponseWriter, r *htt
}
}
func printVersion() {
var versionString string
switch {
case version != "":
if githash != "" && !IsDevelop() {
versionString = version + " (" + githash + ")"
} else {
versionString = version
}
case githash != "":
versionString = githash
default:
versionString = "unknown"
}
if config.IsOfficialBuild() {
versionString += " - Official Build"
} else {
versionString += " - Unofficial Build"
}
if buildstamp != "" {
versionString += " - " + buildstamp
}
logger.Infof("stash version: %s\n", versionString)
}
func GetVersion() (string, string, string) {
return version, githash, buildstamp
}
func IsDevelop() bool {
if githash == "" {
return false
}
// if the version is suffixed with -x-xxxx, then we are running a development build
develop := false
re := regexp.MustCompile(`-\d+-g\w+$`)
if re.MatchString(version) {
develop = true
}
return develop
}
func makeTLSConfig(c *config.Instance) (*tls.Config, error) {
c.InitTLS()
certFile, keyFile := c.GetTLSFiles()