Skip to content

Commit

Permalink
feat(app): enhance CLI with shorthand flags and dynamic versioning
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
skrashevich committed May 12, 2024
1 parent eaba451 commit dd7ea26
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

0 comments on commit dd7ea26

Please sign in to comment.