about summary refs log tree commit diff
path: root/shell/shell.go
blob: 1b629df321d50ccf2ee5be6034e96fa4895da2d8 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main

import (
	"bytes"
	"encoding/csv"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"strings"

	"github.com/marcusolsson/tui-go"

	"git.darknebu.la/GalaxySimulator/db-container/shell/structs"
)

func main() {
	parameters := structs.NewEnv("localhost:8042", "data.csv", 10, 0)
	boundary := structs.NewBoundary(0, 0, 4000)

	// define the history box displayed in the top part of the window
	history := tui.NewVBox()
	historyScroll := tui.NewScrollArea(history)
	historyScroll.SetAutoscrollToBottom(true)
	historyBox := tui.NewVBox(historyScroll)
	historyBox.SetBorder(true)

	// define the input bar displayed on the bottom window edge
	input := tui.NewEntry()
	input.SetFocused(true)
	input.SetSizePolicy(tui.Expanding, tui.Maximum)
	inputBox := tui.NewHBox(input)
	inputBox.SetBorder(true)
	inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)

	// define a root container containing all the containers and maximizing them in the given space
	root := tui.NewVBox(historyBox, inputBox)
	root.SetSizePolicy(tui.Expanding, tui.Expanding)

	// on submission of the input box, add the text to the history box
	input.OnSubmit(func(e *tui.Entry) {

		// add the input to the history
		history.Append(tui.NewHBox(tui.NewLabel(e.Text())))
		handleInput(history, e.Text(), parameters, boundary)
		input.SetText("")
	})

	ui, err := tui.New(root)
	if err != nil {
		panic(err)
	}

	ui.SetKeybinding("Esc", func() { ui.Quit() })

	if err := ui.Run(); err != nil {
		panic(err)
	}
}

func faultyInput(history *tui.Box) {
	// print an error message if the input cannot be assigned to an action
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-30s", "", "Faulty Input!")),
		),
	)
}

func printEnvironment(history *tui.Box, parameters *structs.Env) {
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30s", "", "url", parameters.Url())),
		),
	)
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30s", "", "data", parameters.Data())),
		),
	)
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30d", "", "amount", parameters.Amount())),
		),
	)
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30d", "", "treeindex", parameters.Treeindex())),
		),
	)
}

func printBoundary(history *tui.Box, boundary *structs.Boundary) {
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30d", "", "x", boundary.X())),
		),
	)
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30d", "", "y", boundary.Y())),
		),
	)
	history.Append(
		tui.NewHBox(
			tui.NewLabel(fmt.Sprintf("%10s%-15s%-30d", "", "width", boundary.Width())),
		),
	)
}

func setEnvironment(history *tui.Box, parameters *structs.Env, key string, value string) {
	switch key {
	case "url":
		parameters.SetUrl(value)
		break
	case "data":
		parameters.SetData(value)
		break
	case "amount":
		valueInt, _ := strconv.ParseInt(value, 10, 64)
		parameters.SetAmount(valueInt)
		break
	case "treeindex":
		valueInt, _ := strconv.ParseInt(value, 10, 64)
		parameters.SetTreeindex(valueInt)
		break
	default:
		faultyInput(history)
	}
}

func setBoundary(history *tui.Box, boundary *structs.Boundary, key string, value string) {
	switch key {
	case "x":
		valueInt, _ := strconv.ParseInt(value, 10, 64)
		boundary.SetX(valueInt)
		break
	case "y":
		valueInt, _ := strconv.ParseInt(value, 10, 64)
		boundary.SetY(valueInt)
		break
	case "width":
		valueInt, _ := strconv.ParseInt(value, 10, 64)
		boundary.SetWidth(valueInt)
		break
	default:
		faultyInput(history)
	}
}

func handleInput(history *tui.Box, input string, parameters *structs.Env, boundary *structs.Boundary) {
	if input == "help" {
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-35s%-30s", "", "print <env|boundary|all>", "print the according local values")),
			),
		)
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-35s%-30s", "", "set <env|boundary> [KEY] [VALUE]", "set the local values")),
			),
		)
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-35s%-30s", "", "new", "Create a new tree using the values defined in env and boundary")),
			),
		)
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-35s%-30s", "", "<ESC>", "quit")),
			),
		)
		return
	}

	if strings.HasPrefix(input, "print") {
		key := ""

		if len(input) > 6 {
			key = strings.Split(input[6:], " ")[0]
		} else {
			history.Append(
				tui.NewHBox(
					tui.NewLabel(fmt.Sprintf("%10s%-35s", "", "[env|bounadry|all]")),
				),
			)
			return
		}

		switch key {
		case "env":
			printEnvironment(history, parameters)
			break
		case "boundary":
			printBoundary(history, boundary)
			break
		case "all":
			printEnvironment(history, parameters)
			printBoundary(history, boundary)
			break
		default:
		}
		return
	}

	// test if the input string starts with 'set'
	if strings.HasPrefix(input, "set") {
		// trim of the "set" off the string
		parameter := strings.Split(input[4:], " ")[0]
		key := strings.Split(input[4:], " ")[1]
		value := strings.Split(input[4:], " ")[2]

		switch parameter {
		case "env":
			setEnvironment(history, parameters, key, value)
			break
		case "boundary":
			setBoundary(history, boundary, key, value)
			break
		}

		return
	}

	if input == "new" {
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-30s", "", "Creating a new tree")),
			),
		)

		// Generate the request url
		requestUrl := fmt.Sprintf("http://%s/new", parameters.Url())

		// Bundle the post request data
		data := []byte(fmt.Sprintf("x=%d&y=%d&w=%d", boundary.X(), boundary.Y(), boundary.Width()))

		// make the new request the the endpoint defined above using the data bundled above
		req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(data))
		if err != nil {
			log.Fatal("Error reading request.", err)
		}

		// Send the request
		client := http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			log.Fatal("Error reading response.", err)
		}

		// return the http response code
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-30s", "", resp.Status)),
			),
		)
		return
	}

	if input == "insert" {
		infoMessage := fmt.Sprintf("Inserting %d stars from %s into tree Nr. %d defined in %s.", parameters.Amount(), parameters.Data(), parameters.Treeindex(), parameters.Url())
		history.Append(
			tui.NewHBox(
				tui.NewLabel(fmt.Sprintf("%10s%-30s", "", infoMessage)),
			),
		)

		// open parameters.Data()
		dat, err := ioutil.ReadFile(parameters.Data())
		if err != nil {
			log.Fatal("Error reading file")
		}

		// parse the data using a csv reader
		csvData := csv.NewReader(strings.NewReader(string(dat)))

		for i := 0; i < int(parameters.Amount()); i++ {
			record, err := csvData.Read()
			if err == io.EOF {
				break
			}
			if err != nil {
				log.Fatal("Error Reading the csv data: ", err)
			}

			history.Append(
				tui.NewHBox(
					tui.NewLabel(fmt.Sprintf("%10s%-30s", "", record)),
				),
			)

			// Generate the request url
			requestUrl := fmt.Sprintf("http://%s/insert/%d", parameters.Url(), parameters.Treeindex())

			history.Append(
				tui.NewHBox(
					tui.NewLabel(fmt.Sprintf("%10s%-30s", "requesturl: ", requestUrl)),
				),
			)

			x, _ := strconv.ParseFloat(record[0], 64)
			y, _ := strconv.ParseFloat(record[0], 64)

			// Bundle the post request data
			data := []byte(fmt.Sprintf("x=%f&y=%f&vx=0&vy=0&m=10", x, y))

			// make the new request the the endpoint defined above using the data bundled above
			req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(data))
			if err != nil {
				log.Fatal("Error reading request.", err)
			}

			// Send the request
			client := http.Client{}
			resp, err := client.Do(req)
			if err != nil {
				log.Fatal("Error reading response.", err)
			}

			// return the http response code
			history.Append(
				tui.NewHBox(
					tui.NewLabel(fmt.Sprintf("%10s%-30s", "", resp.Status)),
				),
			)
		}

		return
	}

	faultyInput(history)
}