-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
89 lines (76 loc) · 2.32 KB
/
main.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
package main
import (
"flag"
"log"
"os"
"path"
"github.com/cpanato/github-sync/pkg/config"
"github.com/cpanato/github-sync/pkg/github"
"github.com/cpanato/github-sync/pkg/reconciler"
)
type options struct {
dryRun bool
config string
authConfig string
}
func parseOptions() options {
var o options
flag.BoolVar(&o.dryRun, "dry-run", true, "does nothing if true (which is the default)")
flag.StringVar(&o.config, "config", "", "path to a configuration file, or directory of files")
flag.StringVar(&o.authConfig, "auth", "auth.json", "path to github auth config")
flag.Parse()
return o
}
func main() {
log.Println("Starting GitHub GitOps reconciler")
o := parseOptions()
c, err := github.LoadConfig(o.authConfig)
if err != nil {
log.Fatalf("Failed to load github auth config: %v.\n", err)
}
client, err := github.NewClient(c.AuthToken)
if err != nil {
log.Fatalf("failed to init client: %v\n", err)
}
stat, err := os.Stat(o.config)
if err != nil {
log.Fatalf("Failed to stat %s: %v\n", o.config, err)
}
p := config.NewParser()
if stat.IsDir() {
err = p.ParseDir(o.config)
} else {
err = p.ParseFile(o.config, path.Dir(o.config))
}
if err != nil {
log.Fatalf("Failed to load config: %v\n", err)
}
strDict := map[string]bool{}
for _, value := range p.Config.Users {
if _, exist := strDict[value.Username]; exist {
log.Fatalf("Reconciliation failed: Username duplicate in config file. Username must be unique unique: username = %s", value.Username)
} else {
strDict[value.Username] = true
}
}
strDictRepo := map[string]bool{}
for _, repo := range p.Config.Repositories {
if _, exist := strDictRepo[repo.Name]; exist {
log.Fatalf("Reconciliation failed: Repository duplicate in config file. Repository must be unique unique: Repository = %s", repo.Name)
} else {
strDictRepo[repo.Name] = true
}
strDictCollab := map[string]bool{}
for _, value := range repo.Collaborators {
if _, exist := strDictCollab[value.Username]; exist {
log.Fatalf("Reconciliation failed: Username duplicate in config file. Username must be unique unique: username = %s", value.Username)
} else {
strDictCollab[value.Username] = true
}
}
}
r := reconciler.New(client, p.Config, c.Org)
if err := r.Reconcile(o.dryRun); err != nil {
log.Fatalf("Reconciliation failed: %v\n", err)
}
}