Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(version): Enhancements to Version Display Functionality #1107

Merged
merged 4 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}