diff --git a/config.toml b/config.toml index 0effe73..2539382 100644 --- a/config.toml +++ b/config.toml @@ -1,8 +1,14 @@ +[General] BaseDir = "/mnt/media" -Format = "mkv" +Format = "mkv" TempBaseDir = "/tmp/nre" EnableConsole = true -WatchedFolder = "/mnt/watched" + +[WatchFolder] +Path = "/mnt/watched" +PollingInterval = 10 +UsePolling = false +UseInotify = true [N_m3u8DLRE] Path = "nre" \ No newline at end of file diff --git a/src/config.go b/src/config.go index 3af4b15..fc8a45f 100644 --- a/src/config.go +++ b/src/config.go @@ -4,19 +4,28 @@ import ( "fmt" "io" "os" + "strconv" + "strings" "github.com/BurntSushi/toml" ) type Config struct { - BaseDir string - Format string - TempBaseDir string - N_m3u8DLRE struct { + General struct { + BaseDir string + Format string + TempBaseDir string + EnableConsole bool + } + WatchFolder struct { + Path string + UsePolling bool + UseInotify bool + PollingInterval int + } + N_m3u8DLRE struct { Path string } - EnableConsole bool - WatchedFolder string } var config Config @@ -24,25 +33,109 @@ var config Config func loadConfig() { configFile, err := os.Open("config.toml") if err != nil { - fmt.Println("Error opening config file:", err) - return + logger.LogError("Config", fmt.Sprintf("Error opening config file: %v", err)) + os.Exit(1) } defer configFile.Close() byteValue, _ := io.ReadAll(configFile) if _, err := toml.Decode(string(byteValue), &config); err != nil { - fmt.Println("Error decoding config file:", err) - return + logger.LogError("Config", fmt.Sprintf("Error decoding config file: %v", err)) + os.Exit(1) } - if config.N_m3u8DLRE.Path == "" { - fmt.Println("Error: N_m3u8DL-RE path is not specified in the config file") - return + overrideConfigWithEnv() + + if err := validatePaths(); err != nil { + logger.LogError("Config", fmt.Sprintf("Configuration error: %v", err)) + os.Exit(1) } - if config.WatchedFolder == "" { - fmt.Println("Error: Watched folder is not specified in the config file") - return + if config.WatchFolder.PollingInterval <= 0 { + config.WatchFolder.PollingInterval = 10 + } + + logConfig() +} + +func overrideConfigWithEnv() { + if envBaseDir := os.Getenv("BASE_DIR"); envBaseDir != "" { + config.General.BaseDir = envBaseDir + } + if envFormat := os.Getenv("FORMAT"); envFormat != "" { + config.General.Format = envFormat + } + if envTempBaseDir := os.Getenv("TEMP_BASE_DIR"); envTempBaseDir != "" { + config.General.TempBaseDir = envTempBaseDir + } + if envWatchedFolder := os.Getenv("WATCHED_FOLDER"); envWatchedFolder != "" { + config.WatchFolder.Path = envWatchedFolder + } + if envUsePolling := os.Getenv("USE_POLLING"); envUsePolling != "" { + config.WatchFolder.UsePolling = strings.ToLower(envUsePolling) == "true" + } + if envUseInotify := os.Getenv("USE_INOTIFY"); envUseInotify != "" { + config.WatchFolder.UseInotify = strings.ToLower(envUseInotify) == "true" + } + if envPollingInterval := os.Getenv("POLLING_INTERVAL"); envPollingInterval != "" { + if interval, err := strconv.Atoi(envPollingInterval); err == nil { + config.WatchFolder.PollingInterval = interval + } } } + +func validatePaths() error { + paths := []struct { + name string + path string + }{ + {"BaseDir", config.General.BaseDir}, + } + + for _, p := range paths { + if p.path == "" { + return fmt.Errorf("%s is not specified", p.name) + } + if _, err := os.Stat(p.path); os.IsNotExist(err) { + return fmt.Errorf("%s does not exist: %s", p.name, p.path) + } else if err != nil { + return fmt.Errorf("error accessing %s: %v", p.name, err) + } + } + + if config.WatchFolder.UsePolling || config.WatchFolder.UseInotify { + if config.WatchFolder.Path == "" { + return fmt.Errorf("WatchedFolder is not specified") + } + if _, err := os.Stat(config.WatchFolder.Path); os.IsNotExist(err) { + return fmt.Errorf("WatchedFolder does not exist: %s", config.WatchFolder.Path) + } else if err != nil { + return fmt.Errorf("error accessing WatchedFolder: %v", err) + } + } + + return nil +} + +func logConfig() { + configInfo := fmt.Sprintf(` +Configuration Loaded: + General: + BaseDir: %s + Format: %s + TempBaseDir: %s + EnableConsole: %t + WatchFolder: + Path: %s + UsePolling: %t + UseInotify: %t + PollingInterval: %d + N_m3u8DLRE: + Path: %s +`, config.General.BaseDir, config.General.Format, config.General.TempBaseDir, config.General.EnableConsole, + config.WatchFolder.Path, config.WatchFolder.UsePolling, config.WatchFolder.UseInotify, config.WatchFolder.PollingInterval, + config.N_m3u8DLRE.Path) + + logger.LogInfo("Config", configInfo) +}