-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
145 lines (125 loc) · 3 KB
/
app.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
// Copyright (C) 2014 Constantin Schomburg <me@cschomburg.com>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package core handles most of the common db, config and proto initialization.
package core
import (
"flag"
"io/ioutil"
"log"
"net/url"
"os"
"os/signal"
"syscall"
"github.com/sarifsystems/sarif/sarif"
"github.com/sarifsystems/sarif/transports/amqp"
"github.com/sarifsystems/sarif/transports/sfproto"
)
var verbose = flag.Bool("v", false, "verbose debug output")
var vverbose = flag.Bool("vv", false, "very verbose debug output: db, individual messages")
var configPath = flag.String("config", "", "path to config file")
type App struct {
AppName string
ModuleName string
Config *Config
Log *Logger
ClientFactory sarif.ClientFactory
}
func NewApp(appName, moduleName string) *App {
if appName == "" {
appName = "sarif"
}
if !flag.Parsed() {
flag.Parse()
}
app := &App{
AppName: appName,
ModuleName: moduleName,
Log: DefaultLog,
}
if *vverbose {
app.Log.SetFlags(log.Ldate | log.Lmicroseconds)
app.Log.SetLevel(LogLevelTrace)
} else if *verbose {
app.Log.SetLevel(LogLevelDebug)
} else {
app.Log.SetLevel(LogLevelInfo)
}
return app
}
func (app *App) Init() {
if err := app.initConfig(); err != nil {
app.Log.Fatalln(err)
}
app.WriteConfig()
}
func (app *App) initConfig() (err error) {
if *configPath == "" {
if app.ModuleName == "temp" {
dir, err := ioutil.TempDir(os.TempDir(), app.AppName+"-")
if err != nil {
return err
}
app.Config = NewConfig(dir + "/config.json")
} else {
app.Config, err = FindConfig(app.AppName, app.ModuleName)
}
} else {
app.Config, err = OpenConfig(*configPath, true)
}
if err != nil {
return err
}
app.Log.Debugf("[core] reading config from '%s'", app.Config.Path())
return nil
}
func (app *App) InitClientFactory() (err error) {
cfg := sfproto.NetConfig{}
app.Config.Get("dial", &cfg)
app.WriteConfig()
if cfg.Address == "" {
cfg.Address = os.Getenv("SARIF_QUEUE_URL")
}
u, err := url.Parse(cfg.Address)
if err != nil {
return err
}
if u.Scheme == "amqp" {
app.ClientFactory = amqp.NewClientFactory(cfg)
} else {
app.ClientFactory = sfproto.NewClientFactory(cfg)
}
return nil
}
func (app *App) WriteConfig() {
if app.Config.IsModified() {
app.Log.Infof("[core] writing config to '%s'", app.Config.Path())
app.Must(app.Config.Write())
}
}
func (app *App) Close() {
if app.ModuleName == "temp" {
app.Must(os.RemoveAll(app.Config.Dir()))
} else {
app.WriteConfig()
}
}
func (app *App) Must(err error) {
if err != nil {
app.Log.Fatalln(err)
}
}
func (app *App) ClientDial(ci sarif.ClientInfo) (sarif.Client, error) {
if app.ClientFactory == nil {
if err := app.InitClientFactory(); err != nil {
return nil, err
}
}
return app.ClientFactory.NewClient(ci)
}
func WaitUntilInterrupt() {
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
<-ch
}