Skip to content

Commit

Permalink
Merge pull request #1107 from skrashevich/version-display-enhance
Browse files Browse the repository at this point in the history
feat(version): Enhancements to Version Display Functionality
  • Loading branch information
AlexxIT committed May 13, 2024
2 parents 152df3e + 874c07b commit 0075145
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
"strings"

"github.com/AlexxIT/go2rtc/pkg/shell"
Expand All @@ -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" {
Expand Down Expand Up @@ -89,6 +107,8 @@ func Init() {
Info["config_path"] = ConfigPath
}

Info["revision"] = revision

var cfg struct {
Mod map[string]string `yaml:"log"`
}
Expand All @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit 0075145

Please sign in to comment.