Merge pull request #1107 from skrashevich/version-display-enhance

feat(version): Enhancements to Version Display Functionality
This commit is contained in:
Alex X
2024-05-13 12:45:22 +03:00
committed by GitHub

View File

@@ -8,6 +8,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"github.com/AlexxIT/go2rtc/pkg/shell"
@@ -23,24 +24,41 @@ var Info = map[string]any{
"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() {
var confs Config
var daemon bool
var version bool
flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple")
if runtime.GOOS != "windows" {
flag.BoolVar(&daemon, "daemon", false, "Run program in background")
}
flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
flag.Var(&confs, "config", "")
flag.Var(&confs, "c", "")
flag.BoolVar(&daemon, "daemon", false, "")
flag.BoolVar(&daemon, "d", false, "")
flag.BoolVar(&version, "version", false, "")
flag.BoolVar(&version, "v", false, "")
flag.Usage = func() { fmt.Print(usage) }
flag.Parse()
revision, vcsTime := readRevisionTime()
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)
}
if daemon {
if runtime.GOOS == "windows" {
fmt.Println("Daemon not supported on Windows")
os.Exit(1)
}
args := os.Args[1:]
for i, arg := range args {
if arg == "-daemon" {
@@ -89,6 +107,8 @@ func Init() {
Info["config_path"] = ConfigPath
}
Info["revision"] = revision
var cfg struct {
Mod map[string]string `yaml:"log"`
}
@@ -100,8 +120,8 @@ func Init() {
modules = cfg.Mod
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
log.Info().Str("version", Version).Str("platform", platform).Msg("go2rtc")
log.Debug().Str("version", runtime.Version()).Msg("build")
log.Info().Str("version", Version).Str("platform", platform).Str("revision", revision).Msg("go2rtc")
log.Debug().Str("version", runtime.Version()).Str("vcs.time", vcsTime).Msg("build")
if ConfigPath != "" {
log.Info().Str("path", ConfigPath).Msg("config")
@@ -148,3 +168,25 @@ func (c *Config) Set(value string) error {
}
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
}