-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (139 loc) · 3.33 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
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/opensourceways/community-robot-lib/logrusutil"
"github.com/opensourceways/sync-repo-file/client"
"github.com/opensourceways/sync-repo-file/server"
"github.com/sirupsen/logrus"
)
type options struct {
platform string
endpoint string
fileNames string
orgRepos string
}
func (o options) validate() error {
if o.endpoint == "" {
return fmt.Errorf("endpoint must be set")
}
if o.orgRepos == "" {
return fmt.Errorf("orgRepos must be set")
}
return nil
}
func gatherOptions(fs *flag.FlagSet, args ...string) options {
var o options
fs.StringVar(&o.platform, "platform", "gitee", "the platform of the repository that needs to sync files")
fs.StringVar(&o.fileNames, "fileNames", "OWNERS", "the file name that needs to be synchronized, multiple separated ,")
fs.StringVar(&o.endpoint, "endpoint", "", "grpc connection address")
fs.StringVar(&o.orgRepos, "orgRepos", "", "the full path of the repository that needs to be synchronized, e.g: org/repo. multiple separated with ,")
_ = fs.Parse(args)
return o
}
func main() {
logrusutil.ComponentInit("sync-repo-file-job")
o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
if err := o.validate(); err != nil {
logrus.WithError(err).Error("parse option")
return
}
syncConf := transOpt2SyncConfig(o)
logrus.Info(syncConf)
clients := initClients(o.platform, o.endpoint)
if len(clients) == 0 {
return
}
defer func() {
for _, cli := range clients {
_ = cli.Stop()
}
}()
clis := map[string]server.SyncFileClient{}
for k, v := range clients {
clis[k] = v
}
wait, cancel := server.DoOnce(clis, syncConf, 10)
run(wait, cancel)
}
func transOpt2SyncConfig(o options) []server.SyncFileConfig {
sfc := server.SyncFileConfig{}
sfc.Platform = o.platform
sfc.FileNames = strings.Split(o.fileNames, ",")
rrs := strings.Split(o.orgRepos, ",")
mr := make(map[string][]string, 0)
for _, v := range rrs {
ts := strings.Split(v, "/")
tOrg, tRepo := "", ""
if len(ts) == 0 {
continue
}
if len(ts) == 1 {
tOrg = ts[0]
} else {
tOrg = ts[0]
tRepo = ts[1]
}
if tOrg == "" {
continue
}
if _, ok := mr[tOrg]; ok {
if tRepo != "" {
mr[tOrg] = append(mr[tOrg], tRepo)
}
} else {
if tRepo != "" {
mr[tOrg] = []string{tRepo}
} else {
mr[tOrg] = []string{}
}
}
}
for k, v := range mr {
sfc.OrgRepos = append(sfc.OrgRepos, server.OrgRepos{
Org: k,
Repos: v,
})
}
return []server.SyncFileConfig{sfc}
}
func initClients(platform, endpoint string) map[string]*client.SyncFileClient {
clients := map[string]*client.SyncFileClient{}
cli, err := client.NewSyncFileClient(endpoint)
if err != nil {
logrus.WithField("enpoint", endpoint).WithError(err).Infof(
"init sync file client for platform:%s", platform,
)
}
clients[platform] = cli
return clients
}
func run(wait, cancel func()) {
done := make(chan struct{})
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
var wg sync.WaitGroup
defer func() {
wg.Wait()
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-done:
logrus.Info("receive done. exit normally")
return
case <-sig:
logrus.Info("receive exit signal")
cancel()
return
}
}()
wait()
close(done)
}