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
This commit is contained in:
Sergey Krashevich
2024-05-12 21:24:06 +03:00
parent eaba451a47
commit dd7ea2657a
+52 -21
View File
@@ -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)
}