forked from belak/gitdir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_user.go
66 lines (53 loc) · 1.43 KB
/
config_user.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
package gitdir
import (
"github.com/belak/go-gitdir/internal/git"
"github.com/belak/go-gitdir/models"
)
func (c *Config) loadUserConfigs() error {
// Bail early if we don't need to load anything.
if !c.Options.UserConfigKeys && !c.Options.UserConfigRepos {
return nil
}
errors := make([]error, 0, len(c.Users))
for username := range c.Users {
errors = append(errors, c.loadUserConfig(username))
}
// Because we want to display all the errors, we return this as a
// multi-error rather than bailing on the first error.
return newMultiError(errors...)
}
func (c *Config) loadUserConfig(username string) error {
userRepo, err := git.EnsureRepo(c.fs, "admin/user-"+username)
if err != nil {
return err
}
err = userRepo.Checkout(c.userRepos[username])
if err != nil {
return err
}
if userRepo.FileExists("config.yml") {
data, err := userRepo.GetFile("config.yml")
if err != nil {
return err
}
userConfig, err := models.ParseUserConfig(data)
if err != nil {
return err
}
if c.Options.UserConfigKeys {
c.Users[username].Keys = append(c.Users[username].Keys, userConfig.Keys...)
}
if c.Options.UserConfigRepos {
for repoName, repo := range userConfig.Repos {
// If it's already defined, skip it.
//
// TODO: this should throw a validation error
if _, ok := c.Users[username].Repos[repoName]; ok {
continue
}
c.Users[username].Repos[repoName] = repo
}
}
}
return nil
}