diff options
author | Emile <hanemile@protonmail.com> | 2019-02-09 18:57:58 +0100 |
---|---|---|
committer | Emile <hanemile@protonmail.com> | 2019-02-09 18:57:58 +0100 |
commit | 4904db304d33a6a2d7ede4d8097f31f55bc1c0c1 (patch) | |
tree | f392a83939092fbf909edcd12c0cb1bfa5d967db /shell | |
parent | 581710da6c42a2f37966e6f3401e09e780d0d3a0 (diff) |
removed the shell, it's just not necessary...
Diffstat (limited to 'shell')
-rw-r--r-- | shell/shell.go | 334 | ||||
-rw-r--r-- | shell/structs/boundary.go | 36 | ||||
-rw-r--r-- | shell/structs/env.go | 43 |
3 files changed, 0 insertions, 413 deletions
diff --git a/shell/shell.go b/shell/shell.go deleted file mode 100644 index 1b629df..0000000 --- a/shell/shell.go +++ /dev/null @@ -1,334 +0,0 @@ -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) -} diff --git a/shell/structs/boundary.go b/shell/structs/boundary.go deleted file mode 100644 index b49c7a2..0000000 --- a/shell/structs/boundary.go +++ /dev/null @@ -1,36 +0,0 @@ -package structs - -type Boundary struct { - // boundary box values - x int64 - y int64 - width int64 -} - -func NewBoundary(x int64, y int64, width int64) *Boundary { - return &Boundary{x: x, y: y, width: width} -} - -func (b *Boundary) Width() int64 { - return b.width -} - -func (b *Boundary) SetWidth(width int64) { - b.width = width -} - -func (b *Boundary) Y() int64 { - return b.y -} - -func (b *Boundary) SetY(y int64) { - b.y = y -} - -func (b *Boundary) X() int64 { - return b.x -} - -func (b *Boundary) SetX(x int64) { - b.x = x -} diff --git a/shell/structs/env.go b/shell/structs/env.go deleted file mode 100644 index 949460f..0000000 --- a/shell/structs/env.go +++ /dev/null @@ -1,43 +0,0 @@ -package structs - -// Env structure storing the variables used to define what stars are inserted where -type Env struct { - // general values - url string - data string - amount int64 - treeindex int64 -} - -func NewEnv(url string, data string, amount int64, treeindex int64) *Env { - return &Env{url: url, data: data, amount: amount, treeindex: treeindex} -} - -// Getters and setters -func (e *Env) Treeindex() int64 { - return e.treeindex -} -func (e *Env) SetTreeindex(treeindex int64) { - e.treeindex = treeindex -} - -func (e *Env) Amount() int64 { - return e.amount -} -func (e *Env) SetAmount(amount int64) { - e.amount = amount -} - -func (e *Env) Data() string { - return e.data -} -func (e *Env) SetData(data string) { - e.data = data -} - -func (e *Env) Url() string { - return e.url -} -func (e *Env) SetUrl(url string) { - e.url = url -} |