-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
138 lines (123 loc) · 3.49 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
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
type BackupProviderConfigType string
const (
BackupProviderConfigTypeGitea BackupProviderConfigType = "gitea"
BackupProviderConfigTypeFile BackupProviderConfigType = "file"
)
type UnmatchedRepoAction string
const (
UnmatchedRepoActionDelete UnmatchedRepoAction = "delete"
UnmatchedRepoActionIgnore UnmatchedRepoAction = "ignore"
)
type BackupProviderConfig struct {
Type BackupProviderConfigType `json:"type"`
Config any `json:"config"`
}
type DefaultConfig struct {
GithubToken string `json:"github_token"`
RepoOwner string `json:"repo_owner"`
Backup *BackupProviderConfig `json:"backup"`
Filter *FilterConfig `json:"filter"`
SpecificGithubToken map[string]string `json:"specific_github_token"`
}
type GithubConfig struct {
Owner string `json:"owner"`
Token string `json:"token"`
IsOwnerOrg bool `json:"is_owner_org"`
RepoOwner string `json:"repo_owner"`
IsRepoOwnerOrg bool `json:"is_repo_owner_org"`
Backup *BackupProviderConfig `json:"backup"`
Filter *FilterConfig `json:"filter"`
SpecificGithubToken map[string]string `json:"specific_github_token"`
}
type FilterConfig struct {
UnmatchedRepoAction UnmatchedRepoAction `json:"unmatched_repo_action"`
AllowRule []string `json:"allow_rule"`
DenyRule []string `json:"deny_rule"`
}
func (c *GithubConfig) MergeDefault(defaultConf *DefaultConfig) {
if defaultConf == nil {
return
}
if c.Token == "" {
c.Token = defaultConf.GithubToken
}
if c.RepoOwner == "" {
c.RepoOwner = defaultConf.RepoOwner
}
if c.RepoOwner == "" {
c.RepoOwner = c.Owner
}
if c.Backup == nil {
c.Backup = defaultConf.Backup
}
if c.Filter == nil {
c.Filter = defaultConf.Filter
if c.Filter == nil {
c.Filter = &FilterConfig{}
}
}
if c.Filter.UnmatchedRepoAction == "" {
c.Filter.UnmatchedRepoAction = defaultConf.Filter.UnmatchedRepoAction
if c.Filter.UnmatchedRepoAction == "" {
c.Filter.UnmatchedRepoAction = UnmatchedRepoActionIgnore
}
}
if len(c.Filter.AllowRule) == 0 {
c.Filter.AllowRule = defaultConf.Filter.AllowRule
}
if len(c.Filter.DenyRule) == 0 {
c.Filter.DenyRule = defaultConf.Filter.DenyRule
}
if len(c.SpecificGithubToken) == 0 {
c.SpecificGithubToken = defaultConf.SpecificGithubToken
}
}
type SyncConfig struct {
DefaultConf *DefaultConfig `json:"default_conf"`
Targets []GithubConfig `json:"targets"`
Cron string `json:"cron"`
}
func ConvertToBackupProviderConfig[T any](raw any) (*T, error) {
b, err := json.Marshal(raw)
if err != nil {
return nil, err
}
var conf T
err = json.Unmarshal(b, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}
func LoadConfig(path string) (*SyncConfig, error) {
var body []byte
var err error
if strings.HasPrefix(path, "http") {
resp, httpErr := http.Get(path)
if httpErr != nil {
return nil, fmt.Errorf("failed to fetch config: %w", httpErr)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
} else {
body, err = os.ReadFile(path)
}
if err != nil {
return nil, fmt.Errorf("failed to read config: %w", err)
}
var conf SyncConfig
err = json.Unmarshal(body, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}