-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
129 lines (114 loc) · 3.38 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
// Config : configuration for what orgs / users / repos and token to use
type Config struct {
AutoDiscover struct {
Organizations []struct {
Name string `yaml:"name"`
Topic string `yaml:"topic"`
} `yaml:"organizations"`
Users []struct {
Name string `yaml:"name"`
Topic string `yaml:"topic"`
} `yaml:"users"`
} `yaml:"autoDiscover"`
SubscribedRepos []string `yaml:"subscribedRepos"`
Token string `yaml:"token"`
}
func ensureConfigDirExists(configDir string) error {
// Check if config directory exists
_, err := os.Stat(configDir)
if os.IsNotExist(err) {
// Config directory doesn't exist, create it
err := os.MkdirAll(configDir, 0700) // 0700 means only the owner can read, write, and execute
if err != nil {
return fmt.Errorf("failed to create config directory: %v", err)
}
} else if err != nil {
// Some error occurred while checking the existence of the directory
return fmt.Errorf("failed to check config directory: %v", err)
}
return nil
}
func ensureConfigFileExists(configFilePath string) error {
_, err := os.Stat(configFilePath)
if os.IsNotExist(err) {
// Config file doesn't exist, create it with default values
defaultConfig := Config{
SubscribedRepos: []string{},
Token: "",
}
configBytes, err := yaml.Marshal(defaultConfig)
if err != nil {
return fmt.Errorf("failed to marshal default config: %v", err)
}
err = os.WriteFile(configFilePath, configBytes, 0600) // 0600 means only the owner can read and write
if err != nil {
return fmt.Errorf("failed to create config file: %v", err)
}
} else if err != nil {
// Some error occurred while checking the existence of the file
return fmt.Errorf("failed to check config file: %v", err)
}
return nil
}
func getConfigFilePath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
configDir := filepath.Join(homeDir, ".config/ghreport")
if err := ensureConfigDirExists(configDir); err != nil {
return "", err
}
configFilePath := filepath.Join(configDir, "config.yaml")
if err := ensureConfigFileExists(configFilePath); err != nil {
return "", err
}
return configFilePath, nil
}
func readConfigFile(configFilePath string) (*Config, error) {
file, err := os.Open(configFilePath)
if err != nil {
return nil, err
}
defer file.Close()
var config Config
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return nil, err
}
return &config, nil
}
func getConfig() (*Config, error) {
// Check if config file exists
configFilePath, err := getConfigFilePath()
if err == nil {
if _, err := os.Stat(configFilePath); err == nil {
// Config file exists, read values from there
config, err := readConfigFile(configFilePath)
if err == nil {
return config, nil
}
}
}
// If config file doesn't exist or there was an error reading it, fallback to environment variables
var config Config
envSubscribedRepos := os.Getenv("subscribedRepos")
if envSubscribedRepos == "" {
return &config, fmt.Errorf("env variable subscribedRepos is not defined")
}
config.SubscribedRepos = strings.Split(envSubscribedRepos, " ")
envToken := os.Getenv("ghreportToken")
if envToken == "" {
return &config, fmt.Errorf("env variable ghreportToken is not defined")
}
config.Token = envToken
return &config, nil
}