Code refactoring for #1107
This commit is contained in:
+34
-45
@@ -10,7 +10,6 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/shell"
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/yaml"
|
"github.com/AlexxIT/go2rtc/pkg/yaml"
|
||||||
@@ -25,51 +24,41 @@ var Info = map[string]any{
|
|||||||
"version": Version,
|
"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() {
|
func Init() {
|
||||||
var confs Config
|
var confs Config
|
||||||
var daemon bool
|
var daemon bool
|
||||||
var version 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", "")
|
flag.Var(&confs, "c", "")
|
||||||
if runtime.GOOS != "windows" {
|
flag.BoolVar(&daemon, "daemon", false, "")
|
||||||
flag.BoolVar(&daemon, "daemon", false, "Run program in background")
|
flag.BoolVar(&daemon, "d", false, "")
|
||||||
}
|
flag.BoolVar(&version, "version", false, "")
|
||||||
flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
|
|
||||||
flag.BoolVar(&version, "v", false, "")
|
flag.BoolVar(&version, "v", false, "")
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() { fmt.Print(usage) }
|
||||||
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()
|
flag.Parse()
|
||||||
|
|
||||||
|
revision, vcsTime := readRevisionTime()
|
||||||
|
|
||||||
if version {
|
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)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if daemon {
|
if daemon {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
fmt.Println("Daemon not supported on Windows")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
if arg == "-daemon" {
|
if arg == "-daemon" {
|
||||||
@@ -118,6 +107,8 @@ func Init() {
|
|||||||
Info["config_path"] = ConfigPath
|
Info["config_path"] = ConfigPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Info["revision"] = revision
|
||||||
|
|
||||||
var cfg struct {
|
var cfg struct {
|
||||||
Mod map[string]string `yaml:"log"`
|
Mod map[string]string `yaml:"log"`
|
||||||
}
|
}
|
||||||
@@ -129,8 +120,8 @@ func Init() {
|
|||||||
modules = cfg.Mod
|
modules = cfg.Mod
|
||||||
|
|
||||||
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
||||||
log.Info().Str("version", Version).Str("platform", platform).Msg("go2rtc")
|
log.Info().Str("version", Version).Str("platform", platform).Str("revision", revision).Msg("go2rtc")
|
||||||
log.Debug().Str("version", runtime.Version()).Msg("build")
|
log.Debug().Str("version", runtime.Version()).Str("vcs.time", vcsTime).Msg("build")
|
||||||
|
|
||||||
if ConfigPath != "" {
|
if ConfigPath != "" {
|
||||||
log.Info().Str("path", ConfigPath).Msg("config")
|
log.Info().Str("path", ConfigPath).Msg("config")
|
||||||
@@ -178,26 +169,24 @@ func (c *Config) Set(value string) error {
|
|||||||
|
|
||||||
var configs [][]byte
|
var configs [][]byte
|
||||||
|
|
||||||
func GetVersionString() string {
|
func readRevisionTime() (revision, vcsTime string) {
|
||||||
var vcsRevision string
|
|
||||||
vcsTime := time.Now()
|
|
||||||
|
|
||||||
if info, ok := debug.ReadBuildInfo(); ok {
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
for _, setting := range info.Settings {
|
for _, setting := range info.Settings {
|
||||||
switch setting.Key {
|
switch setting.Key {
|
||||||
case "vcs.revision":
|
case "vcs.revision":
|
||||||
vcsRevision = setting.Value
|
if len(setting.Value) > 7 {
|
||||||
if len(vcsRevision) > 7 {
|
revision = setting.Value[:7]
|
||||||
vcsRevision = vcsRevision[:7]
|
} else {
|
||||||
|
revision = setting.Value
|
||||||
}
|
}
|
||||||
vcsRevision = "(" + vcsRevision + ")"
|
|
||||||
case "vcs.time":
|
case "vcs.time":
|
||||||
if parsedTime, err := time.Parse(time.RFC3339, setting.Value); err == nil {
|
vcsTime = setting.Value
|
||||||
vcsTime = parsedTime.Local()
|
case "vcs.modified":
|
||||||
|
if setting.Value == "true" {
|
||||||
|
revision = "mod." + revision
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return fmt.Sprintf("%s%s: %s %s/%s", Version, vcsRevision, vcsTime.String(), runtime.GOOS, runtime.GOARCH)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user