Files
pi-dashboard/config.go

57 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// Config holds the dashboard configuration
type Config struct {
PollInterval int `yaml:"poll_interval"` // in seconds
Local struct {
Path string `yaml:"path"`
} `yaml:"local"`
Remote []RemoteConfig `yaml:"remote"`
}
// RemoteConfig holds SSH connection info for a remote machine
type RemoteConfig struct {
Host string `yaml:"host"`
User string `yaml:"user"`
Path string `yaml:"path"`
Port int `yaml:"port"`
}
func defaultConfig() Config {
home, _ := os.UserHomeDir()
return Config{
PollInterval: 2,
Local: struct {
Path string `yaml:"path"`
}{
Path: filepath.Join(home, ".pi", "agent", "dashboard"),
},
}
}
func loadConfig(path string) (Config, error) {
cfg := defaultConfig()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return cfg, nil // use defaults
}
return cfg, fmt.Errorf("reading config: %w", err)
}
if err := yaml.Unmarshal(data, &cfg); err != nil {
return cfg, fmt.Errorf("parsing config: %w", err)
}
return cfg, nil
}