mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
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:
8
Makefile
8
Makefile
@@ -62,10 +62,10 @@ endif
|
|||||||
.PHONY: build-flags
|
.PHONY: build-flags
|
||||||
build-flags: pre-build
|
build-flags: pre-build
|
||||||
$(eval BUILD_LDFLAGS := $(LDFLAGS))
|
$(eval BUILD_LDFLAGS := $(LDFLAGS))
|
||||||
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/api.buildstamp=$(BUILD_DATE)')
|
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.buildstamp=$(BUILD_DATE)')
|
||||||
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/api.githash=$(GITHASH)')
|
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.githash=$(GITHASH)')
|
||||||
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/api.version=$(STASH_VERSION)')
|
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.version=$(STASH_VERSION)')
|
||||||
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/manager/config.officialBuild=$(OFFICIAL_BUILD)')
|
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.officialBuild=$(OFFICIAL_BUILD)')
|
||||||
$(eval BUILD_FLAGS := -v -tags "$(GO_BUILD_TAGS)" $(GO_BUILD_FLAGS) -ldflags "$(BUILD_LDFLAGS)")
|
$(eval BUILD_FLAGS := -v -tags "$(GO_BUILD_TAGS)" $(GO_BUILD_FLAGS) -ldflags "$(BUILD_LDFLAGS)")
|
||||||
|
|
||||||
# builds a dynamically-linked debug binary
|
# builds a dynamically-linked debug binary
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/sys/cpu"
|
"golang.org/x/sys/cpu"
|
||||||
|
|
||||||
|
"github.com/stashapp/stash/internal/build"
|
||||||
"github.com/stashapp/stash/pkg/logger"
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -170,7 +171,7 @@ func GetLatestRelease(ctx context.Context) (*LatestRelease, error) {
|
|||||||
wantedRelease := stashReleases()[platform]
|
wantedRelease := stashReleases()[platform]
|
||||||
|
|
||||||
url := apiReleases
|
url := apiReleases
|
||||||
if IsDevelop() {
|
if build.IsDevelop() {
|
||||||
// get the release tagged with the development tag
|
// get the release tagged with the development tag
|
||||||
url += "/tags/" + developmentTag
|
url += "/tags/" + developmentTag
|
||||||
} else {
|
} else {
|
||||||
@@ -213,7 +214,7 @@ func GetLatestRelease(ctx context.Context) (*LatestRelease, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, githash, _ := GetVersion()
|
_, githash, _ := build.Version()
|
||||||
shLength := len(githash)
|
shLength := len(githash)
|
||||||
if shLength == 0 {
|
if shLength == 0 {
|
||||||
shLength = defaultSHLength
|
shLength = defaultSHLength
|
||||||
@@ -273,7 +274,7 @@ func printLatestVersion(ctx context.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Couldn't retrieve latest version: %v", err)
|
logger.Errorf("Couldn't retrieve latest version: %v", err)
|
||||||
} else {
|
} else {
|
||||||
_, githash, _ = GetVersion()
|
_, githash, _ := build.Version()
|
||||||
switch {
|
switch {
|
||||||
case githash == "":
|
case githash == "":
|
||||||
logger.Infof("Latest version: %s (%s)", latestRelease.Version, latestRelease.ShortHash)
|
logger.Infof("Latest version: %s (%s)", latestRelease.Version, latestRelease.ShortHash)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/stashapp/stash/internal/build"
|
||||||
"github.com/stashapp/stash/internal/manager"
|
"github.com/stashapp/stash/internal/manager"
|
||||||
"github.com/stashapp/stash/pkg/logger"
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
"github.com/stashapp/stash/pkg/models"
|
"github.com/stashapp/stash/pkg/models"
|
||||||
@@ -188,7 +189,7 @@ func (r *queryResolver) Stats(ctx context.Context) (*StatsResultType, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *queryResolver) Version(ctx context.Context) (*Version, error) {
|
func (r *queryResolver) Version(ctx context.Context) (*Version, error) {
|
||||||
version, hash, buildtime := GetVersion()
|
version, hash, buildtime := build.Version()
|
||||||
|
|
||||||
return &Version{
|
return &Version{
|
||||||
Version: &version,
|
Version: &version,
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -30,6 +29,7 @@ import (
|
|||||||
"github.com/go-chi/cors"
|
"github.com/go-chi/cors"
|
||||||
"github.com/go-chi/httplog"
|
"github.com/go-chi/httplog"
|
||||||
"github.com/stashapp/stash/internal/api/loaders"
|
"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"
|
||||||
"github.com/stashapp/stash/internal/manager/config"
|
"github.com/stashapp/stash/internal/manager/config"
|
||||||
"github.com/stashapp/stash/pkg/fsutil"
|
"github.com/stashapp/stash/pkg/fsutil"
|
||||||
@@ -46,10 +46,6 @@ const (
|
|||||||
playgroundEndpoint = "/playground"
|
playgroundEndpoint = "/playground"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version string
|
|
||||||
var buildstamp string
|
|
||||||
var githash string
|
|
||||||
|
|
||||||
var uiBox = ui.UIBox
|
var uiBox = ui.UIBox
|
||||||
var loginUIBox = ui.LoginUIBox
|
var loginUIBox = ui.LoginUIBox
|
||||||
|
|
||||||
@@ -270,7 +266,7 @@ func Start() error {
|
|||||||
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
||||||
}
|
}
|
||||||
|
|
||||||
printVersion()
|
logger.Infof("stash version: %s\n", build.VersionString())
|
||||||
go printLatestVersion(context.TODO())
|
go printLatestVersion(context.TODO())
|
||||||
logger.Infof("stash is listening on " + address)
|
logger.Infof("stash is listening on " + address)
|
||||||
if tlsConfig != nil {
|
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) {
|
func makeTLSConfig(c *config.Instance) (*tls.Config, error) {
|
||||||
c.InitTLS()
|
c.InitTLS()
|
||||||
certFile, keyFile := c.GetTLSFiles()
|
certFile, keyFile := c.GetTLSFiles()
|
||||||
|
|||||||
57
internal/build/version.go
Normal file
57
internal/build/version.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var version string
|
||||||
|
var buildstamp string
|
||||||
|
var githash string
|
||||||
|
var officialBuild string
|
||||||
|
|
||||||
|
func Version() (string, string, string) {
|
||||||
|
return version, githash, buildstamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func VersionString() string {
|
||||||
|
var versionString string
|
||||||
|
switch {
|
||||||
|
case version != "":
|
||||||
|
if githash != "" && !IsDevelop() {
|
||||||
|
versionString = version + " (" + githash + ")"
|
||||||
|
} else {
|
||||||
|
versionString = version
|
||||||
|
}
|
||||||
|
case githash != "":
|
||||||
|
versionString = githash
|
||||||
|
default:
|
||||||
|
versionString = "unknown"
|
||||||
|
}
|
||||||
|
if IsOfficial() {
|
||||||
|
versionString += " - Official Build"
|
||||||
|
} else {
|
||||||
|
versionString += " - Unofficial Build"
|
||||||
|
}
|
||||||
|
if buildstamp != "" {
|
||||||
|
versionString += " - " + buildstamp
|
||||||
|
}
|
||||||
|
return versionString
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsOfficial() bool {
|
||||||
|
return officialBuild == "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/browser"
|
"github.com/pkg/browser"
|
||||||
|
"github.com/stashapp/stash/internal/build"
|
||||||
"github.com/stashapp/stash/internal/manager/config"
|
"github.com/stashapp/stash/internal/manager/config"
|
||||||
"github.com/stashapp/stash/pkg/fsutil"
|
"github.com/stashapp/stash/pkg/fsutil"
|
||||||
"github.com/stashapp/stash/pkg/logger"
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
@@ -104,7 +105,7 @@ func writeStashIcon(faviconProvider FaviconProvider) {
|
|||||||
func IsAllowedAutoUpdate() bool {
|
func IsAllowedAutoUpdate() bool {
|
||||||
|
|
||||||
// Only try to update if downloaded from official sources
|
// Only try to update if downloaded from official sources
|
||||||
if !config.IsOfficialBuild() {
|
if !build.IsOfficial() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ import (
|
|||||||
"github.com/stashapp/stash/pkg/models/paths"
|
"github.com/stashapp/stash/pkg/models/paths"
|
||||||
)
|
)
|
||||||
|
|
||||||
var officialBuild string
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Stash = "stash"
|
Stash = "stash"
|
||||||
Cache = "cache"
|
Cache = "cache"
|
||||||
@@ -275,10 +273,6 @@ func (s *StashBoxError) Error() string {
|
|||||||
return "Stash-box: " + s.msg
|
return "Stash-box: " + s.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsOfficialBuild() bool {
|
|
||||||
return officialBuild == "true"
|
|
||||||
}
|
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
// main instance - backed by config file
|
// main instance - backed by config file
|
||||||
main *viper.Viper
|
main *viper.Viper
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
"github.com/stashapp/stash/internal/build"
|
||||||
"github.com/stashapp/stash/pkg/fsutil"
|
"github.com/stashapp/stash/pkg/fsutil"
|
||||||
"github.com/stashapp/stash/pkg/logger"
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
)
|
)
|
||||||
@@ -25,6 +26,7 @@ type flagStruct struct {
|
|||||||
cpuProfilePath string
|
cpuProfilePath string
|
||||||
nobrowser bool
|
nobrowser bool
|
||||||
helpFlag bool
|
helpFlag bool
|
||||||
|
versionFlag bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetInstance() *Instance {
|
func GetInstance() *Instance {
|
||||||
@@ -47,6 +49,11 @@ func Initialize() (*Instance, error) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flags.versionFlag {
|
||||||
|
fmt.Printf(build.VersionString() + "\n")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
overrides := makeOverrideConfig()
|
overrides := makeOverrideConfig()
|
||||||
|
|
||||||
_ = GetInstance()
|
_ = GetInstance()
|
||||||
@@ -134,6 +141,7 @@ func initFlags() flagStruct {
|
|||||||
pflag.StringVar(&flags.cpuProfilePath, "cpuprofile", "", "write cpu profile to file")
|
pflag.StringVar(&flags.cpuProfilePath, "cpuprofile", "", "write cpu profile to file")
|
||||||
pflag.BoolVar(&flags.nobrowser, "nobrowser", false, "Don't open a browser window after launch")
|
pflag.BoolVar(&flags.nobrowser, "nobrowser", false, "Don't open a browser window after launch")
|
||||||
pflag.BoolVarP(&flags.helpFlag, "help", "h", false, "show this help text and exit")
|
pflag.BoolVarP(&flags.helpFlag, "help", "h", false, "show this help text and exit")
|
||||||
|
pflag.BoolVarP(&flags.versionFlag, "version", "v", false, "show version number and exit")
|
||||||
|
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user