forked from review-robot/robot-gitee-openeuler-review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (53 loc) · 1.72 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
package main
import (
"flag"
"net/url"
"os"
"github.com/opensourceways/community-robot-lib/giteeclient"
libplugin "github.com/opensourceways/community-robot-lib/giteeplugin"
"github.com/opensourceways/community-robot-lib/logrusutil"
liboptions "github.com/opensourceways/community-robot-lib/options"
"github.com/opensourceways/community-robot-lib/secret"
cache "github.com/opensourceways/repo-file-cache/sdk"
"github.com/sirupsen/logrus"
)
type options struct {
plugin liboptions.PluginOptions
gitee liboptions.GiteeOptions
cacheEndpoint string
maxRetries int
}
func (o *options) Validate() error {
if _, err := url.ParseRequestURI(o.cacheEndpoint); err != nil {
return err
}
if err := o.plugin.Validate(); err != nil {
return err
}
return o.gitee.Validate()
}
func gatherOptions(fs *flag.FlagSet, args ...string) options {
var o options
o.gitee.AddFlags(fs)
o.plugin.AddFlags(fs)
fs.StringVar(&o.cacheEndpoint, "cache-endpoint", "", "The endpoint of repo file cache")
fs.IntVar(&o.maxRetries, "max-retries", 3, "The number of failed retry attempts to call the cache api")
_ = fs.Parse(args)
return o
}
func main() {
logrusutil.ComponentInit(botName)
o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
if err := o.Validate(); err != nil {
logrus.WithError(err).Fatal("Invalid options")
}
secretAgent := new(secret.Agent)
if err := secretAgent.Start([]string{o.gitee.TokenPath}); err != nil {
logrus.WithError(err).Fatal("Error starting secret agent.")
}
c := giteeclient.NewClient(secretAgent.GetTokenGenerator(o.gitee.TokenPath))
s := cache.NewSDK(o.cacheEndpoint, o.maxRetries)
p := newRobot(c, s)
libplugin.Run(p, o.plugin)
secretAgent.Stop()
}