Files
home-sensors/internal/watcher/config/config.go
T

72 lines
1.6 KiB
Go
Raw Normal View History

2023-10-01 20:38:38 -07:00
// Package config handles loading the config.
package config
import (
"flag"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
var Default *Mod
type Cmd struct {
Binary string
Arguments []string
}
type Config struct {
2023-10-03 16:17:34 -07:00
HomeName string `yaml:"home"`
CameraName string `yaml:"name"`
H264Cmd *Cmd `yaml:"h264"`
IVFCmd *Cmd `yaml:"ivf"`
SensorCmd *Cmd `yaml:"sensor"`
SensorRateMS int64 `yaml:"sensor_rate_ms"`
LocalCopy string `yaml:"local_copy"`
2023-10-01 20:38:38 -07:00
}
2024-01-15 22:44:05 -08:00
func New(source []byte, name string) (*Config, error) {
2023-10-01 20:38:38 -07:00
config := &Config{}
if err := yaml.Unmarshal(source, config); err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
2024-01-15 22:44:05 -08:00
config.CameraName = name
2023-10-01 20:38:38 -07:00
return config, nil
}
type Mod struct {
filePath string
2024-01-15 22:44:05 -08:00
namePath string
2023-10-01 20:38:38 -07:00
}
func (m *Mod) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(&m.filePath, "watcher_config", "", "path to the watcher configuration")
2024-01-15 22:44:05 -08:00
fs.StringVar(&m.namePath, "watcher_name", "/var/lib/dbus/machine-id", "location of the file to pull name from")
2023-10-01 20:38:38 -07:00
}
func (m *Mod) Get() (*Config, error) {
bytes, err := os.ReadFile(m.filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", m.filePath, err)
}
2024-01-15 22:44:05 -08:00
// Override name using a unique ID; we can let users name things in the app
myID, err := os.ReadFile(m.namePath)
if err != nil {
return nil, fmt.Errorf("failed to read machine ID from %q", m.namePath)
}
config, err := New(bytes, string(myID))
2023-10-01 20:38:38 -07:00
if err != nil {
return nil, err
}
return config, nil
}
func init() {
Default = &Mod{}
Default.RegisterFlags(flag.CommandLine)
}