fuzzyworld

template / files / internal / core / config.go

config.go

Blame: the commit that last changed each line · file · history

37160b89 2026-05-01 Pavel Safronov1package core
4a7361b0 2022-09-21 Pavel Safronov2
3import (
55d96f9c 2022-09-23 Pavel Safronov4 "fmt"
4a7361b0 2022-09-21 Pavel Safronov5 "os"
6
37160b89 2026-05-01 Pavel Safronov7 "slices"
4a7361b0 2022-09-21 Pavel Safronov8 "gopkg.in/yaml.v3"
9)
10
11type Config struct {
55d96f9c 2022-09-23 Pavel Safronov12 Profiles []Profile
4a7361b0 2022-09-21 Pavel Safronov13}
14
b0586433 2022-10-21 Pavel Safronov15func validateConfig(config *Config) error {
83f12389 2026-01-15 Pavel Safronov16 if len(config.Profiles) == 0 {
d1bc1307 2022-09-24 Pavel Safronov17 return fmt.Errorf("no profiles found")
55d96f9c 2022-09-23 Pavel Safronov18 }
19
ae89a42a 2022-11-02 Pavel Safronov20 names := []string{}
55d96f9c 2022-09-23 Pavel Safronov21 foundDefault := false
b0586433 2022-10-21 Pavel Safronov22 for index, profile := range config.Profiles {
23 profile := profile
ae89a42a 2022-11-02 Pavel Safronov24
25 // check for duplicate names
26 if slices.Contains(names, profile.Name) {
27 return fmt.Errorf("duplicate profile name '%v'", profile.Name)
28 }
29 names = append(names, profile.Name)
30
31 // check for multiple default profiles
55d96f9c 2022-09-23 Pavel Safronov32 if profile.IsDefault {
33 if foundDefault {
d1bc1307 2022-09-24 Pavel Safronov34 return fmt.Errorf("multiple default profiles found")
55d96f9c 2022-09-23 Pavel Safronov35 }
36 foundDefault = true
37 }
ae89a42a 2022-11-02 Pavel Safronov38
39 // validate and possibly fix profile
40 err := validateProfile(&profile)
41 if err != nil {
42 return fmt.Errorf("error validating profile %s: %s", profile.Name, err)
b65fb5e9 2022-09-23 Pavel Safronov43 }
b0586433 2022-10-21 Pavel Safronov44 config.Profiles[index] = profile
55d96f9c 2022-09-23 Pavel Safronov45 }
46 return nil
47}
48
4a7361b0 2022-09-21 Pavel Safronov49func loadConfig(path string) (Config, error) {
ae89a42a 2022-11-02 Pavel Safronov50 file, err := os.ReadFile(path)
51 if err != nil {
52 return Config{}, err
4a7361b0 2022-09-21 Pavel Safronov53 }
54
55 var config Config
ae89a42a 2022-11-02 Pavel Safronov56 err = yaml.Unmarshal(file, &config)
57 if err != nil {
58 return Config{}, err
59 }
60
61 err = validateConfig(&config)
62 if err != nil {
63 return Config{}, err
4a7361b0 2022-09-21 Pavel Safronov64 }
65
66 return config, nil
67}
68
69func saveConfig(path string, config Config) error {
ae89a42a 2022-11-02 Pavel Safronov70 err := validateConfig(&config)
71 if err != nil {
72 return err
73 }
74 file, err := os.Create(path)
75 if err != nil {
76 return err
4a7361b0 2022-09-21 Pavel Safronov77 }
83f12389 2026-01-15 Pavel Safronov78 defer func() { _ = file.Close() }()
4a7361b0 2022-09-21 Pavel Safronov79 encoder := yaml.NewEncoder(file)
ae89a42a 2022-11-02 Pavel Safronov80 err = encoder.Encode(config)
81 if err != nil {
82 return err
4a7361b0 2022-09-21 Pavel Safronov83 }
84
85 return nil
86}
55c36f6a 2022-10-05 Pavel Safronov87
ae89a42a 2022-11-02 Pavel Safronov88func mergeConfigs(a Config, b Config) (Config, error) {
89 err := validateConfig(&a)
90 if err != nil {
9573ca02 2022-11-16 Pavel Safronov91 return Config{}, fmt.Errorf("error validating config a: %s", err)
ae89a42a 2022-11-02 Pavel Safronov92 }
93 err = validateConfig(&b)
55c36f6a 2022-10-05 Pavel Safronov94 if err != nil {
9573ca02 2022-11-16 Pavel Safronov95 return Config{}, fmt.Errorf("error validating config b: %s", err)
ae89a42a 2022-11-02 Pavel Safronov96 }
55c36f6a 2022-10-05 Pavel Safronov97
ae89a42a 2022-11-02 Pavel Safronov98 merged := Config{
99 Profiles: a.Profiles,
100 }
101 for _, profile := range b.Profiles {
102 index, err := findProfileIndex(merged.Profiles, profile.Name)
103 if err == nil {
104 merged.Profiles[index] = profile
105 } else {
106 merged.Profiles = append(merged.Profiles, profile)
55c36f6a 2022-10-05 Pavel Safronov107 }
ae89a42a 2022-11-02 Pavel Safronov108 }
109 return merged, nil
110}
55c36f6a 2022-10-05 Pavel Safronov111
ae89a42a 2022-11-02 Pavel Safronov112func findDefaultProfile(profiles []Profile) (Profile, error) {
113 for _, p := range profiles {
114 if p.IsDefault {
115 return p, nil
55c36f6a 2022-10-05 Pavel Safronov116 }
117 }
ae89a42a 2022-11-02 Pavel Safronov118 return Profile{}, fmt.Errorf("unable to find default profile")
119}
55c36f6a 2022-10-05 Pavel Safronov120
ae89a42a 2022-11-02 Pavel Safronov121func findProfileIndex(profiles []Profile, profileName string) (int, error) {
122 for index, p := range profiles {
123 if p.Name == profileName {
124 return index, nil
125 }
55c36f6a 2022-10-05 Pavel Safronov126 }
ae89a42a 2022-11-02 Pavel Safronov127 return 0, fmt.Errorf("unable to find profile '%v'", profileName)
55c36f6a 2022-10-05 Pavel Safronov128}