template / windows / files / config.go
Branch windows · default branch · all branches
config.go
4a7361b0 2022-09-21 Pavel Safronov | 1 | package main |
| 2 | ||
| 3 | import ( | |
55d96f9c 2022-09-23 Pavel Safronov | 4 | "fmt" |
4a7361b0 2022-09-21 Pavel Safronov | 5 | "os" |
| 6 | ||
ae89a42a 2022-11-02 Pavel Safronov | 7 | "golang.org/x/exp/slices" |
4a7361b0 2022-09-21 Pavel Safronov | 8 | "gopkg.in/yaml.v3" |
| 9 | ) | |
| 10 | ||
| 11 | type Config struct { | |
55d96f9c 2022-09-23 Pavel Safronov | 12 | Profiles []Profile |
4a7361b0 2022-09-21 Pavel Safronov | 13 | } |
| 14 | ||
b0586433 2022-10-21 Pavel Safronov | 15 | func validateConfig(config *Config) error { |
55d96f9c 2022-09-23 Pavel Safronov | 16 | if config.Profiles == nil || len(config.Profiles) == 0 { |
d1bc1307 2022-09-24 Pavel Safronov | 17 | return fmt.Errorf("no profiles found") |
55d96f9c 2022-09-23 Pavel Safronov | 18 | } |
| 19 | ||
ae89a42a 2022-11-02 Pavel Safronov | 20 | names := []string{} |
55d96f9c 2022-09-23 Pavel Safronov | 21 | foundDefault := false |
b0586433 2022-10-21 Pavel Safronov | 22 | for index, profile := range config.Profiles { |
| 23 | profile := profile | |
ae89a42a 2022-11-02 Pavel Safronov | 24 | |
| 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 Safronov | 32 | if profile.IsDefault { |
| 33 | if foundDefault { | |
d1bc1307 2022-09-24 Pavel Safronov | 34 | return fmt.Errorf("multiple default profiles found") |
55d96f9c 2022-09-23 Pavel Safronov | 35 | } |
| 36 | foundDefault = true | |
| 37 | } | |
ae89a42a 2022-11-02 Pavel Safronov | 38 | |
| 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 Safronov | 43 | } |
b0586433 2022-10-21 Pavel Safronov | 44 | config.Profiles[index] = profile |
55d96f9c 2022-09-23 Pavel Safronov | 45 | } |
| 46 | return nil | |
| 47 | } | |
| 48 | ||
4a7361b0 2022-09-21 Pavel Safronov | 49 | func loadConfig(path string) (Config, error) { |
ae89a42a 2022-11-02 Pavel Safronov | 50 | file, err := os.ReadFile(path) |
| 51 | if err != nil { | |
| 52 | return Config{}, err | |
4a7361b0 2022-09-21 Pavel Safronov | 53 | } |
| 54 | ||
| 55 | var config Config | |
ae89a42a 2022-11-02 Pavel Safronov | 56 | 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 Safronov | 64 | } |
| 65 | ||
| 66 | return config, nil | |
| 67 | } | |
| 68 | ||
| 69 | func saveConfig(path string, config Config) error { | |
ae89a42a 2022-11-02 Pavel Safronov | 70 | 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 Safronov | 77 | } |
| 78 | defer file.Close() | |
| 79 | encoder := yaml.NewEncoder(file) | |
ae89a42a 2022-11-02 Pavel Safronov | 80 | err = encoder.Encode(config) |
| 81 | if err != nil { | |
| 82 | return err | |
4a7361b0 2022-09-21 Pavel Safronov | 83 | } |
| 84 | ||
| 85 | return nil | |
| 86 | } | |
55c36f6a 2022-10-05 Pavel Safronov | 87 | |
ae89a42a 2022-11-02 Pavel Safronov | 88 | func mergeConfigs(a Config, b Config) (Config, error) { |
| 89 | err := validateConfig(&a) | |
| 90 | if err != nil { | |
9573ca02 2022-11-16 Pavel Safronov | 91 | return Config{}, fmt.Errorf("error validating config a: %s", err) |
ae89a42a 2022-11-02 Pavel Safronov | 92 | } |
| 93 | err = validateConfig(&b) | |
55c36f6a 2022-10-05 Pavel Safronov | 94 | if err != nil { |
9573ca02 2022-11-16 Pavel Safronov | 95 | return Config{}, fmt.Errorf("error validating config b: %s", err) |
ae89a42a 2022-11-02 Pavel Safronov | 96 | } |
55c36f6a 2022-10-05 Pavel Safronov | 97 | |
ae89a42a 2022-11-02 Pavel Safronov | 98 | 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 Safronov | 107 | } |
ae89a42a 2022-11-02 Pavel Safronov | 108 | } |
| 109 | return merged, nil | |
| 110 | } | |
55c36f6a 2022-10-05 Pavel Safronov | 111 | |
ae89a42a 2022-11-02 Pavel Safronov | 112 | func findDefaultProfile(profiles []Profile) (Profile, error) { |
| 113 | for _, p := range profiles { | |
| 114 | if p.IsDefault { | |
| 115 | return p, nil | |
55c36f6a 2022-10-05 Pavel Safronov | 116 | } |
| 117 | } | |
ae89a42a 2022-11-02 Pavel Safronov | 118 | return Profile{}, fmt.Errorf("unable to find default profile") |
| 119 | } | |
55c36f6a 2022-10-05 Pavel Safronov | 120 | |
ae89a42a 2022-11-02 Pavel Safronov | 121 | func 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 Safronov | 126 | } |
ae89a42a 2022-11-02 Pavel Safronov | 127 | return 0, fmt.Errorf("unable to find profile '%v'", profileName) |
55c36f6a 2022-10-05 Pavel Safronov | 128 | } |