Commit 35871ec

Richard Luby <richluby@gmail.com>
2016-10-26 13:54:36
client now loads configuration
client can read file and load information from a configuration file default configuration is 127.0.0.1:8080
1 parent 47bcd6f
client.go
@@ -1,6 +1,46 @@
 package main
 
+import (
+	"flag"
+	"github.com/BurntSushi/toml"
+	"log"
+	"os"
+	"strings"
+)
+
+// CONFIG determines how the client is configured
+type CONFIG struct {
+	// SERVER is the IP or domain address of the server
+	SERVER string
+	// PORT is the port on which to connect
+	PORT int
+	// USE_HTTPS determines if the client should connect using HTTPS
+	USE_HTTPS bool
+}
+
+// loadClientConfiguration loads the configuration for the client using the given string
+func loadClientConfiguration(cfgFile string) CONFIG {
+	log.Printf("Loading configuration from %s.", cfgFile)
+	var config CONFIG
+	config.SERVER = "127.0.0.1"
+	config.PORT = 8080
+	config.USE_HTTPS = false
+	_, err := toml.DecodeFile(cfgFile, &config)
+	if err != nil {
+		log.Fatalf("Could not read configuration file: %+v\n", err.Error())
+		os.Exit(EXIT_CODE.FILE_IO_ERROR)
+	}
+	return config
+}
+
 // initializes the client and handles the user interaction
 func main() {
-
+	filePath := flag.String("file", "", "defines a path to the configuration file")
+	flag.Parse()
+	var config CONFIG
+	if strings.Compare(*filePath, "") != 0 {
+		config = loadClientConfiguration(*filePath)
+	}
+	log.Printf("Configuration: %+v", config)
+	initUserSession(config)
 }
questioner.go
@@ -15,6 +15,22 @@ import (
 	"time"
 )
 
+// CONFIG defines default for the server
+type CONFIG struct {
+	// LISTEN_ADDRESS defines the local address on which to listen
+	LISTEN_ADDRESS string
+	//LISTEN_PORT defines the the port on which to listen
+	LISTEN_PORT int
+	// PERMIT_BLANK_PASSWORD determines if a password should be provided with the user names
+	PERMIT_BLANK_PASSWORD bool
+	// PRIVATE_KEY defines the path to the server's private key for signing https connections
+	PRIVATE_KEY string
+	// USE_HTTPS determines if the server should use HTTPS
+	USE_HTTPS bool
+	// QUESTIONS contains the path to the questions directory
+	QUESTIONS string
+}
+
 // defaultConfig is used when no configuration file is given
 const defaultConfig = `
 LISTEN_ADDRESS = "127.0.0.1"
@@ -28,9 +44,9 @@ QUESTIONS = "/path/to/questions"
 // Contains the questions from which to pull
 var records []Record
 
-//LoadConfiguration loads the configuration file
+//loadServerConfiguration loads the configuration file
 // cfgFile : the file that contains the configuration
-func LoadConfiguration(cfgFile string) CONFIG {
+func loadServerConfiguration(cfgFile string) CONFIG {
 	var contents string
 	var config CONFIG
 	if cfgFile == "" {
@@ -82,7 +98,7 @@ func main() {
 	flag.StringVar(&confFile, "file", "", "define a specific configuration file to read")
 	flag.StringVar(&confFile, "f", "", "define a specific configuration file to read")
 	flag.Parse()
-	config := LoadConfiguration(confFile)
+	config := loadServerConfiguration(confFile)
 	if err = filepath.Walk(config.QUESTIONS, loadRecords); err != nil {
 		log.Printf("Failed to load questions due to error: %+v", err)
 		os.Exit(EXIT_CODE.FILE_IO_ERROR)
structures.go
@@ -13,22 +13,6 @@ var EXIT_CODE = EXIT_CODES{
 	FILE_IO_ERROR:    2,
 	NETWORK_IO_ERROR: 3}
 
-// CONFIG defines default for the server
-type CONFIG struct {
-	// LISTEN_ADDRESS defines the local address on which to listen
-	LISTEN_ADDRESS string
-	//LISTEN_PORT defines the the port on which to listen
-	LISTEN_PORT int
-	// PERMIT_BLANK_PASSWORD determines if a password should be provided with the user names
-	PERMIT_BLANK_PASSWORD bool
-	// PRIVATE_KEY defines the path to the server's private key for signing https connections
-	PRIVATE_KEY string
-	// USE_HTTPS determines if the server should use HTTPS
-	USE_HTTPS bool
-	// QUESTIONS contains the path to the questions directory
-	QUESTIONS string
-}
-
 // Record stores information related to a single record
 type Record struct {
 	Question,