-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
238 lines (192 loc) · 4.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gitdir
import (
"fmt"
billy "github.com/go-git/go-billy/v5"
"github.com/belak/go-gitdir/internal/git"
"github.com/belak/go-gitdir/models"
)
// Config represents the config which has been loaded from all repos.
type Config struct {
Invites map[string]string
Groups map[string][]string
Orgs map[string]*models.OrgConfig
Users map[string]*models.AdminConfigUser
Repos map[string]*models.RepoConfig
Options models.AdminConfigOptions
PrivateKeys []models.PrivateKey
// Internal state
fs billy.Filesystem
publicKeys map[string]string `yaml:"-"`
// We store any override hashes for repos so this can be used for hooks as
// well.
adminRepoHash string
orgRepos map[string]string
userRepos map[string]string
}
// NewConfig returns an empty config, attached to the given fs. In general, Load
// should be called after creating a new config at a bare minimum.
func NewConfig(fs billy.Filesystem) *Config {
return &Config{
Invites: make(map[string]string),
Groups: make(map[string][]string),
Orgs: make(map[string]*models.OrgConfig),
Users: make(map[string]*models.AdminConfigUser),
Repos: make(map[string]*models.RepoConfig),
orgRepos: make(map[string]string),
userRepos: make(map[string]string),
publicKeys: make(map[string]string),
Options: models.DefaultAdminConfigOptions,
fs: fs,
}
}
// Load will load the config from the given fs.
func (c *Config) Load() error {
adminRepo, err := c.openAdminRepo()
if err != nil {
return err
}
return c.loadConfig(adminRepo)
}
func (c *Config) openAdminRepo() (*git.Repository, error) {
adminRepo, err := git.EnsureRepo(c.fs, "admin/admin")
if err != nil {
return nil, err
}
err = adminRepo.Checkout(c.adminRepoHash)
if err != nil {
return nil, err
}
return adminRepo, nil
}
func (c *Config) loadConfig(adminRepo *git.Repository) error {
// Load config
err := c.loadAdminConfig(adminRepo)
if err != nil {
return err
}
// Load sub-configs
err = newMultiError(
c.loadUserConfigs(),
c.loadOrgConfigs(),
)
if err != nil {
return err
}
c.flatten()
return nil
}
func (c *Config) EnsureConfig() error {
adminRepo, err := c.openAdminRepo()
if err != nil {
return err
}
return c.ensureConfig(adminRepo)
}
func (c *Config) ensureConfig(adminRepo *git.Repository) error {
// Ensure config
err := c.ensureAdminConfig(adminRepo)
if err != nil {
return err
}
// Load config
err = c.loadConfig(adminRepo)
if err != nil {
return err
}
// We only commit at the very end, after everything has been loaded. This
// ensures we have a valid config.
status, err := adminRepo.Worktree.Status()
if err != nil {
return err
}
if !status.IsClean() {
err = adminRepo.Commit("Updated config", nil)
if err != nil {
return err
}
}
return nil
}
// EnsureUser will load the current admin config and ensure the given user
// exists.
func (c *Config) EnsureAdminUser(username string, pubKey *models.PublicKey) error {
adminRepo, err := c.openAdminRepo()
if err != nil {
return err
}
// Ensure the base config before we try and add a user.
err = c.ensureAdminConfig(adminRepo)
if err != nil {
return err
}
// Ensure User
err = c.ensureAdminUser(adminRepo, username, pubKey)
if err != nil {
return err
}
// Load config
err = c.loadConfig(adminRepo)
if err != nil {
return err
}
// We only commit at the very end, after everything has been loaded. This
// ensures we have a valid config.
status, err := adminRepo.Worktree.Status()
if err != nil {
return err
}
if !status.IsClean() {
err = adminRepo.Commit(fmt.Sprintf("Added %s to config as admin", username), nil)
if err != nil {
return err
}
}
return nil
}
func (c *Config) flatten() {
// Add all user public keys to the config.
for username, user := range c.Users {
for _, key := range user.Keys {
c.publicKeys[key.RawMarshalAuthorizedKey()] = username
}
}
}
// SetHash will set the hash of the admin repo to use when loading.
func (c *Config) SetHash(hash string) error {
adminRepo, err := git.EnsureRepo(c.fs, "admin/admin")
if err != nil {
return err
}
err = adminRepo.Checkout(hash)
if err != nil {
return err
}
c.adminRepoHash = hash
return nil
}
// SetUserHash will set the hash of the given user repo to use when loading.
func (c *Config) SetUserHash(username, hash string) error {
repo, err := git.EnsureRepo(c.fs, "admin/user-"+username)
if err != nil {
return err
}
err = repo.Checkout(hash)
if err != nil {
return err
}
c.userRepos[username] = hash
return nil
}
// SetOrgHash will set the hash of the given org repo to use when loading.
func (c *Config) SetOrgHash(orgName, hash string) error {
repo, err := git.EnsureRepo(c.fs, "admin/org-"+orgName)
if err != nil {
return err
}
err = repo.Checkout(hash)
if err != nil {
return err
}
c.orgRepos[orgName] = hash
return nil
}