-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
124 lines (115 loc) · 4.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"crypto/tls"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"github.com/namsral/flag"
"github.com/bugfender/bugfender-integration-elasticsearch/pkg/dummy"
"github.com/bugfender/bugfender-integration-elasticsearch/pkg/elasticsearch"
"github.com/bugfender/bugfender-integration-elasticsearch/pkg/integration"
)
func main() {
var (
clientID string
clientSecret string
apiURL string
appID int64
esIndex string
esNodes string
esUsername, esPassword string
consoleOutput bool
stateFile string
insecureSkipTLSVerify bool
verbose bool
retries uint
)
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
// Bugfender parameters
flag.StringVar(&clientID, "client-id", "", "OAuth client ID to connect to Bugfender (mandatory)")
flag.StringVar(&clientSecret, "client-secret", "", "OAuth client secret to connect to Bugfender (mandatory)")
flag.Int64Var(&appID, "app-id", 0, "Bugfender app ID (mandatory)")
flag.StringVar(&apiURL, "api-url", "https://dashboard.bugfender.com", "Bugfender API URL (only necessary for on-premises)")
// Elasticsearch parameters
flag.StringVar(&esIndex, "es-index", "", "Elasticsearch index to write to (default: logs)")
flag.StringVar(&esNodes, "es-nodes", "", "List of Elasticsearch nodes (multiple nodes can be specified, separated by spaces)")
flag.StringVar(&esUsername, "es-username", "", "Username to connect to Elasticsearch")
flag.StringVar(&esPassword, "es-password", "", "Password to connect to Elasticsearch")
// Console output
flag.BoolVar(&consoleOutput, "console-output", false, "Print logs to console instead of Elasticsearch (for debugging)")
// other
flag.StringVar(&stateFile, "state-file", "", "File to restore and save state, to resume sync (recommended)")
flag.BoolVar(&insecureSkipTLSVerify, "insecure-skip-tls-verify", false, "Skip TLS certificate verification (insecure)")
flag.BoolVar(&verbose, "verbose", false, "Verbose messages")
flag.UintVar(&retries, "retries", 10, "Number of times to retry on errors before exiting. 0 = never give up.")
flag.Parse()
// parameter validation
if clientID == "" || clientSecret == "" || appID == 0 {
flag.Usage()
os.Exit(1)
}
parsedURL, err := url.Parse(apiURL)
if err != nil {
log.Fatal("invalid apiurl:", err)
}
if insecureSkipTLSVerify {
// #nosec G402 this is intended, user specified -insecure-skip-tls-verify flag
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
// connect to Bugfender
state, err := ioutil.ReadFile(stateFile) // #nosec G304 user intends to load this file
if perr, ok := err.(*os.PathError); ok && perr.Err.(syscall.Errno) == syscall.ENOENT {
// missing file, ignore
} else if err != nil {
log.Fatal("can not open state file:", err)
}
bf, err := integration.NewBugfenderClient(&integration.Config{
OAuthClientID: clientID,
OAuthClientSecret: clientSecret,
ApiUrl: parsedURL,
}, appID, state)
if err != nil {
log.Fatal("error initializing Bugfender client", err)
}
var destination integration.LogWriter
if consoleOutput {
destination = dummy.NewConsoleDestination()
}
// connect to Elasticsearch
if esIndex != "" && esNodes != "" {
destination, err = elasticsearch.NewClient(esIndex, strings.Split(esNodes, " "), esUsername, esPassword)
if err != nil {
log.Fatal("error initializing Elasticsearch client:", err)
}
}
if destination == nil {
log.Fatal("No destination specified")
}
// run integration
i, err := integration.New(bf, destination, verbose, stateFile)
if err != nil {
log.Fatal("error initializing integration:", err)
}
// trap SIGINT to trigger a shutdown.
ctx, cancelFunc := context.WithCancel(context.Background())
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-exitSignal
log.Println("")
if verbose {
log.Println("- Ctrl+C pressed in Terminal, closing")
}
cancelFunc()
}()
err = i.Sync(ctx, retries)
if ctx.Err() != context.Canceled && err != nil {
log.Fatal(err)
}
}