From b7c11db60450699975e8a27dc85d98a6d3efa282 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sun, 12 May 2024 06:34:28 +0300 Subject: [PATCH 1/4] feat(version): enhance version command output with VCS revision and timestamp --- internal/app/app.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/app/app.go b/internal/app/app.go index bc44a16f3..6f3ccf72d 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -8,7 +8,9 @@ import ( "os/exec" "path/filepath" "runtime" + "runtime/debug" "strings" + "time" "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/AlexxIT/go2rtc/pkg/yaml" @@ -36,7 +38,25 @@ func Init() { flag.Parse() if version { - fmt.Printf("go2rtc version %s %s/%s\n", Version, runtime.GOOS, runtime.GOARCH) + vcsRevision := "" + vcsTime := time.Now().Local() + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + if len(setting.Value) > 7 { + vcsRevision = setting.Value[:7] + } else { + vcsRevision = setting.Value + } + vcsRevision = "(" + vcsRevision + ")" + } + if setting.Key == "vcs.time" { + vcsTime, _ = time.Parse(time.RFC3339, setting.Value) + vcsTime = vcsTime.Local() + } + } + } + fmt.Printf("go2rtc version %s%s: %s %s/%s\n", Version, vcsRevision, vcsTime.Local().String(), runtime.GOOS, runtime.GOARCH) os.Exit(0) } From eaba451a479deb8613ba9b365073bb945c626043 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sun, 12 May 2024 06:46:45 +0300 Subject: [PATCH 2/4] refactor(app): streamline version info retrieval and formatting --- internal/app/app.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 6f3ccf72d..a18896c9e 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -38,25 +38,27 @@ func Init() { flag.Parse() if version { - vcsRevision := "" - vcsTime := time.Now().Local() + var vcsRevision string + vcsTime := time.Now() + if info, ok := debug.ReadBuildInfo(); ok { for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - if len(setting.Value) > 7 { - vcsRevision = setting.Value[:7] - } else { - vcsRevision = setting.Value + switch setting.Key { + case "vcs.revision": + vcsRevision = setting.Value + if len(vcsRevision) > 7 { + vcsRevision = vcsRevision[:7] } vcsRevision = "(" + vcsRevision + ")" - } - if setting.Key == "vcs.time" { - vcsTime, _ = time.Parse(time.RFC3339, setting.Value) - vcsTime = vcsTime.Local() + case "vcs.time": + if parsedTime, err := time.Parse(time.RFC3339, setting.Value); err == nil { + vcsTime = parsedTime.Local() + } } } } - fmt.Printf("go2rtc version %s%s: %s %s/%s\n", Version, vcsRevision, vcsTime.Local().String(), runtime.GOOS, runtime.GOARCH) + + fmt.Printf("go2rtc version %s%s: %s %s/%s\n", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH) os.Exit(0) } From dd7ea2657aa90990aada8df03ee40096ed00130d Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sun, 12 May 2024 21:24:06 +0300 Subject: [PATCH 3/4] feat(app): enhance CLI with shorthand flags and dynamic versioning - Added shorthand flag support for `config`, `daemon`, and `version` - Implemented dynamic version string generation using build info - Updated flag usage output to include shorthand options and help command --- internal/app/app.go | 73 ++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index a18896c9e..c94df69d4 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -31,34 +31,41 @@ func Init() { var version bool flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple") + flag.Var(&confs, "c", "") 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.Parse() - - if version { - var vcsRevision string - vcsTime := time.Now() - - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - switch setting.Key { - case "vcs.revision": - vcsRevision = setting.Value - if len(vcsRevision) > 7 { - vcsRevision = vcsRevision[:7] - } - vcsRevision = "(" + vcsRevision + ")" - case "vcs.time": - if parsedTime, err := time.Parse(time.RFC3339, setting.Value); err == nil { - vcsTime = parsedTime.Local() - } + flag.BoolVar(&version, "v", false, "") + + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of go2rtc\nversion %s\n\n", GetVersionString()) + flag.VisitAll(func(f *flag.Flag) { + pname := "" + if f.Usage != "" { + switch f.Name { + case "config": + pname = "-c --config" + break + case "daemon": + pname = "-d --daemon" + break + case "version": + pname = "-v --version" + break + default: + pname = "-" + f.Name } + fmt.Fprintf(os.Stderr, "\t%s\n\t\t%s (default %q)\n", pname, f.Usage, f.DefValue) } - } + }) + fmt.Fprintf(os.Stderr, "\t%s\n\t\t%s\n", "-h --help", "Print this help") + } + + flag.Parse() - fmt.Printf("go2rtc version %s%s: %s %s/%s\n", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH) + if version { + fmt.Println("go2rtc version " + GetVersionString()) os.Exit(0) } @@ -170,3 +177,27 @@ func (c *Config) Set(value string) error { } var configs [][]byte + +func GetVersionString() string { + var vcsRevision string + vcsTime := time.Now() + + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + vcsRevision = setting.Value + if len(vcsRevision) > 7 { + vcsRevision = vcsRevision[:7] + } + vcsRevision = "(" + vcsRevision + ")" + case "vcs.time": + if parsedTime, err := time.Parse(time.RFC3339, setting.Value); err == nil { + vcsTime = parsedTime.Local() + } + } + } + } + + return fmt.Sprintf("%s%s: %s %s/%s", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH) +} From 874c07b88773f987f9eb5701b79bb517e9787ce9 Mon Sep 17 00:00:00 2001 From: Alex X Date: Mon, 13 May 2024 12:42:55 +0300 Subject: [PATCH 4/4] Code refactoring for #1107 --- internal/app/app.go | 79 +++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index c94df69d4..6afaebb4d 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -10,7 +10,6 @@ import ( "runtime" "runtime/debug" "strings" - "time" "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/AlexxIT/go2rtc/pkg/yaml" @@ -25,51 +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") + flag.Var(&confs, "config", "") flag.Var(&confs, "c", "") - 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.BoolVar(&daemon, "daemon", false, "") + flag.BoolVar(&daemon, "d", false, "") + flag.BoolVar(&version, "version", false, "") flag.BoolVar(&version, "v", false, "") - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage of go2rtc\nversion %s\n\n", GetVersionString()) - flag.VisitAll(func(f *flag.Flag) { - pname := "" - if f.Usage != "" { - switch f.Name { - case "config": - pname = "-c --config" - break - case "daemon": - pname = "-d --daemon" - break - case "version": - pname = "-v --version" - break - default: - pname = "-" + f.Name - } - fmt.Fprintf(os.Stderr, "\t%s\n\t\t%s (default %q)\n", pname, f.Usage, f.DefValue) - } - }) - fmt.Fprintf(os.Stderr, "\t%s\n\t\t%s\n", "-h --help", "Print this help") - } - + flag.Usage = func() { fmt.Print(usage) } flag.Parse() + revision, vcsTime := readRevisionTime() + if version { - fmt.Println("go2rtc version " + GetVersionString()) + 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" { @@ -118,6 +107,8 @@ func Init() { Info["config_path"] = ConfigPath } + Info["revision"] = revision + var cfg struct { Mod map[string]string `yaml:"log"` } @@ -129,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") @@ -178,26 +169,24 @@ func (c *Config) Set(value string) error { var configs [][]byte -func GetVersionString() string { - var vcsRevision string - vcsTime := time.Now() - +func readRevisionTime() (revision, vcsTime string) { if info, ok := debug.ReadBuildInfo(); ok { for _, setting := range info.Settings { switch setting.Key { case "vcs.revision": - vcsRevision = setting.Value - if len(vcsRevision) > 7 { - vcsRevision = vcsRevision[:7] + if len(setting.Value) > 7 { + revision = setting.Value[:7] + } else { + revision = setting.Value } - vcsRevision = "(" + vcsRevision + ")" case "vcs.time": - if parsedTime, err := time.Parse(time.RFC3339, setting.Value); err == nil { - vcsTime = parsedTime.Local() + vcsTime = setting.Value + case "vcs.modified": + if setting.Value == "true" { + revision = "mod." + revision } } } } - - return fmt.Sprintf("%s%s: %s %s/%s", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH) + return }