From dd7ea2657aa90990aada8df03ee40096ed00130d Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sun, 12 May 2024 21:24:06 +0300 Subject: [PATCH] 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 a18896c9..c94df69d 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.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() 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() - } - } - } - } - - fmt.Printf("go2rtc version %s%s: %s %s/%s\n", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH) + 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) +}