// Package puppet holds the small pieces of PuppetDB/Puppetboard plumbing shared // by node-lookup and its companion tools (pburl, pblastreport): config loading, // PuppetDB "nodes" queries, Puppetboard URL construction, and reading hostnames // from piped node-lookup output. // // Every tool reads the same config files and env vars, so one config file // configures the whole family. package puppet import ( "fmt" "os" "path/filepath" "strings" "gopkg.in/yaml.v3" ) const ( // DefaultRoleFact is the role fact node-lookup queries with -R. DefaultRoleFact = "enc_role" appName = "node-lookup" configFileName = "config.yaml" ) // SystemConfigPath is the system-wide config file, read before the user one. var SystemConfigPath = filepath.Join("/etc", appName, configFileName) // Config is shared by all tools. Fields map 1:1 to config file keys and // NODE_LOOKUP_* env vars. type Config struct { PuppetDBURL string `yaml:"puppetdb_url"` RoleFact string `yaml:"role_fact"` PuppetboardURL string `yaml:"puppetboard_url"` // Domain is appended to short (dotless) node names; empty disables that. Domain string `yaml:"domain"` } // envVars maps each config key to its env var. var envVars = map[string]string{ "puppetdb_url": "NODE_LOOKUP_URL", "role_fact": "NODE_LOOKUP_ROLE_FACT", "puppetboard_url": "NODE_LOOKUP_PUPPETBOARD_URL", "domain": "NODE_LOOKUP_DOMAIN", } func (c *Config) field(key string) *string { switch key { case "puppetdb_url": return &c.PuppetDBURL case "role_fact": return &c.RoleFact case "puppetboard_url": return &c.PuppetboardURL case "domain": return &c.Domain } panic("unknown config key " + key) } // ConfigDir returns the XDG_CONFIG_HOME/node-lookup directory. func ConfigDir() string { base := os.Getenv("XDG_CONFIG_HOME") if base == "" { home, _ := os.UserHomeDir() base = filepath.Join(home, ".config") } return filepath.Join(base, appName) } // ConfigPath returns the full path to the user config file. func ConfigPath() string { return filepath.Join(ConfigDir(), configFileName) } // Load merges, lowest precedence first: the built-in role fact, the system // config file, the user config file, then NODE_LOOKUP_* env vars; empty values // never override. Flags are // applied by the caller, which then calls Require. func Load() (Config, error) { cfg := Config{RoleFact: DefaultRoleFact} for _, path := range []string{SystemConfigPath, ConfigPath()} { data, err := os.ReadFile(path) if os.IsNotExist(err) { continue } if err != nil { return cfg, fmt.Errorf("reading config %s: %w", path, err) } var file Config if err := yaml.Unmarshal(data, &file); err != nil { return cfg, fmt.Errorf("parsing config %s: %w", path, err) } for key := range envVars { if v := *file.field(key); v != "" { *cfg.field(key) = v } } } for key, env := range envVars { if v := os.Getenv(env); v != "" { *cfg.field(key) = v } } return cfg, nil } // Require returns an error naming every missing key and where to set it. func (c Config) Require(keys ...string) error { var missing []string for _, k := range keys { if *c.field(k) == "" { missing = append(missing, fmt.Sprintf("%s (env %s)", k, envVars[k])) } } if len(missing) == 0 { return nil } return fmt.Errorf("missing config: %s; set it in %s or %s, via env, or with a flag", strings.Join(missing, ", "), ConfigPath(), SystemConfigPath) }