81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.1e99.eu/1e99/wolbodge/devices"
|
|
"git.1e99.eu/1e99/wolbodge/sessions"
|
|
)
|
|
|
|
func NewStorage() (sessions.Storage, error) {
|
|
var storageType string
|
|
Env("WOLBODGE_STORAGE_TYPE", &storageType, "mem")
|
|
|
|
switch storageType {
|
|
case "mem", "memory":
|
|
return sessions.NewMemStorage(), nil
|
|
|
|
default:
|
|
return nil, fmt.Errorf("unknown storage type: %s", storageType)
|
|
}
|
|
}
|
|
|
|
func NewDevice() (devices.Device, error) {
|
|
var deviceType string
|
|
Env("WOLBODGE_DEVICE_TYPE", &deviceType, "test")
|
|
|
|
switch deviceType {
|
|
case "test":
|
|
return devices.NewTest(false), nil
|
|
|
|
case "command":
|
|
var shell []string
|
|
var statusCmd string
|
|
var toggleStatusCmd string
|
|
Env("WOLBODGE_DEVICE_SHELL", &shell, "")
|
|
Env("WOLBODGE_DEVICE_STATUS_COMMAND", &statusCmd, "")
|
|
Env("WOLBODGE_DEVICE_TOGGLE_STATUS_COMMAND", &toggleStatusCmd, "")
|
|
|
|
return devices.NewCommand(shell, statusCmd, toggleStatusCmd), nil
|
|
|
|
default:
|
|
return nil, fmt.Errorf("unknown device type: %s", deviceType)
|
|
}
|
|
}
|
|
|
|
func Env(name string, out any, def string) {
|
|
raw := os.Getenv(name)
|
|
if raw == "" {
|
|
raw = def
|
|
log.Printf("No \"%s\" provided, defaulting to \"%s\".", name, def)
|
|
}
|
|
|
|
switch value := out.(type) {
|
|
case *int:
|
|
i, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
log.Printf("\"%s\" is not a number (\"%s\").", name, raw)
|
|
return
|
|
}
|
|
|
|
*value = int(i)
|
|
|
|
case *bool:
|
|
switch raw {
|
|
case "true", "TRUE", "1":
|
|
*value = true
|
|
case "false", "FALSE", "0":
|
|
*value = false
|
|
}
|
|
|
|
case *string:
|
|
*value = raw
|
|
|
|
case *[]string:
|
|
*value = strings.Fields(raw)
|
|
}
|
|
}
|