-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
114 lines (102 loc) · 3.87 KB
/
config.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
package prbot
import (
"flag"
"fmt"
"os"
"time"
"github.com/ilyakaznacheev/cleanenv"
"github.com/rs/zerolog/log"
)
type Config struct {
Build struct {
Commit string `yaml:"Commit" env:"COMMIT" env-description:"Commit hash of the build passed"`
Repo string `yaml:"Repo" env:"REPO" env-description:"Github repo containing the source of the build"`
Version string `yaml:"Version" env:"VERSION" env-description:"Version number 1-0.14.qwehsadhascxzva"`
} `yaml:"Build" env-prefix:"BUILD_"`
ServiceName string `yaml:"ServiceName" env:"SERVICE_NAME" env-default:"pr-bot" env-description:"Name of the service"`
Env string `yaml:"Env" env:"ENV" env-default:"local" env-description:"stage name; one of local|dev|staging|prod"`
Server struct {
Host string `yaml:"Host" env:"HOST" env-description:"Server host"`
Port int `yaml:"Port" env:"PORT" env-description:"Server port"`
TLS struct {
CertFile string `yaml:"CertFile" env:"CERT_FILE" env-description:"Server certificate file path"`
KeyFile string `yaml:"KeyFile" env:"KEY_FILE" env-description:"File path of server cert's private key"`
} `yaml:"TLS" env-prefix:"TLS_"`
} `yaml:"Server" env-prefix:"SERVER_"`
AWS struct {
Region string `yaml:"REGION" env:"REGION" env-default:"us-east-1"`
Secrets struct {
Webhook string `yaml:"Webhook" env:"WEBHOOK" env-default:"/ci/pr-bot/webhook"`
Token string `yaml:"Token" env:"TOKEN" env-default:"/ci/pr-bot/token"`
} `yaml:"Secrets" env-prefix:"SECRETS_"`
} `yaml:"AWS" env-prefix:"AWS_"`
OPA struct {
Bundles struct {
Root string `yaml:"Root" env:"ROOT"`
Filename string `yaml:"Filename" env:"FILENAME"`
ECR struct {
Registry string `yaml:"Registry" env:"REGISTRY"`
Repo string `yaml:"Repo" env:"REPO"`
Tag string `yaml:"Tag" env:"TAG"`
} `yaml:"ECR" env-prefix:"ECR_"`
} `yaml:"Bundles" env-prefix:"BUNDLES_"`
EvaluationReport struct {
TTL time.Duration `yaml:"TTL" env:"TTL"`
TableName string `yaml:"TableName" env:"TABLE_NAME"`
} `yaml:"EvaluationReport" env-prefix:"EVALUATION_REPORT_"`
} `yaml:"OPA" env-prefix:"OPA_"`
Reviewer struct {
Locker struct {
TableName string `yaml:"TableName" env:"TABLE_NAME"`
} `yaml:"Locker" env-prefix:"LOCKER_"`
} `yaml:"Reviewer" env-prefix:"REVIEWER_"`
GHE struct {
ServiceAccount string `yaml:"ServiceAccount" env:"SERVICE_ACCOUNT"`
Hostname string `yaml:"Hostname" env:"HOSTNAME" env-default:"github.com"`
} `yaml:"GHE" env-prefix:"GHE_"`
ConfigStore struct {
Table string `yaml:"Table" env:"TABLE"`
Refresh time.Duration `yaml:"Refresh" env:"REFRESH"`
} `yaml:"ConfigStore" env-prefix:"CONFIG_STORE_"`
Throttler struct {
Table string `yaml:"Table" env:"TABLE"`
PartitionKey string `yaml:"PartitionKey" env:"PARTITION_KEY"`
SortKey string `yaml:"SortKey" env:"SORT_KEY"`
TTLFieldName string `yaml:"TTLFieldName" env:"TTL_FIELD_NAME"`
} `yaml:"Throttler" env-prefix:"THROTTLER_"`
Datastore struct {
Table string `yaml:"Table" env:"TABLE"`
TTL time.Duration `yaml:"TTL" env:"TTL"`
} `yaml:"Datastore" env-prefix:"DATASTORE_"`
}
func ParseConfigFiles() (*Config, error) {
var cfg Config
file := ProcessCLIArgs(&cfg)
err := cleanenv.ReadConfig(file, &cfg)
if err != nil {
log.Err(err).Str("configfile", file).Msg("Error reading configuration from file")
return nil, err
}
return &cfg, nil
}
// ProcessArgs processes and handles CLI arguments
func ProcessCLIArgs(cfg *Config) string {
var path string
f := flag.NewFlagSet("pr-bot", flag.ExitOnError)
f.StringVar(&path, "config", "config.yaml", "file path to configuration file")
fu := f.Usage
f.Usage = func() {
fu()
envHelp, _ := cleanenv.GetDescription(cfg, nil)
//nolint:all
fmt.Println()
//nolint:all
fmt.Println(envHelp)
}
err := f.Parse(os.Args[1:])
if len(path) < 1 || err != nil {
f.Usage()
os.Exit(1)
}
return path
}