Commit 8866dfa

Richard Luby <richluby@gmail.com>
2016-10-27 10:21:49
added command framework
commands stored in array. command stores reference to function to execute command operations. function should be self-sufficient given an array of string arguments
1 parent 5c45326
Changed files (1)
client.go
@@ -3,6 +3,7 @@ package main
 import (
 	"encoding/json"
 	"flag"
+	"fmt"
 	"github.com/BurntSushi/toml"
 	"io/ioutil"
 	"log"
@@ -63,14 +64,28 @@ func getRecordFromServer(client *http.Client, config CONFIG) (Record, error) {
 
 // initUserSession starts the interactive prompt for the user
 func initUserSession(config CONFIG) {
-	client := &http.Client{}
-	var record Record
-	var err error
-	record, err = getRecordFromServer(client, config)
-	if err != nil {
-		log.Printf("Error for request: %+v", err)
+	//client := &http.Client{}
+	fmt.Printf("%s", "Welcome to the question interface. Use 'help' for more information.")
+	for {
+		var input string
+		var commandType int
+		var err error
+		fmt.Printf("\n:> ")
+		fmt.Scanln(&input)
+		args := strings.Fields(input)
+		if commandType, err = convertToiota(args[0]); err != nil {
+			log.Printf("Error while parsing: %+v", err)
+			continue
+		}
+		// loop through available commands and run the correct one
+		for _, command := range commandArray {
+			if command.Command == commandType {
+				if err = command.Run(args[1:]); err != nil {
+					log.Printf("Error while executing: %+v", err)
+				}
+			}
+		}
 	}
-	log.Printf("Record: %+v", record)
 }
 
 // initializes the client and handles the user interaction