slideshow/internal/config/config.go

67 lines
1.7 KiB
Go
Raw Normal View History

2023-11-16 15:26:24 +01:00
package config
import (
"os"
"strconv"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
)
var Interval time.Duration
var Directory string
var Resolution string
2023-11-20 16:48:18 +01:00
var Palette string
2023-11-16 15:26:24 +01:00
// initialize the config
func Initialize() {
loggo.SetLogLevelByName(os.Getenv("SLIDESHOW_LOGLEVEL"))
tmp, _ := strconv.Atoi(os.Getenv("SLIDESHOW_INTERVAL"))
if tmp <= 0 {
tmp = 60
}
Interval = time.Duration(tmp) * time.Second
Directory = os.Getenv("SLIDESHOW_DIRECTORY")
if len(Directory) == 0 {
tmp, error := os.UserHomeDir()
if error != nil {
loggo.Fatal("encountered an error getting the current user's home directory", error.Error())
}
Directory = tmp
}
stats, error := os.Stat(Directory)
if error != nil {
loggo.Fatal("encountered an error checking the directory '"+Directory+"'", error.Error())
}
if !stats.IsDir() {
loggo.Fatal("configured directory '" + Directory + "' is not a valid directory")
}
Resolution = os.Getenv("SLIDESHOW_RESOLUTION")
if len(Resolution) > 0 {
width, height, found := strings.Cut(Resolution, "x")
if !found {
loggo.Fatal("encountered an error parsing the configured resolution, make sure to specify the format like '1920x1080'")
}
_, error = strconv.Atoi(width)
if error != nil {
loggo.Fatal("encountered an error parsing the configured width '" + width + "'")
}
_, error = strconv.Atoi(height)
if error != nil {
loggo.Fatal("encountered an error parsing the configured height '" + height + "'")
}
}
2023-11-20 16:48:18 +01:00
Palette = os.Getenv("SLIDESHOW_PALETTE")
2023-11-16 15:26:24 +01:00
}
// check if a resolution has been specified
func IsResolutionSet() bool {
return len(Resolution) > 0
}
2023-11-20 16:48:18 +01:00
// check if a socket has been set
func IsPaletteSet() bool {
return len(Palette) > 0
}