3afe8d7c1d
This commit fixes the issue where the '-daemon' argument was not being properly handled when re-executing the program in daemon mode. The loop removes the '-daemon' flag from the arguments slice before the program is re-run in the background, ensuring that subsequent executions do not attempt to enter daemon mode again. The change will prevent potential errors or unexpected behavior due to the presence of the '-daemon' argument in recursive calls, making the daemon mode feature more robust and reliable.
144 lines
2.6 KiB
Go
144 lines
2.6 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
|
"github.com/AlexxIT/go2rtc/pkg/yaml"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
var Version = "1.8.5"
|
|
var UserAgent = "go2rtc/" + Version
|
|
|
|
var ConfigPath string
|
|
var Info = map[string]any{
|
|
"version": Version,
|
|
}
|
|
|
|
func Init() {
|
|
var confs Config
|
|
var version bool
|
|
var daemon bool
|
|
|
|
flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple")
|
|
if runtime.GOOS != "windows" {
|
|
flag.BoolVar(&daemon, "daemon", false, "Run program in background")
|
|
} else {
|
|
daemon = false
|
|
}
|
|
flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
|
|
flag.Parse()
|
|
|
|
if version {
|
|
fmt.Println("Current version: ", Version)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if daemon {
|
|
for i, arg := range os.Args[1:] {
|
|
if arg == "-daemon" {
|
|
os.Args[i+1] = ""
|
|
}
|
|
}
|
|
// Re-run the program in background and exit
|
|
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
|
cmd.Start()
|
|
fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if confs == nil {
|
|
confs = []string{"go2rtc.yaml"}
|
|
}
|
|
|
|
for _, conf := range confs {
|
|
if conf[0] != '{' {
|
|
// config as file
|
|
if ConfigPath == "" {
|
|
ConfigPath = conf
|
|
}
|
|
|
|
data, _ := os.ReadFile(conf)
|
|
if data == nil {
|
|
continue
|
|
}
|
|
|
|
data = []byte(shell.ReplaceEnvVars(string(data)))
|
|
configs = append(configs, data)
|
|
} else {
|
|
// config as raw YAML
|
|
configs = append(configs, []byte(conf))
|
|
}
|
|
}
|
|
|
|
if ConfigPath != "" {
|
|
if !filepath.IsAbs(ConfigPath) {
|
|
if cwd, err := os.Getwd(); err == nil {
|
|
ConfigPath = filepath.Join(cwd, ConfigPath)
|
|
}
|
|
}
|
|
Info["config_path"] = ConfigPath
|
|
}
|
|
|
|
var cfg struct {
|
|
Mod map[string]string `yaml:"log"`
|
|
}
|
|
|
|
LoadConfig(&cfg)
|
|
|
|
log.Logger = NewLogger(cfg.Mod["format"], cfg.Mod["level"])
|
|
|
|
modules = cfg.Mod
|
|
|
|
log.Info().Msgf("go2rtc version %s %s/%s", Version, runtime.GOOS, runtime.GOARCH)
|
|
|
|
migrateStore()
|
|
}
|
|
|
|
func LoadConfig(v any) {
|
|
for _, data := range configs {
|
|
if err := yaml.Unmarshal(data, v); err != nil {
|
|
log.Warn().Err(err).Msg("[app] read config")
|
|
}
|
|
}
|
|
}
|
|
|
|
func PatchConfig(key string, value any, path ...string) error {
|
|
if ConfigPath == "" {
|
|
return errors.New("config file disabled")
|
|
}
|
|
|
|
// empty config is OK
|
|
b, _ := os.ReadFile(ConfigPath)
|
|
|
|
b, err := yaml.Patch(b, key, value, path...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.WriteFile(ConfigPath, b, 0644)
|
|
}
|
|
|
|
// internal
|
|
|
|
type Config []string
|
|
|
|
func (c *Config) String() string {
|
|
return strings.Join(*c, " ")
|
|
}
|
|
|
|
func (c *Config) Set(value string) error {
|
|
*c = append(*c, value)
|
|
return nil
|
|
}
|
|
|
|
var configs [][]byte
|