// Package daemon contains types and functions related specifically to the // isle daemon. package daemon import ( "fmt" "io" "isle/yamlutil" "os" "path/filepath" "github.com/imdario/mergo" "gopkg.in/yaml.v3" ) func defaultConfigPath(appDirPath string) string { return filepath.Join(appDirPath, "etc", "daemon.yml") } // CopyDefaultConfig copies the daemon config file embedded in the AppDir into // the given io.Writer. func CopyDefaultConfig(into io.Writer, appDirPath string) error { defaultConfigPath := defaultConfigPath(appDirPath) f, err := os.Open(defaultConfigPath) if err != nil { return fmt.Errorf("opening daemon config at %q: %w", defaultConfigPath, err) } defer f.Close() if _, err := io.Copy(into, f); err != nil { return fmt.Errorf("copying daemon config from %q: %w", defaultConfigPath, err) } return nil } // LoadConfig loads the daemon config from userConfigPath, merges it with // the default found in the appDirPath, and returns the result. // // If userConfigPath is not given then the default is loaded and returned. func LoadConfig( appDirPath, userConfigPath string, ) ( Config, error, ) { defaultConfigPath := defaultConfigPath(appDirPath) var fullDaemon map[string]interface{} if err := yamlutil.LoadYamlFile(&fullDaemon, defaultConfigPath); err != nil { return Config{}, fmt.Errorf("parsing default daemon config file: %w", err) } if userConfigPath != "" { var daemonConfig map[string]interface{} if err := yamlutil.LoadYamlFile(&daemonConfig, userConfigPath); err != nil { return Config{}, fmt.Errorf("parsing %q: %w", userConfigPath, err) } err := mergo.Merge(&fullDaemon, daemonConfig, mergo.WithOverride) if err != nil { return Config{}, fmt.Errorf("merging contents of file %q: %w", userConfigPath, err) } } fullDaemonB, err := yaml.Marshal(fullDaemon) if err != nil { return Config{}, fmt.Errorf("yaml marshaling: %w", err) } var config Config if err := yaml.Unmarshal(fullDaemonB, &config); err != nil { return Config{}, fmt.Errorf("yaml unmarshaling back into Config struct: %w", err) } config.fillDefaults() return config, nil }