-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
73 lines (61 loc) · 1.58 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2021 Bojan Zivanovic and contributors
// SPDX-License-Identifier: Apache-2.0
package broom
import (
"fmt"
"os"
"sort"
"gopkg.in/yaml.v2"
)
// Config represents Broom's configuration.
type Config map[string]ProfileConfig
// Profiles returns a list of all configured profiles.
func (c Config) Profiles() []string {
profiles := make([]string, 0, len(c))
for profile := range c {
profiles = append(profiles, profile)
}
sort.Strings(profiles)
return profiles
}
// ProfileConfig represents Broom's per-profile configuration.
type ProfileConfig struct {
SpecFile string `yaml:"spec_file"`
ServerURL string `yaml:"server_url"`
Auth AuthConfig `yaml:"auth"`
}
// AuthConfig represents a profile's authentication configuration.
type AuthConfig struct {
Credentials string `yaml:"credentials"`
Command string `yaml:"command"`
Type string `yaml:"type"`
APIKeyHeader string `yaml:"api_key_header"`
}
// ReadConfig reads a config file with the given filename.
func ReadConfig(filename string) (Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return Config{}, err
}
if len(data) == 0 {
return Config{}, fmt.Errorf("%s is empty", filename)
}
config := Config{}
err = yaml.Unmarshal(data, &config)
if err != nil {
return Config{}, err
}
return config, nil
}
// WriteConfig writes the given config to the given filename.
func WriteConfig(filename string, cfg Config) error {
b, err := yaml.Marshal(cfg)
if err != nil {
return nil
}
err = os.WriteFile(filename, b, 0666)
if err != nil {
return err
}
return nil
}