-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add geolocation for the 'from address' #7
- Added 'list' command to view saved locations
- Loading branch information
KyleBanks
committed
Feb 22, 2017
1 parent
dc85959
commit 509c5e1
Showing
19 changed files
with
806 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// ErrAddNameMissing is returned when running the add command the the -name arugment is missing. | ||
ErrAddNameMissing = errors.New("missing -name parameter") | ||
// ErrAddLocationMissing is returned when running the add command the the -location arugment is missing. | ||
ErrAddLocationMissing = errors.New("missing -location parameter") | ||
) | ||
|
||
// AddCmd represents a command to add a named location. | ||
type AddCmd struct { | ||
Name string | ||
Value string | ||
|
||
Store StorageProvider | ||
} | ||
|
||
// Run adds the named location, overwriting the existing value if necessary. | ||
func (a *AddCmd) Run(conf *Configuration, i Indicator) error { | ||
conf.Locations[a.Name] = a.Value | ||
return a.Store.Save(conf) | ||
} | ||
|
||
// Validate validates the AddCmd is properly initialized and ready to be Run. | ||
func (a *AddCmd) Validate(conf *Configuration) error { | ||
if len(a.Name) == 0 { | ||
return ErrAddNameMissing | ||
} | ||
if len(a.Value) == 0 { | ||
return ErrAddLocationMissing | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// String returns a string representation of the AddCmd. | ||
func (a *AddCmd) String() string { | ||
return fmt.Sprintf("Adding named location '%v' with value '%v'", a.Name, a.Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestAddCmd_Run(t *testing.T) { | ||
tests := []struct { | ||
conf *Configuration | ||
|
||
name string | ||
value string | ||
}{ | ||
{&Configuration{Locations: make(map[string]string)}, "name", "value"}, | ||
{&Configuration{Locations: map[string]string{"name": "value1"}}, "name", "value2"}, | ||
} | ||
|
||
for idx, tt := range tests { | ||
m := mockStorageProvider{ | ||
saveFn: func(i interface{}) error { | ||
if i != tt.conf { | ||
t.Fatalf("[#%v] Unexpected parameter to Save, got=%v", idx, i) | ||
} | ||
return nil | ||
}, | ||
} | ||
a := AddCmd{Name: tt.name, Value: tt.value, Store: &m} | ||
|
||
if err := a.Run(tt.conf, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if tt.conf.Locations[tt.name] != tt.value { | ||
t.Fatalf("[#%v] Unexpected value stored, expected=%v, got=%v", idx, tt.value, tt.conf.Locations[tt.name]) | ||
} | ||
} | ||
|
||
// Negative | ||
{ | ||
testErr := errors.New("mock err") | ||
var m mockStorageProvider | ||
m.saveFn = func(i interface{}) error { | ||
return testErr | ||
} | ||
|
||
conf := Configuration{Locations: make(map[string]string)} | ||
a := AddCmd{Name: "name", Value: "value", Store: &m} | ||
if err := a.Run(&conf, nil); err != testErr { | ||
t.Fatalf("Unexpected error, expected=%v, got=%v", testErr, err) | ||
} | ||
} | ||
} | ||
|
||
func TestAddCmd_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value string | ||
err error | ||
}{ | ||
{"name", "value", nil}, | ||
{"", "value", ErrAddNameMissing}, | ||
{"name", "", ErrAddLocationMissing}, | ||
} | ||
|
||
for idx, tt := range tests { | ||
a := AddCmd{Name: tt.name, Value: tt.value} | ||
|
||
if err := a.Validate(nil); err != tt.err { | ||
t.Fatalf("[#%v] Unexpected error, expected=%v, got=%v", idx, tt.err, err) | ||
} | ||
} | ||
} |
Oops, something went wrong.