Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SophieKAn committed Aug 12, 2016
1 parent f844d0f commit 9093185
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
34 changes: 17 additions & 17 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func interfaceToList(cfgfile map[string]interface{}, name string) []map[string]i
return groupList
}

// getEnvars
//
// getEnvars creates a map of all the necessary environment variables for the
// program.
func getEnVars() map[string]string {
envMap := make(map[string]string)

Expand All @@ -136,8 +136,10 @@ func getEnVars() map[string]string {
return envMap
}

// getConfigfile
//
// getConfigfile gets passed the name for the config file that was or wasn't
// given on the command line. If there wasn't one there it check environment
// variables, then default linux and freeBSD path. If it can't find a config
// file, the program will not proceed.
func getConfigfile(filename interface{}) string {
var config string

Expand All @@ -150,15 +152,15 @@ func getConfigfile(filename interface{}) string {
} else if _, err := os.Stat(freeBSDConfigPath); err == nil {
config = freeBSDConfigPath
} else {
fmt.Println("This program requires a config file to run. See documentation")
fmt.Println("This program requires a config file to run. Please refer to documentation.")
os.Exit(1)
}

return config
}

// bindArg
//
// bindArg parses the command-line argument for binding interface to port,
// then returns the acquired interface and port.
func bindArg(input interface{}) (string, string) {
var interf, port string

Expand All @@ -170,8 +172,8 @@ func bindArg(input interface{}) (string, string) {
return interf, port
}

// intervalArg
//
// intervalArg interpretes the command-line interval argument if there is one.
// If not, it returns zero.
func intervalArg(input interface{}) time.Duration {
var interval time.Duration

Expand All @@ -183,8 +185,8 @@ func intervalArg(input interface{}) time.Duration {
return interval
}

// splitInterfacePort
//
// splitInterfacePort takes a string taken from the command line or an
// environment variable and splits it using a regex.
func splitInterfacePort(inputString string) (string, string) {
var interf, port string

Expand All @@ -207,8 +209,6 @@ func splitInterfacePort(inputString string) (string, string) {
return interf, port
}

// mapSubexpNames
//
func mapSubexpNames(m, n []string) map[string]string {
/* http://stackoverflow.com/a/30483899/6279238 */
/* Code found in comment on main answer */
Expand All @@ -220,8 +220,8 @@ func mapSubexpNames(m, n []string) map[string]string {
return r
}

// getTimeInterval
//
// getTimeInterval finds a time.Duration depending on the number and time units
// given on the command line or from environment variables.
func getTimeInterval(intervalString string) time.Duration {
var interval time.Duration

Expand All @@ -238,8 +238,8 @@ func getTimeInterval(intervalString string) time.Duration {
return interval
}

// stringToTime
//
// stringToTime takes the string representing the update interval and converts
// it into a time.Duration type.
func stringToTime(intervalString string, timeUnit string) time.Duration {
durationString := strings.TrimSuffix(intervalString, timeUnit)
duration, err := strconv.Atoi(durationString)
Expand Down
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Machine struct {
Status string `json:"status"`
}

// runServer takes the config struct. It runs a hub, starts the server, and
// continually updates the status of all the machines, then broadcasting those
// changes to all connected clients in the hub.
func runServer(config *Config) {
/* > Check for debug mode */
debugMode(config)
Expand Down Expand Up @@ -63,8 +66,10 @@ func runServer(config *Config) {
}
}

// serveUpdates responds to a websocket connection by creating a 'client',
// sending said client to the hub, sending it the set of all machines, and
// finally calling writePump().
func serveUpdates(hub *Hub, allMachines []*Machine, w http.ResponseWriter, r *http.Request) {
/* > Open the websocket connections. */
ws, err := upgrader.Upgrade(w, r, nil)
check(err)
defer ws.Close()
Expand All @@ -78,6 +83,8 @@ func serveUpdates(hub *Hub, allMachines []*Machine, w http.ResponseWriter, r *ht
client.writePump()
}

// debugMode checks the config to see if Debug is true, and if so prints
// the current settings.
func debugMode(config *Config) {
if config.Debug {
fmt.Printf("interface: %s\n", config.Interface)
Expand Down
10 changes: 5 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"os"
)

// check takes an error, and prints the error if it isn't nil.
// check takes an error, and prints the error if it isn't nil, then exits.
func check(e error) {
if e != nil {
log.Printf("Error: %v\n", e)
os.Exit(1)
}
}

// GetConfig takes the name of the configuration file (currently "config.json")
// and attempts to open/read file then unmarshal it into a list of interfaces.
// getConfig takes the name of the configuration file, attempts to open the
// file and unmarshal it into a map of strings to interfaces.
func getConfig(fileName string) map[string]interface{} {

configFile, err := ioutil.ReadFile(fileName)
Expand All @@ -34,8 +34,8 @@ func getConfig(fileName string) map[string]interface{} {
return configuration
}

// GetMachines takes the unmarshalled config.json and constructs a slice of
// pointers to Machine structs representing all the machines indicated in the
// getMachines takes the unmarshalled "machineRanges" list and constructs a
// slice of pointers to Machine structs representing all the machines from the
// config.
func getMachines(labs []map[string]interface{}) []*Machine {
var allMachines []*Machine
Expand Down

0 comments on commit 9093185

Please sign in to comment.