-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
183 lines (151 loc) · 3.94 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
177
178
179
180
181
182
183
package main
import (
"errors"
"fmt"
"log"
"net/http"
"strings"
"github.tools.sap/actions-rollout-app/config"
"github.tools.sap/actions-rollout-app/pkg/clients"
"github.tools.sap/actions-rollout-app/pkg/webhooks"
"github.tools.sap/actions-rollout-app/utils"
"github.com/go-playground/validator"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
const (
cfgFileType = "yaml"
moduleName = "sap-actions-controller"
)
var (
cfgFile string
logger *zap.SugaredLogger
globalConfig *config.Configuration
)
type Opts struct {
BindAddr string
Port int
}
var cmd = &cobra.Command{
Use: moduleName,
Short: "a bot helping with automating tasks on github",
Version: utils.V.String(),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := initConfig()
if err != nil {
return err
}
initLogging()
opts, err := initOpts()
if err != nil {
return fmt.Errorf("unable to init options: %w", err)
}
return run(opts)
},
}
func main() {
if err := cmd.Execute(); err != nil {
logger.Fatalw("an error occurred", "error", err)
}
}
func init() {
cmd.PersistentFlags().StringP("log-level", "", "info", "sets the application log level")
cmd.Flags().StringVarP(&cfgFile, "config", "c", "", "alternative path to config file")
cmd.Flags().StringP("bind-addr", "", "127.0.0.1", "the bind addr of the server")
cmd.Flags().IntP("port", "", 3000, "the port to serve on")
err := viper.BindPFlags(cmd.Flags())
if err != nil {
log.Fatalf("unable to construct root command: %v", err)
}
err = viper.BindPFlags(cmd.PersistentFlags())
if err != nil {
log.Fatalf("unable to construct root command: %v", err)
}
}
func initOpts() (*Opts, error) {
opts := &Opts{
BindAddr: viper.GetString("bind-addr"),
Port: viper.GetInt("port"),
}
validate := validator.New()
err := validate.Struct(opts)
if err != nil {
return nil, err
}
return opts, nil
}
func initConfig() error {
viper.SetEnvPrefix("SAP_ACTIONS_ROBOT")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
viper.SetConfigType(cfgFileType)
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("config file path set explicitly, but unreadable: %w", err)
}
} else {
viper.SetConfigName(moduleName + "." + cfgFileType)
viper.AddConfigPath("/etc/" + moduleName)
viper.AddConfigPath("$HOME/." + moduleName)
viper.AddConfigPath("/app/" + moduleName)
viper.AddConfigPath("/app")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
usedCfg := viper.ConfigFileUsed()
if usedCfg != "" {
return fmt.Errorf("config file unreadable: %w", err)
}
}
}
fmt.Sprintln("viper config file", viper.ConfigFileUsed())
err := loadConfig()
if err != nil {
return fmt.Errorf("error occurred loading config: %w", err)
}
return nil
}
func loadConfig() error {
var err error
globalConfig, err = config.New(viper.ConfigFileUsed())
if err != nil {
return err
}
return nil
}
func initLogging() {
level := zap.InfoLevel
if viper.IsSet("log-level") {
err := level.UnmarshalText([]byte(viper.GetString("log-level")))
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
}
cfg := zap.NewProductionConfig()
cfg.Level = zap.NewAtomicLevelAt(level)
l, err := cfg.Build()
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
logger = l.Sugar()
}
func run(opts *Opts) error {
cs, err := clients.InitClients(logger, globalConfig.Clients)
if err != nil {
return err
}
err = webhooks.InitWebhooks(logger, cs, globalConfig)
if err != nil {
return err
}
addr := fmt.Sprintf("%s:%d", opts.BindAddr, opts.Port)
logger.Infow("starting Actions Controller server", "version", utils.V.String(), "address", addr)
err = http.ListenAndServe(addr, nil)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
var visitors = make(map[string]bool)