-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (132 loc) · 3.76 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"errors"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/rs/zerolog/log"
"github.com/runar-rkmedia/gabyoall/auth"
"github.com/runar-rkmedia/gabyoall/cmd"
"github.com/runar-rkmedia/gabyoall/logger"
"github.com/runar-rkmedia/gabyoall/printer"
"github.com/runar-rkmedia/gabyoall/requests"
"github.com/runar-rkmedia/gabyoall/utils"
"github.com/runar-rkmedia/gabyoall/worker"
)
// TODO: Reread token every minute? In case of short-lived tokens.
// For instance, if they are read from env-variables, the user could set them.
type TemplateVars struct {
cmd.Config
}
func main() {
err := cmd.Execute(func() {
err := cmd.InitConfig()
if err != nil {
panic(err)
}
})
if err != nil {
os.Exit(1)
}
config := cmd.GetConfig(logger.GetLogger("initial"))
// TODO: Refactor so this is a bit more general. (but still support graphql)
var query = requests.Request{
Body: config.Body,
Query: config.Query,
Variables: config.Variables,
OperationName: config.OperationName,
Headers: config.Header,
Method: config.Method,
}
logger.InitLogger(logger.LogConfig{
Level: config.LogLevel,
Format: config.LogFormat,
WithCaller: true,
})
l := logger.GetLogger("main")
if config.RequestCount == 0 {
l.Fatal().Msg("Request-count cannot be 0")
}
if query.Query == "" && query.Body == "" {
l.Fatal().Interface("query", query).Msg("Missing query/body")
}
if config.Auth.HeaderKey == "" {
config.Auth.HeaderKey = "Authorization"
}
vars := TemplateVars{*config}
// TODO: Clean the path
outputPath := config.Output
if outputPath != "" {
outputPath = utils.RunTemplating(l, config.Output, "output", vars)
err := os.MkdirAll(path.Dir(outputPath), 0755)
if err != nil {
l.Fatal().Err(err).Str("dir", path.Dir(outputPath)).Msg("Failed to create directories for output")
}
}
token := utils.RunTemplating(l, config.AuthToken, "token", vars)
var tokenPayload *auth.TokenPayload
var validityStringer printer.ValidityStringer
if token == "" {
err, token, tokenPayload, validityStringer = auth.Retrieve(l, config.Auth)
if err != nil {
l.Fatal().Err(err).Msg("Failed to perform authentication")
}
}
out, err := cmd.NewOutput(l, outputPath, config.Url, query, tokenPayload.Raw)
if err != nil {
l.Fatal().Err(err).Msg("Failed to set up output")
}
l.Info().Str("path", out.GetPath()).Msg("Will write output to path:")
ts := requests.NewTimeSeriesWithLabel(time.Now())
endpoint := requests.NewEndpoint(logger.GetLogger("gql"), config.Url, &ts)
authPrefix := ""
if strings.ToLower(config.Auth.Kind) == "bearer" {
authPrefix = "Bearer "
}
endpoint.Headers.Add(config.Auth.HeaderKey, authPrefix+token)
l.Info().Str("url", config.Url).Str("operationName", query.OperationName).Int("count", config.RequestCount).Int("paralism", config.Concurrency).Msg("Running requests with paralism")
SetupCloseHandler(func(signal os.Signal) {
out.Write()
})
wt := worker.WorkThing{}
ch, quit := wt.Run(endpoint, *config, query)
successes := 0
startTime := time.Now()
print := printer.NewPrinter(
*config,
validityStringer,
query.OperationName,
out,
startTime,
)
print.Animate()
for i := 0; i < config.RequestCount; i++ {
stat := <-ch
if stat.ErrorType == "" {
successes++
}
out.AddStat(stat)
print.Update(i, successes)
}
out.CalculateStats()
print.Complete(config.RequestCount, successes)
err = out.Write()
if err != nil {
l.Fatal().Err(errors.Unwrap(err)).Msg("Failed to write output")
}
close(quit)
l.Info().Msg("All done")
}
func SetupCloseHandler(f func(signal os.Signal)) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
signal := <-c
log.Warn().Msg("- Ctrl+C pressed in Terminal")
f(signal)
os.Exit(0)
}()
}