Skip to content

Commit

Permalink
Consistent comments, better error handling move version file to root
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhiram committed Apr 9, 2018
1 parent 02d1b0f commit 3a3991a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 147 deletions.
20 changes: 10 additions & 10 deletions cmd/wol/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package main
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"os"
"path"
"sync"
Expand Down Expand Up @@ -85,26 +85,26 @@ func LoadAliases(dbpath string) (*Aliases, error) {
}

// Add updates an alias entry or adds a new alias entry. If the alias already
// exists it is just overwritten
// exists it is just overwritten.
func (a *Aliases) Add(alias, mac, iface string) error {
a.mtx.Lock()
defer a.mtx.Unlock()

// Create a buffer to store the encoded MAC, interface pair
// Create a buffer to store the encoded MAC, interface pair.
buf, err := EncodeFromMacIface(mac, iface)
if err != nil {
return err
}

// We don't have to worry about the key existing, as we will just
// update it if it is already there
// We don't have to worry about the key existing, as we will update it
// provided it exists.
return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(MainBucketName))
return bucket.Put([]byte(alias), buf.Bytes())
})
}

// Del removes an alias from the store based on the alias string
// Del removes an alias from the store based on the alias string.
func (a *Aliases) Del(alias string) error {
a.mtx.Lock()
defer a.mtx.Unlock()
Expand All @@ -115,7 +115,7 @@ func (a *Aliases) Del(alias string) error {
})
}

// Get retrieves a MacIface from the store based on an alias string
// Get retrieves a MacIface from the store based on an alias string.
func (a *Aliases) Get(alias string) (MacIface, error) {
a.mtx.Lock()
defer a.mtx.Unlock()
Expand All @@ -127,7 +127,7 @@ func (a *Aliases) Get(alias string) (MacIface, error) {
bucket := tx.Bucket([]byte(MainBucketName))
value := bucket.Get([]byte(alias))
if value == nil {
return errors.New("Alias not found in db")
return fmt.Errorf("alias (%s) not found in db", alias)
}

entry, err = DecodeToMacIface(bytes.NewBuffer(value))
Expand All @@ -136,7 +136,7 @@ func (a *Aliases) Get(alias string) (MacIface, error) {
return entry, err
}

// List returns a map containing all alias MacIface pairs
// List returns a map containing all alias MacIface pairs.
func (a *Aliases) List() (map[string]MacIface, error) {
a.mtx.Lock()
defer a.mtx.Unlock()
Expand All @@ -157,7 +157,7 @@ func (a *Aliases) List() (map[string]MacIface, error) {
return aliasMap, err
}

// Close closes the alias store
// Close closes the alias store.
func (a *Aliases) Close() error {
a.mtx.Lock()
defer a.mtx.Unlock()
Expand Down
44 changes: 19 additions & 25 deletions cmd/wol/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,38 @@ var RE_stripFnPreamble = regexp.MustCompile(`^.*\.(.*)$`)

////////////////////////////////////////////////////////////////////////////////

// Validate the DecodeToMacIface function
// Validate the DecodeToMacIface function.
func TestDecodeToMacIface(t *testing.T) {
var TestCases = []MacIface{
{"00:00:00:00:00:00", ""},
{"00:00:00:00:00:AA", "eth1"},
}

for _, entry := range TestCases {
// First encode the MacIface to a bunch of bytes
// First encode the MacIface to a bunch of bytes.
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(entry)
assert.Nil(t, err)

// Invoke the function and validate that it is equal
// to our starting MacIface
result, err := DecodeToMacIface(buf)
assert.Nil(t, err)
assert.Equal(t, entry.Mac, result.Mac)
assert.Equal(t, entry.Iface, result.Iface)
}
}

// Validate the EncodeFromMacIface function
// Validate the EncodeFromMacIface function.
func TestEncodeFromMacIface(t *testing.T) {
var TestCases = []MacIface{
{"00:00:00:00:00:00", "eth0"},
{"00:00:00:00:00:AA", ""},
}

for _, entry := range TestCases {
// First encode the MacIface to a bunch of bytes
// First encode the MacIface to a bunch of bytes.
buf, err := EncodeFromMacIface(entry.Mac, entry.Iface)
assert.Nil(t, err)

// Invoke the function and validate that it is equal
// to our starting MacIface
result, err := DecodeToMacIface(buf)
assert.Nil(t, err)
assert.Equal(t, entry.Mac, result.Mac)
Expand Down Expand Up @@ -87,19 +83,19 @@ func (suite *AliasDBTests) SetupTest() {
assert.Nil(suite.T(), err)
}

// The TearDown function closes the connection to the DB, and
// removes the temporary file created for the same
// The TearDown function closes the connection to the DB, and removes the
// temporary file created for the same.
func (suite *AliasDBTests) TearDownTest() {
// Close the connection to the bolt db
// Close the connection to the bolt db.
err := suite.aliases.Close()
assert.Nil(suite.T(), err)

// Remove the temporary test db
// Remove the temporary test db.
err = os.Remove("./" + suite.dbName)
assert.Nil(suite.T(), err)
}

// Validates the Aliases Add function
// Validates the Aliases `Add` function.
func (suite *AliasDBTests) TestAddAlias() {
var TestCases = []struct {
alias, mac, iface string
Expand All @@ -112,29 +108,29 @@ func (suite *AliasDBTests) TestAddAlias() {

entryCount := 0
for _, entry := range TestCases {
// Add the alias to the db
// Add the alias to the db.
err := suite.aliases.Add(entry.alias, entry.mac, entry.iface)
assert.Nil(suite.T(), err)
entryCount += 1

// Validate that we have "entryCount" number of aliases added
// Validate that we have "entryCount" number of aliases added.
list, err := suite.aliases.List()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), entryCount, len(list))

// Check to ensure that the current map contains the key we
// just added to the db
// just added to the db.
assert.Equal(suite.T(), entry.mac, list[entry.alias].Mac)
assert.Equal(suite.T(), entry.iface, list[entry.alias].Iface)
}
}

// Adding a duplicate entry should overwrite the original one
// Adding a duplicate entry should overwrite the original one.
func (suite *AliasDBTests) TestAddDuplicateAlias() {
err := suite.aliases.Add("test01", "00:11:22:33:44:55", "eth0")
assert.Nil(suite.T(), err)

// Validate the first entry exists
// Validate the first entry exists.
list, err := suite.aliases.List()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "00:11:22:33:44:55", list["test01"].Mac)
Expand All @@ -149,7 +145,7 @@ func (suite *AliasDBTests) TestAddDuplicateAlias() {
assert.Equal(suite.T(), "", list["test01"].Iface)
}

// Adding a duplicate entry should overwrite the original one
// Adding a duplicate entry should overwrite the original one.
func (suite *AliasDBTests) TestDeleteAlias() {
var err error
var list map[string]MacIface
Expand All @@ -159,27 +155,25 @@ func (suite *AliasDBTests) TestDeleteAlias() {
err = suite.aliases.Add("test02", "00:11:22:33:44:66", "")
assert.Nil(suite.T(), err)

// Validate that we have two items in the db
// Validate that we have two items in the db.
list, err = suite.aliases.List()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), 2, len(list))

// Remove test01
err = suite.aliases.Del("test01")
assert.Nil(suite.T(), err)
list, err = suite.aliases.List()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), 1, len(list))

// Remove test02
err = suite.aliases.Del("test02")
assert.Nil(suite.T(), err)
list, err = suite.aliases.List()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), 0, len(list))
}

// Adding a duplicate entry should overwrite the original one
// Adding a duplicate entry should overwrite the original one.
func (suite *AliasDBTests) TestGetAlias() {
var mi MacIface

Expand All @@ -202,14 +196,14 @@ func (suite *AliasDBTests) TestGetAlias() {
assert.Equal(suite.T(), entry.iface, mi.Iface)
}

// Negative test case - aliases which do not exist
// Negative test case - aliases which do not exist.
mi, err := suite.aliases.Get("foobar")
assert.NotNil(suite.T(), err)
}

////////////////////////////////////////////////////////////////////////////////

// Group up all the test suites we wish to run and dispatch them here
// Group up all the test suites we wish to run and dispatch them here.
func TestRunAllSuites(t *testing.T) {
suite.Run(t, new(AliasDBTests))
}
25 changes: 6 additions & 19 deletions cmd/wol/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package main

import (
"fmt"
"strings"

"github.com/sabhiram/go-colorize"

wol "github.com/sabhiram/go-wol"
)

////////////////////////////////////////////////////////////////////////////////

var (
// List of strings which contain allowed commands
ValidCommands = []struct {
name, description string
}{
Expand All @@ -22,7 +22,6 @@ var (
{`remove`, `removes an alias or a mac address`},
}

// List of options which wol supports
ValidOptions = []struct {
short, long, description string
}{
Expand All @@ -33,7 +32,6 @@ var (
{`i`, `interface`, `outbound interface to broadcast using`},
}

// Usage string for wol
UsageString = `Usage:
To wake up a machine:
Expand Down Expand Up @@ -66,7 +64,7 @@ Version:

////////////////////////////////////////////////////////////////////////////////

// Build a command string from the above valid ones
// Build a command string from the above valid ones.
func getAllCommands() string {
commands := ""
for _, c := range ValidCommands {
Expand All @@ -75,7 +73,7 @@ func getAllCommands() string {
return commands
}

// Build an option string from the above valid ones
// Build an option string from the above valid ones.
func getAllOptions() string {
options := ""
for _, o := range ValidOptions {
Expand All @@ -84,18 +82,7 @@ func getAllOptions() string {
return options
}

// Returns the Usage string for this application
// Returns the Usage string for this application.
func getAppUsageString() string {
return colorize.Colorize(fmt.Sprintf(UsageString, getAllCommands(), getAllOptions(), Version))
}

// Returns true if the ValidCommands struct contains an entry with the
// input string "s"
func isValidCommand(s string) bool {
for _, c := range ValidCommands {
if strings.ToLower(s) == c.name {
return true
}
}
return false
return colorize.Colorize(fmt.Sprintf(UsageString, getAllCommands(), getAllOptions(), wol.Version))
}
Loading

0 comments on commit 3a3991a

Please sign in to comment.