Skip to content

Commit

Permalink
Ability to specify a commute method #6
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBanks committed Feb 25, 2017
1 parent 974e3d5 commit 65d4cbe
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sanity:
.PHONY: sanity

# Creates release binaries for each supported platform/architecture.
release:
release: | sanity
@gox -osarch="darwin/386 darwin/amd64 linux/386 linux/amd64 linux/arm windows/386 windows/amd64" \
-output "bin/{{.Dir}}_{{.OS}}_{{.Arch}}" .
.PHONY: release
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ $ commuter -from gym -to-current
12 Minutes
```

### Travel Modes

By default, `commuter` assumes you are driving between locations. However, you can specify one or more commute methods using the `-drive`, `-walk`, `-bike` and `-transit` flags, like so:

```sh
# Single mode:
$ commuter -walk -to work
7 Hours 50 Minutes

# Multiple modes:
$ commuter -walk -transit -drive -bike -to work
Drive: 30 Minutes
Walk: 7 Hours 50 Minutes
Bike: 2 Hours 45 Minutes
Transit: 1 Hour 17 Minutes
```

And of course the different travel modes can be combined with your current location:

```sh
$ commuter -bike -from-current -to gym
2 Hours 18 Minutes
```

## License

```
Expand Down
30 changes: 19 additions & 11 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@ import (
)

const (
cmdDefault = "commuter"
defaultFromParam = "from"
defaultFromUsage = "The starting point of your commute, either a named location [ex. 'work'] or an address [ex. '123 Main St. Toronto, Canada']."
defaultToParam = "to"
defaultToUsage = "The destination of your commute, either a named location [ex. 'work'] or an address [ex. '123 Main St. Toronto, Canada']."
defaultFromCurrentParam = "from-current"
defaultFromCurrentUsage = "Sets your current location as the starting point of your commute. This uses Geolocation to attempt to determine your Latitude/Longitude based on IP Address. [Accuracy may vary]"
defaultToCurrentParam = "to-current"
defaultToCurrentUsage = "Sets your current location as the destination of your commute. This uses Geolocation to attempt to determine your Latitude/Longitude based on IP Address. [Accuracy may vary]"
cmdCommute = "commuter"
commuteFromParam = "from"
commuteFromUsage = "The starting point of your commute, either a named location [ex. 'work'] or an address [ex. '123 Main St. Toronto, Canada']."
commuteToParam = "to"
commuteToUsage = "The destination of your commute, either a named location [ex. 'work'] or an address [ex. '123 Main St. Toronto, Canada']."
commuteFromCurrentParam = "from-current"
commuteFromCurrentUsage = "Sets your current location as the starting point of your commute. This uses Geolocation to attempt to determine your Latitude/Longitude based on IP Address. [Accuracy may vary]"
commuteToCurrentParam = "to-current"
commuteToCurrentUsage = "Sets your current location as the destination of your commute. This uses Geolocation to attempt to determine your Latitude/Longitude based on IP Address. [Accuracy may vary]"
commuteDriveParam = "drive"
commuteDriveUsage = "Adds 'driving' as a transit type [default]"
commuteWalkParam = "walk"
commuteWalkUsage = "Adds 'walking' as a transit type"
commuteBikeParam = "bike"
commuteBikeUsage = "Adds 'biking' as a transit type"
commuteTransitParam = "transit"
commuteTransitUsage = "Adds 'transit' as a transit type"

cmdAdd = "add"
addNameParam = "name"
addNameUsage = `The name of the location you'd like to add [ex. "work"]. (required)\n`
addNameUsage = "The name of the location you'd like to add [ex. 'work']. (required)\n"
addLocationParam = "location"
addLocationUsage = `The location to be added [ex. "123 Main St. Toronto, Canada"]. (required)\n`
addLocationUsage = "The location to be added [ex. '123 Main St. Toronto, Canada']. (required)\n"

cmdList = "list"
)
Expand Down
20 changes: 15 additions & 5 deletions cli/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@ func (a *ArgParser) parseCommuteCmd(conf *cmd.Configuration, args []string) (*cm

c := cmd.CommuteCmd{Durationer: r, Locator: r}

f := flag.NewFlagSet(cmdDefault, flag.ExitOnError)
f.StringVar(&c.From, defaultFromParam, cmd.DefaultLocationAlias, defaultFromUsage)
f.BoolVar(&c.FromCurrent, defaultFromCurrentParam, false, defaultFromCurrentUsage)
f.StringVar(&c.To, defaultToParam, cmd.DefaultLocationAlias, defaultToUsage)
f.BoolVar(&c.ToCurrent, defaultToCurrentParam, false, defaultToCurrentUsage)
f := flag.NewFlagSet(cmdCommute, flag.ExitOnError)
f.StringVar(&c.From, commuteFromParam, cmd.DefaultLocationAlias, commuteFromUsage)
f.BoolVar(&c.FromCurrent, commuteFromCurrentParam, false, commuteFromCurrentUsage)
f.StringVar(&c.To, commuteToParam, cmd.DefaultLocationAlias, commuteToUsage)
f.BoolVar(&c.ToCurrent, commuteToCurrentParam, false, commuteToCurrentUsage)

f.BoolVar(&c.Drive, commuteDriveParam, false, commuteDriveUsage)
f.BoolVar(&c.Walk, commuteWalkParam, false, commuteWalkUsage)
f.BoolVar(&c.Bike, commuteBikeParam, false, commuteBikeUsage)
f.BoolVar(&c.Transit, commuteTransitParam, false, commuteTransitUsage)
f.Parse(args)

// Only default drive to true when no other methods of transport are selected.
if !c.Walk && !c.Bike && !c.Transit {
c.Drive = true
}

return &c, nil
}

Expand Down
37 changes: 37 additions & 0 deletions cli/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func TestArgParser_parseCommuteCmd(t *testing.T) {
}

conf.APIKey = "example"

// To/From and ToCurrent/FromCurrent
tests := []struct {
args []string
expected cmd.CommuteCmd
Expand Down Expand Up @@ -151,6 +153,41 @@ func TestArgParser_parseCommuteCmd(t *testing.T) {
t.Fatalf("[%v] Unexpected nil Durationer", idx)
} else if r.Locator == nil {
t.Fatalf("[%v] Unexpected nil Locator", idx)
} else if r.Drive == false {
t.Fatalf("[%v] Unexpected Drive, expected=true, got=false", idx)
} else if r.Bike == true || r.Walk == true || r.Transit == true {
t.Fatalf("[%v] Unexpected Bike/Walk/Transit, expected=false, got=[%v, %v, %v]", idx, r.Bike, r.Walk, r.Transit)
}
}

// Modes
mTests := []struct {
args []string
expected cmd.CommuteCmd
}{
{[]string{""}, cmd.CommuteCmd{Drive: true}},
{[]string{"-drive"}, cmd.CommuteCmd{Drive: true}},
{[]string{"-walk"}, cmd.CommuteCmd{Walk: true}},
{[]string{"-bike"}, cmd.CommuteCmd{Bike: true}},
{[]string{"-transit"}, cmd.CommuteCmd{Transit: true}},
{[]string{"-drive", "-walk"}, cmd.CommuteCmd{Drive: true, Walk: true}},
{[]string{"-drive", "-walk", "-bike", "-transit"}, cmd.CommuteCmd{Drive: true, Walk: true, Bike: true, Transit: true}},
}

for idx, tt := range mTests {
r, err := a.parseCommuteCmd(&conf, tt.args)
if err != nil {
t.Fatal(err)
}

if tt.expected.Drive != r.Drive {
t.Fatalf("[%v] Unexpected 'Drive' parsed, expected=%v, got=%v", idx, tt.expected.Drive, r.Drive)
} else if tt.expected.Walk != r.Walk {
t.Fatalf("[%v] Unexpected 'Walk' parsed, expected=%v, got=%v", idx, tt.expected.Walk, r.Walk)
} else if tt.expected.Bike != r.Bike {
t.Fatalf("[%v] Unexpected 'Bike' parsed, expected=%v, got=%v", idx, tt.expected.Bike, r.Bike)
} else if tt.expected.Transit != r.Transit {
t.Fatalf("[%v] Unexpected 'Transit' parsed, expected=%v, got=%v", idx, tt.expected.Transit, r.Transit)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd

import (
"time"

"github.com/KyleBanks/commuter/pkg/geo"
)

// Configuration represents a Commuter configuration, including
Expand Down Expand Up @@ -64,7 +66,7 @@ type StorageProvider interface {
// Durationer provides the ability to retrieve the duration between
// two locations.
type Durationer interface {
Duration(string, string) (*time.Duration, error)
Duration(string, string, geo.TravelMode) (*time.Duration, error)
}

// Locator provides the ability to retrieve the current location as
Expand Down
8 changes: 5 additions & 3 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"fmt"
"time"

"github.com/KyleBanks/commuter/pkg/geo"
)

// mock Indicator
Expand All @@ -18,11 +20,11 @@ func (m *mockIndicator) Indicate(msg string, args ...interface{}) {
// mock Durationer

type mockDurationer struct {
durationFn func(string, string) (*time.Duration, error)
durationFn func(string, string, geo.TravelMode) (*time.Duration, error)
}

func (m *mockDurationer) Duration(from, to string) (*time.Duration, error) {
return m.durationFn(from, to)
func (m *mockDurationer) Duration(from, to string, tm geo.TravelMode) (*time.Duration, error) {
return m.durationFn(from, to, tm)
}

// mock StorageProvider
Expand Down
Loading

0 comments on commit 65d4cbe

Please sign in to comment.