slideshow/internal/config/config.go

157 lines
3.9 KiB
Go
Raw Permalink Normal View History

2023-11-16 15:26:24 +01:00
package config
import (
"os"
"os/exec"
2023-11-16 15:26:24 +01:00
"strconv"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
)
var ApiAddress string
2023-11-16 15:26:24 +01:00
var Interval time.Duration
var Resolution string
var PaletteFile string
var PaletteAlgorithm string
var PaletteColors int
var Script string
var ScriptArgs []string
var ScriptAsync bool
var ScriptStage string
2023-11-16 15:26:24 +01:00
// initialize the config
func Initialize() {
loggo.SetLogLevelByName(os.Getenv("SLIDESHOW_LOGLEVEL"))
ApiAddress = os.Getenv("SLIDESHOW_API")
if len(ApiAddress) == 0 {
loggo.Fatal("no adress of the slideshow-api server has been specified")
2023-11-16 15:26:24 +01:00
}
if !strings.HasPrefix(ApiAddress, "http://") && !strings.HasPrefix(ApiAddress, "https://") {
ApiAddress = "http://" + ApiAddress
2023-11-16 15:26:24 +01:00
}
tmpInt, _ := strconv.Atoi(os.Getenv("SLIDESHOW_INTERVAL"))
if tmpInt <= 0 {
tmpInt = 60
}
Interval = time.Duration(tmpInt) * time.Second
2023-11-16 15:26:24 +01:00
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)
2023-11-16 15:26:24 +01:00
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 + "'")
}
}
PaletteFile = os.Getenv("SLIDESHOW_PALETTE")
PaletteAlgorithm = os.Getenv("SLIDESHOW_PALETTE_ALGORITHM")
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_PALETTE_COLORS"))
if tmpInt <= 0 {
tmpInt = 16
}
PaletteColors = tmpInt
Script = os.Getenv("SLIDESHOW_SCRIPT")
ScriptArgs = strings.Split(os.Getenv("SLIDESHOW_SCRIPT_ARGS"), " ")
ScriptAsync, _ = strconv.ParseBool(os.Getenv("SLIDESHOW_SCRIPT_ASYNC"))
ScriptStage = strings.ToLower(os.Getenv("SLIDESHOW_SCRIPT_STAGE"))
checkScript()
checkFehCommand()
}
// check if a script has been specified and exists
func checkScript() {
if len(Script) == 0 {
return
}
stats, error := os.Stat(Script)
if error != nil {
loggo.Warning("encountered an error getting stats for the script '"+Script+"', script execution is unavailable", error.Error())
Script = ""
return
}
if stats.IsDir() {
loggo.Warning("the script '"+Script+"' seems to be a directory, script execution is unavailable", error.Error())
Script = ""
return
}
if string(stats.Mode().Perm().String()[3]) != "x" {
loggo.Warning("the script '"+Script+"' seems to be a directory, script execution is unavailable", error.Error())
Script = ""
}
checkScriptStage()
}
// check if the specified script stage is valid
func checkScriptStage() {
switch ScriptStage {
case "pre_image":
fallthrough
case "post_image":
fallthrough
case "pre_palette":
fallthrough
case "post_palette":
return
default:
ScriptStage = ""
}
}
// check if 'feh' is available
func checkFehCommand() {
cmd := exec.Command("which", "feh")
error := cmd.Run()
if error == nil {
return
}
loggo.Fatal("could not find command 'feh'")
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 color palette should be generated
2023-11-20 16:48:18 +01:00
func IsPaletteSet() bool {
return len(PaletteFile) > 0
2023-11-20 16:48:18 +01:00
}
// check if a script has been specified
func IsScriptSet() bool {
return len(Script) > 0
}
// check if a script stage has been specified
func IsScriptStageSet() bool {
return len(ScriptStage) > 0
}
// check if script stage 'pre_image' has been specified
func IsScriptStagePreImage() bool {
return ScriptStage == "pre_image"
}
// check if script stage 'post_image' has been specified
func IsScriptStagePostImage() bool {
return ScriptStage == "post_image"
}
// check if script stage 'pre_palette' has been specified
func IsScriptStagePrePalette() bool {
return ScriptStage == "pre_palette"
}
// check if script stage 'post_palette' has been specified
func IsScriptStagePostPalette() bool {
return ScriptStage == "post_palette"
}