Add options for polling, path validation, env variables

This commit is contained in:
Joren 2024-10-07 12:45:49 +02:00
parent f9c2ac64d7
commit fe6b7c78f6
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 117 additions and 18 deletions

View File

@ -1,8 +1,14 @@
[General]
BaseDir = "/mnt/media" BaseDir = "/mnt/media"
Format = "mkv" Format = "mkv"
TempBaseDir = "/tmp/nre" TempBaseDir = "/tmp/nre"
EnableConsole = true EnableConsole = true
WatchedFolder = "/mnt/watched"
[WatchFolder]
Path = "/mnt/watched"
PollingInterval = 10
UsePolling = false
UseInotify = true
[N_m3u8DLRE] [N_m3u8DLRE]
Path = "nre" Path = "nre"

View File

@ -4,19 +4,28 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strconv"
"strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
type Config struct { type Config struct {
BaseDir string General struct {
Format string BaseDir string
TempBaseDir string Format string
N_m3u8DLRE struct { TempBaseDir string
EnableConsole bool
}
WatchFolder struct {
Path string
UsePolling bool
UseInotify bool
PollingInterval int
}
N_m3u8DLRE struct {
Path string Path string
} }
EnableConsole bool
WatchedFolder string
} }
var config Config var config Config
@ -24,25 +33,109 @@ var config Config
func loadConfig() { func loadConfig() {
configFile, err := os.Open("config.toml") configFile, err := os.Open("config.toml")
if err != nil { if err != nil {
fmt.Println("Error opening config file:", err) logger.LogError("Config", fmt.Sprintf("Error opening config file: %v", err))
return os.Exit(1)
} }
defer configFile.Close() defer configFile.Close()
byteValue, _ := io.ReadAll(configFile) byteValue, _ := io.ReadAll(configFile)
if _, err := toml.Decode(string(byteValue), &config); err != nil { if _, err := toml.Decode(string(byteValue), &config); err != nil {
fmt.Println("Error decoding config file:", err) logger.LogError("Config", fmt.Sprintf("Error decoding config file: %v", err))
return os.Exit(1)
} }
if config.N_m3u8DLRE.Path == "" { overrideConfigWithEnv()
fmt.Println("Error: N_m3u8DL-RE path is not specified in the config file")
return if err := validatePaths(); err != nil {
logger.LogError("Config", fmt.Sprintf("Configuration error: %v", err))
os.Exit(1)
} }
if config.WatchedFolder == "" { if config.WatchFolder.PollingInterval <= 0 {
fmt.Println("Error: Watched folder is not specified in the config file") config.WatchFolder.PollingInterval = 10
return }
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)
}