Merge pull request #1107 from skrashevich/version-display-enhance
feat(version): Enhancements to Version Display Functionality
This commit is contained in:
+50
-8
@@ -8,6 +8,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/shell"
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
@@ -23,24 +24,41 @@ var Info = map[string]any{
|
|||||||
"version": Version,
|
"version": Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const usage = `Usage of go2rtc:
|
||||||
|
|
||||||
|
-c, --config Path to config file or config string as YAML or JSON, support multiple
|
||||||
|
-d, --daemon Run in background
|
||||||
|
-v, --version Print version and exit
|
||||||
|
`
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
var confs Config
|
var confs Config
|
||||||
var daemon bool
|
var daemon bool
|
||||||
var version bool
|
var version bool
|
||||||
|
|
||||||
flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple")
|
flag.Var(&confs, "config", "")
|
||||||
if runtime.GOOS != "windows" {
|
flag.Var(&confs, "c", "")
|
||||||
flag.BoolVar(&daemon, "daemon", false, "Run program in background")
|
flag.BoolVar(&daemon, "daemon", false, "")
|
||||||
}
|
flag.BoolVar(&daemon, "d", false, "")
|
||||||
flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
|
flag.BoolVar(&version, "version", false, "")
|
||||||
|
flag.BoolVar(&version, "v", false, "")
|
||||||
|
|
||||||
|
flag.Usage = func() { fmt.Print(usage) }
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
revision, vcsTime := readRevisionTime()
|
||||||
|
|
||||||
if version {
|
if version {
|
||||||
fmt.Printf("go2rtc version %s %s/%s\n", Version, runtime.GOOS, runtime.GOARCH)
|
fmt.Printf("go2rtc version %s (%s) %s/%s\n", Version, revision, runtime.GOOS, runtime.GOARCH)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if daemon {
|
if daemon {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
fmt.Println("Daemon not supported on Windows")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
if arg == "-daemon" {
|
if arg == "-daemon" {
|
||||||
@@ -89,6 +107,8 @@ func Init() {
|
|||||||
Info["config_path"] = ConfigPath
|
Info["config_path"] = ConfigPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Info["revision"] = revision
|
||||||
|
|
||||||
var cfg struct {
|
var cfg struct {
|
||||||
Mod map[string]string `yaml:"log"`
|
Mod map[string]string `yaml:"log"`
|
||||||
}
|
}
|
||||||
@@ -100,8 +120,8 @@ func Init() {
|
|||||||
modules = cfg.Mod
|
modules = cfg.Mod
|
||||||
|
|
||||||
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
||||||
log.Info().Str("version", Version).Str("platform", platform).Msg("go2rtc")
|
log.Info().Str("version", Version).Str("platform", platform).Str("revision", revision).Msg("go2rtc")
|
||||||
log.Debug().Str("version", runtime.Version()).Msg("build")
|
log.Debug().Str("version", runtime.Version()).Str("vcs.time", vcsTime).Msg("build")
|
||||||
|
|
||||||
if ConfigPath != "" {
|
if ConfigPath != "" {
|
||||||
log.Info().Str("path", ConfigPath).Msg("config")
|
log.Info().Str("path", ConfigPath).Msg("config")
|
||||||
@@ -148,3 +168,25 @@ func (c *Config) Set(value string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var configs [][]byte
|
var configs [][]byte
|
||||||
|
|
||||||
|
func readRevisionTime() (revision, vcsTime string) {
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
for _, setting := range info.Settings {
|
||||||
|
switch setting.Key {
|
||||||
|
case "vcs.revision":
|
||||||
|
if len(setting.Value) > 7 {
|
||||||
|
revision = setting.Value[:7]
|
||||||
|
} else {
|
||||||
|
revision = setting.Value
|
||||||
|
}
|
||||||
|
case "vcs.time":
|
||||||
|
vcsTime = setting.Value
|
||||||
|
case "vcs.modified":
|
||||||
|
if setting.Value == "true" {
|
||||||
|
revision = "mod." + revision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user