-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
60 lines (53 loc) · 1.19 KB
/
context.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
package generic
import (
"context"
"github.com/dgraph-io/badger"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"time"
)
type Context struct {
*internalContext
}
type internalContext struct {
Name string
LogsDir string
BadgerDir string
Debug bool
Log *LogHander
}
func NewCliContext() context.Context {
c, _ := context.WithTimeout(context.Background(), 15*time.Second)
return c
}
func NewContext(name string) (*Context, error) {
ctx := Context{&internalContext{}}
ctx.Name = name
ctx.LogsDir = "/var/log/openshift-clusters-pools"
ctx.BadgerDir = "/tmp/badger"
badgerenv := os.Getenv("BADGER_DIR")
debugenv := os.Getenv("DEBUG")
if len(badgerenv) > 0 {
ctx.BadgerDir = badgerenv
}
if len(debugenv) > 0 && debugenv == "true" {
ctx.Debug = true
}
ctx.Log = NewLogger(name, ctx.Debug)
return &ctx, nil
}
func (c Context) NewBadgerConnection() (*badger.DB, error) {
// todo make it log to a file instead
bo := badger.DefaultOptions(c.BadgerDir)
if !c.Debug {
emptyLogger := logrus.New()
emptyLogger.Out = ioutil.Discard
bo = bo.WithLogger(emptyLogger)
}
db, err := badger.Open(bo)
if err != nil {
return nil, err
}
return db, nil
}