-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (75 loc) · 1.85 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"github.com/max-wittig/gitlab-status/version"
)
type appOptions struct {
configPath string
gitlabURL string
gitlabToken string
daemon bool
}
func addEnvironmentVariables(appOptions *appOptions) error {
gitlabURL := os.Getenv("GITLAB_URL")
if gitlabURL == "" {
gitlabURL = "https://gitlab.com"
}
gitlabToken := os.Getenv("GITLAB_TOKEN")
if gitlabURL == "" || gitlabToken == "" {
return errors.New("Missing GitLab environment variables")
}
appOptions.gitlabURL = gitlabURL
appOptions.gitlabToken = gitlabToken
return nil
}
func parseOptions() (*appOptions, error) {
versionFlag := flag.Bool("version", false, "Version")
configFlag := flag.String("config", "", "Path to the config.yml file")
daemonFlag := flag.Bool("daemon", true, "Run as daemon")
flag.Parse()
aConfig := appOptions{}
if *versionFlag {
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Git Commit:", version.GitCommit)
fmt.Println("Version:", version.Version)
fmt.Println("Go Version:", version.GoVersion)
fmt.Println("OS / Arch:", version.OsArch)
return nil, nil
}
if *configFlag != "" {
// Check if path exists
if _, err := os.Stat(*configFlag); !os.IsNotExist(err) {
aConfig.configPath = *configFlag
} else {
return nil, errors.New("Config file doesn't exist")
}
}
aConfig.daemon = *daemonFlag
err := addEnvironmentVariables(&aConfig)
if err != nil {
return nil, err
}
return &aConfig, nil
}
func main() {
appOptions, err := parseOptions()
if err != nil {
log.Fatalln(err)
}
statusConfig, err := ReadConfig(appOptions.configPath)
if err != nil {
log.Fatalln("Could not read config")
}
if appOptions.daemon {
err = ScheduleDaemons(appOptions, statusConfig)
} else {
err = UpdateGitlabStatus(appOptions, statusConfig)
}
if err != nil {
log.Fatalln(err)
}
}