This repository was archived by the owner on Dec 19, 2022. It is now read-only.
forked from leominov/gitlab-project-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
63 lines (52 loc) · 1.87 KB
/
config.go
File metadata and controls
63 lines (52 loc) · 1.87 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/imdario/mergo"
"github.com/spf13/viper"
)
type Config struct {
GitLabUrl string `yaml:"gitlab_url"`
GitLabToken string `yaml:"gitlab_private_token"`
CreateMissing bool `yaml:"create_missing"`
GroupID string `yaml:"group_id"`
GroupSettings map[string]interface{} `yaml:"group_settings"`
Settings map[string]interface{} `yaml:"project_settings"`
Overrides map[string]interface{} `yaml:"overrides"`
OnlyProject []string `yaml:"only_projects"`
ExcludeProjects []string `yaml:"exclude_projects"`
}
func ConfigFromFile(file string) (*Config, error) {
config, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Can't read config file: %w", err)
}
viper.SetConfigType("yaml")
viper.SetEnvPrefix("gitlab")
if err := viper.ReadConfig(bytes.NewBufferString(os.ExpandEnv(string(config)))); err != nil {
return nil, fmt.Errorf("Can't parse config: %w", err)
}
viper.AutomaticEnv() // read in environment variables that match
c := Config{
GitLabUrl: strings.TrimSuffix(viper.GetString("gitlab_url"), "/"),
GitLabToken: viper.GetString("gitlab_private_token"),
CreateMissing: viper.GetBool("create_missing"),
GroupID: viper.GetString("group_id"),
GroupSettings: viper.GetStringMap("group_settings"),
Settings: viper.GetStringMap("project_settings"),
Overrides: viper.GetStringMap("overrides"),
OnlyProject: viper.GetStringSlice("only_projects"),
ExcludeProjects: viper.GetStringSlice("exclude_projects"),
}
return &c, nil
}
func MergeConfig(dst map[string]interface{}, src map[string]interface{}) error {
err := mergo.Merge(&dst, src, mergo.WithOverride)
if err != nil {
return err
}
return nil
}