-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathmain.go
100 lines (87 loc) · 2.53 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
_ "net/http/pprof"
"github.com/hashicorp/go-hclog"
hPlugin "github.com/hashicorp/go-plugin"
"github.com/odpf/optimus/cmd"
"github.com/odpf/optimus/config"
_ "github.com/odpf/optimus/ext/datastore"
"github.com/odpf/optimus/models"
"github.com/odpf/optimus/plugin"
_ "github.com/odpf/optimus/plugin"
"github.com/odpf/salt/log"
)
var (
errRequestFail = errors.New("🔥 unable to complete request successfully")
)
type PlainFormatter struct{}
func (p *PlainFormatter) Format(entry *logrus.Entry) ([]byte, error) {
if len(entry.Data) > 0 {
var data string
for key, val := range entry.Data {
data += fmt.Sprintf("%s: %v ", key, val)
}
return []byte(fmt.Sprintf("%s %s\n", entry.Message, data)), nil
}
return []byte(fmt.Sprintf("%s\n", entry.Message)), nil
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
configuration, err := config.InitOptimus()
if err != nil {
fmt.Printf("ERROR: %s", err.Error())
os.Exit(1)
}
var jsonLogger log.Logger
var plainLogger log.Logger
pluginLogLevel := hclog.Info
if configuration.GetLog().Level != "" {
jsonLogger = log.NewLogrus(log.LogrusWithLevel(configuration.GetLog().Level), log.LogrusWithWriter(os.Stderr))
plainLogger = log.NewLogrus(log.LogrusWithLevel(configuration.GetLog().Level), log.LogrusWithFormatter(new(PlainFormatter)))
if strings.ToLower(configuration.GetLog().Level) == "debug" {
pluginLogLevel = hclog.Debug
}
} else {
jsonLogger = log.NewLogrus(log.LogrusWithLevel("INFO"), log.LogrusWithWriter(os.Stderr))
plainLogger = log.NewLogrus(log.LogrusWithLevel("INFO"), log.LogrusWithFormatter(new(PlainFormatter)))
}
// init telemetry
teleShutdown, err := config.InitTelemetry(jsonLogger, configuration)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
os.Exit(1)
}
defer teleShutdown()
// discover and load plugins
if err := plugin.Initialize(hclog.New(&hclog.LoggerOptions{
Name: "optimus",
Output: os.Stdout,
Level: pluginLogLevel,
})); err != nil {
hPlugin.CleanupClients()
fmt.Printf("ERROR: %s\n", err.Error())
os.Exit(1)
}
// Make sure we clean up any managed plugins at the end of this
defer hPlugin.CleanupClients()
command := cmd.New(
plainLogger,
jsonLogger,
configuration,
models.PluginRegistry,
models.DatastoreRegistry,
)
if err := command.Execute(); err != nil {
hPlugin.CleanupClients()
// no need to print err here, `command` does that already
fmt.Println(errRequestFail)
os.Exit(1)
}
}