summaryrefslogtreecommitdiff
blob: a4caa8fbf55773a03ebb81658658b4e6bb4e1a38 (plain)
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
package main

import (
	"glsamaker/pkg/app"
	"glsamaker/pkg/config"
	"glsamaker/pkg/cveimport"
	"glsamaker/pkg/logger"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"
)

func printHelp() {
	fmt.Println("Please specific one of the following options:")
	fmt.Println("  glsamaker --update       -- incrementally import cve's and update the database")
	fmt.Println("  glsamaker --full-update  -- import all cve's and update the database")
	fmt.Println("  glsamaker --serve        -- serve the application")
}

func isCommand(command string) bool {
	return len(os.Args) > 1 && os.Args[1] == command
}

func main() {

	errorLogFile := logger.CreateLogFile(config.LogFile())
	defer errorLogFile.Close()
	initLoggers(os.Stdout, errorLogFile)

	if isCommand("--serve") {
		waitForPostgres(10)
		app.Serve()
	} else if isCommand("--full-update") {
		waitForPostgres(5)
		cveimport.FullUpdate()
	} else if isCommand("--update") {
		waitForPostgres(7)
		cveimport.Update()
	} else {
		printHelp()
	}

}

// initialize the loggers depending on whether
// config.debug is set to true
func initLoggers(infoHandler io.Writer, errorHandler io.Writer) {
	if config.Debug() == "true" {
		logger.Init(os.Stdout, infoHandler, errorHandler)
	} else {
		logger.Init(ioutil.Discard, infoHandler, errorHandler)
	}
}

// TODO this has to be solved differently
// wait for postgres to come up
func waitForPostgres(seconds int) {
	time.Sleep(time.Duration(seconds) * time.Second)
}