Raw
 1package config
 2
 3import "os"
 4
 5func Load(configPath, secretsPath string) (*Config, error) {
 6	return loadFile(configPath, secretsPath, true)
 7}
 8
 9func loadFile(configPath, secretsPath string, exact bool) (*Config, error) {
10	cfg, problems := readFile(configPath)
11	return withSecrets(cfg, problems, secretsPath, exact)
12}
13
14func readFile(configPath string) (*Config, []Problem) {
15	cfg, problems := loadConfig(configPath)
16	if len(problems) == 0 {
17		problems = readTrust(cfg)
18	}
19	if cfg != nil {
20		problems = append(problems, readAppsDir(configPath, cfg)...)
21	}
22	return cfg, problems
23}
24
25func withSecrets(cfg *Config, problems []Problem, secretsPath string, exact bool) (*Config, error) {
26	vars, secretProblems := readSecrets(secretsPath)
27	problems = append(problems, secretProblems...)
28	var secrets Secrets
29	if len(problems) == 0 {
30		secrets, secretProblems = checkSecrets(secretsPath, vars, cfg, exact)
31		problems = append(problems, secretProblems...)
32	}
33	if len(problems) > 0 {
34		sortProblems(problems)
35		return nil, &Error{Problems: problems}
36	}
37	cfg.Secrets = secrets
38	return cfg, nil
39}
40
41func loadConfig(path string) (*Config, []Problem) {
42	data, err := os.ReadFile(path)
43	if err != nil {
44		return nil, []Problem{{File: path, Reason: reasonUnreadable}}
45	}
46	return loadBytes(path, data)
47}
48
49func loadBytes(file string, data []byte) (*Config, []Problem) {
50	idx, problems := indexKeys(file, data)
51	if idx == nil {
52		return nil, problems
53	}
54	raw, typed := decodeTyped(file, data, idx, problems)
55	problems = append(problems, typed...)
56	if raw == nil || len(problems) > 0 {
57		return nil, problems
58	}
59	return validate(file, idx, raw)
60}
61
62func LoadDisk(configPath, secretsPath string) (*Config, error) {
63	cfg, problems := readFile(configPath)
64	return withSecrets(cfg, problems, secretsPath, cfg == nil || cfg.Source != sourceGit)
65}
66
67func Check(name string, data []byte) error {
68	_, problems := loadBytes(name, data)
69	if len(problems) > 0 {
70		sortProblems(problems)
71		return &Error{Problems: problems}
72	}
73	return nil
74}