-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
executable file
·209 lines (197 loc) · 5.81 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"github.com/fifthsegment/dslreports"
"os"
"fmt"
"github.com/codegangsta/cli"
"strconv"
"time"
)
// A struct that contains all the runtime information needed to perform and compile results
// of the speedtest
var R *dslr.DslrAppRuntime
// initializes the commandline client, things that are done in initialization:
// display a nice title for the command line utility
// registers all functions defined in the dslr package
func initialize() {
APPversion := 0.1;
APPDate := "2016-05-30"
println("DSLReports.com CLI v" + strconv.FormatFloat(APPversion, 'f', 1, 64) + " - " + APPDate)
R = &dslr.DslrAppRuntime{}
R.AppVersion = APPversion;
dslr.Init(R)
M := dslr.Dslrmethod{Name: "VerifyClient", Action: dslr.VerifyClient}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "SetupClient", Action: dslr.SetupClient}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "PerformSpeedTest", Action: dslr.PerformSpeedTest}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "StartSpeedTestDownload", Action: dslr.PerformDownloadSpeedTest}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "StartSpeedTestUpload", Action: dslr.PerformUploadSpeedTestAlpha}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "Cleanup", Action: dslr.Cleanup}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "Results", Action: dslr.OutputResults}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "APIauth", Action: dslr.APIauth}
dslr.Register(R, M)
M = dslr.Dslrmethod{Name: "APIservers", Action: dslr.APIservers}
dslr.Register(R, M)
// Register Alpha features
M = dslr.Dslrmethod{Name: "PerformUploadSpeedTestAlpha", Action: dslr.PerformUploadSpeedTestAlpha}
dslr.Register(R, M);
M = dslr.Dslrmethod{Name: "PushResultstoServer", Action: dslr.PushResultstoServer };
dslr.Register(R, M);
}
// registers all required flags and their respective handler variables in the runtime
func registerFlags(app *cli.App){
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "Enables debug mode",
},
cli.StringFlag{
Name: "output, o",
Usage: "Specify type of output . 'json' and 'csv' are currently supported.",
Value: "default",
Destination: &R.UserFlags.Output,
},
cli.StringFlag{
Name: "down",
Value: "12",
Usage: "Number of streams to use for download",
Destination: &R.UserFlags.DownloadStreams,
},
cli.StringFlag{
Name: "up",
Value: "12",
Usage: "Number of streams to use for up",
Destination: &R.UserFlags.UploadStreams,
},
}
}
// this function registers all commands with their respective handler functions in the runtime.
// we've restricted the client to only run commands that have been registered in the innitialize phase
// this allows for a modular architecture, if you don't like an implementation of a function simply
// register another one in the initialize phase then call it here using the dslr.Run() call.
func registerCommands(app *cli.App){
app.Commands = []cli.Command{
{
Name: "setup",
Aliases: []string{"s"},
Usage: "setup the client",
Action: func(c *cli.Context) {
dslr.Run(R, "SetupClient")
},
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "run the test",
SkipFlagParsing: false,
Flags: []cli.Flag{
cli.BoolFlag{Name: "debug, d"},
},
Action: func(c *cli.Context) error {
c.Command.VisibleFlags()
if c.Bool("debug") {
R.DebugEnabled = true;
fmt.Println("Debug mode enabled");
}
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "StartSpeedTestDownload")
dslr.Run(R, "StartSpeedTestUpload")
dslr.Run(R, "PushResultstoServer")
dslr.Run(R, "Results")
return nil;
},
},
{
Name: "upload",
Aliases: []string{"u"},
Usage: "run the test",
Action: func(c *cli.Context) error {
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "StartSpeedTestUpload")
return nil;
},
},
{
Name: "download",
Aliases: []string{"u"},
Usage: "run the test",
Action: func(c *cli.Context) error {
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "StartSpeedTestDownload")
return nil;
},
},
{
Name: "uploaddemo",
Aliases: []string{"u"},
Usage: "run the test",
Flags: []cli.Flag{
cli.BoolFlag{Name: "debug, d"},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
R.DebugEnabled = true;
fmt.Println("Debug mode enabled");
}
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "PerformUploadSpeedTestAlpha")
return nil;
},
},
{
Name: "testpush",
Aliases: []string{"u"},
Usage: "test push results to mothership",
Flags: []cli.Flag{
cli.BoolFlag{Name: "debug, d"},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
R.DebugEnabled = true;
fmt.Println("Debug mode enabled");
}
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "StartSpeedTestDownload")
dslr.Run(R, "StartSpeedTestUpload")
dslr.Run(R, "PushResultstoServer")
dslr.Run(R, "Results")
return nil;
},
},
}
}
// main
func main() {
initialize()
app := cli.NewApp()
app.Name = "Dslrcli"
app.Usage = "Test network speed"
app.Compiled = time.Now()
app.Version = strconv.FormatFloat(R.AppVersion, 'f', 1, 64)
app.Authors = []cli.Author{
cli.Author{
Name: "Abdullah Irfan",
Email: "[email protected]",
},
}
registerFlags(app);
app.Action = func(c *cli.Context) error {
dslr.Run(R, "APIauth")
dslr.Run(R, "APIservers")
dslr.Run(R, "StartSpeedTestUpload")
return nil;
}
registerCommands(app);
app.Run(os.Args)
}