19 files changed, 87 insertions(+), 97 deletions(-)
R fav/ws.go => api/ws.go
M cmd/add.go
M cmd/root.go
M cmd/update.go
D fav/common.go
D fav/read.go
M format/color.go
M format/interface.go
M format/pretty.go
M format/simple.go
R db/db.go => network/db.go
R db/direction.go => network/direction.go
R fav/write.go => network/favorite.go
R db/line.go => network/line.go
R db/stop.go => network/stop.go
R db/natsort/cmp.go => utils/natsort/cmp.go
R db/natsort/cmp_test.go => utils/natsort/cmp_test.go
R db/natsort/sort.go => utils/natsort/sort.go
R db/natsort/sort_test.go => utils/natsort/sort_test.go
R fav/ws.go => api/ws.go +6 -4
@@ 1,4 1,4 @@
-package fav
+package api
import (
"encoding/json"
@@ 9,6 9,8 @@ import (
"time"
"github.com/denisbrodbeck/machineid"
+
+ "islas/network"
)
const (
@@ 83,11 85,11 @@ type wsHours struct {
}
// GetTimeTable returns a real-time time table for the associated stop.
-func (fav *Favorite) GetTimeTable() (*TimeTable, error) {
- return fav.getTimeTable(true)
+func GetTimeTable(fav *network.Favorite) (*TimeTable, error) {
+ return getTimeTable(fav, true)
}
-func (fav *Favorite) getTimeTable(realtime bool) (*TimeTable, error) {
+func getTimeTable(fav *network.Favorite, realtime bool) (*TimeTable, error) {
date := time.Now().Format("2006-01-02")
calcMode := ""
M cmd/add.go => cmd/add.go +9 -10
@@ 7,8 7,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/dixonwille/wmenu.v4"
- "islas/db"
- "islas/fav"
+ "islas/network"
)
func init() {
@@ 24,7 23,7 @@ var addCmd = &cobra.Command{
// AddFavorite adds a stop to the favorites.
func AddFavorite(cmd *cobra.Command, args []string) {
- conn, err := db.Open()
+ conn, err := network.OpenDB()
if err != nil {
log.WithError(err).Fatal("Cannot open the database")
}
@@ 44,7 43,7 @@ func AddFavorite(cmd *cobra.Command, args []string) {
if len(opts) == 0 || opts[0].ID < 0 {
log.Fatal("Bad choice!")
}
- line := opts[0].Value.(db.Line)
+ line := opts[0].Value.(network.Line)
fmt.Printf("\nSelected line %s (%s)\n", line.Number, line.Name)
addFavoriteStep2(conn, line)
return nil
@@ 54,7 53,7 @@ func AddFavorite(cmd *cobra.Command, args []string) {
}
}
-func addFavoriteStep2(conn *db.DB, line db.Line) {
+func addFavoriteStep2(conn *network.DB, line network.Line) {
directions, err := conn.GetDirections(line.ID)
if err != nil {
log.WithError(err).Fatal("Cannot read the available directions")
@@ 69,7 68,7 @@ func addFavoriteStep2(conn *db.DB, line db.Line) {
if len(opts) == 0 || opts[0].ID < 0 {
log.Fatal("Bad choice!")
}
- direction := opts[0].Value.(db.Direction)
+ direction := opts[0].Value.(network.Direction)
fmt.Printf("\nSelected line %s to %s\n", line.Number, direction.Label)
addFavoriteStep3(conn, line, direction)
return nil
@@ 79,7 78,7 @@ func addFavoriteStep2(conn *db.DB, line db.Line) {
}
}
-func addFavoriteStep3(conn *db.DB, line db.Line, direction db.Direction) {
+func addFavoriteStep3(conn *network.DB, line network.Line, direction network.Direction) {
stops, err := conn.GetStops(line.ID, direction.ID)
if err != nil {
log.WithError(err).Fatal("Cannot read the available stops")
@@ 94,15 93,15 @@ func addFavoriteStep3(conn *db.DB, line db.Line, direction db.Direction) {
if len(opts) == 0 || opts[0].ID < 0 {
log.Fatal("Bad choice!")
}
- stop := opts[0].Value.(db.Stop)
+ stop := opts[0].Value.(network.Stop)
fmt.Printf("\nSelected stop %s on line %s to %s\n", stop.Name, line.Number, direction.Label)
- newFav := fav.Favorite{
+ newFav := network.Favorite{
LineID: line.ID,
DirectionID: direction.ID,
StopID: stop.ID,
}
- return fav.AddToFile(newFav)
+ return newFav.Save()
})
if err := menu.Run(); err != nil {
log.WithError(err).Fatal("Menu error")
M cmd/root.go => cmd/root.go +6 -5
@@ 10,8 10,9 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
- "islas/fav"
+ "islas/api"
"islas/format"
+ "islas/network"
)
var (
@@ 42,18 43,18 @@ func Execute() {
}
func rootCommand(cmd *cobra.Command, args []string) {
- favs, err := fav.FromFile()
+ favs, err := network.FavoritesFromFile()
if err != nil {
log.WithError(err).Fatal("Cannot read the favorites")
}
var g errgroup.Group
- tts := map[int]*fav.TimeTable{}
+ tts := map[int]*api.TimeTable{}
var ttsm sync.Mutex
- getTT := func(idx int, fav fav.Favorite) {
+ getTT := func(idx int, fav *network.Favorite) {
defer g.Done()
- tt, err := fav.GetTimeTable()
+ tt, err := api.GetTimeTable(fav)
if err != nil {
g.Error(err)
return
M cmd/update.go => cmd/update.go +2 -2
@@ 12,7 12,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
- "islas/db"
+ "islas/network"
)
const (
@@ 64,7 64,7 @@ func UpdateDB(cmd *cobra.Command, args []string) {
log.WithError(err).Fatal("Cannot parse the database XML")
}
- dbPath := db.GetPath()
+ dbPath := network.GetDBPath()
url := descriptor.Databases[0].URL[0]
log.WithFields(log.Fields{
"url": url,
D fav/common.go => fav/common.go +0 -24
@@ 1,24 0,0 @@
-package fav
-
-import (
- "github.com/casimir/xdg-go"
-
- "islas/db"
-)
-
-// GetPath returns the path to the favorites file.
-func GetPath() string {
- return xdg.ConfigPath("favorites.yml")
-}
-
-// Favorite stores everything about a bus/tramway stop.
-type Favorite struct {
- StopID int
- LineID int
- DirectionID int
-}
-
-// AsStop returns the DB stop that corresponds to this favorite.
-func (fav *Favorite) AsStop(conn *db.DB) (db.Stop, error) {
- return conn.GetStop(fav.LineID, fav.DirectionID, fav.StopID)
-}
D fav/read.go => fav/read.go +0 -26
@@ 1,26 0,0 @@
-package fav
-
-import (
- "os"
-
- "gopkg.in/yaml.v2"
-)
-
-// FromFile reads a list of favorites from a YAML file.
-func FromFile() ([]Favorite, error) {
- favs := []Favorite{}
-
- path := GetPath()
- favFile, err := os.Open(path)
- if err != nil {
- if os.IsNotExist(err) {
- return favs, nil
- }
- return nil, err
- }
- defer favFile.Close()
-
- dec := yaml.NewDecoder(favFile)
- err = dec.Decode(&favs)
- return favs, err
-}
M format/color.go => format/color.go +3 -3
@@ 3,7 3,7 @@ package format
import (
"github.com/fatih/color"
- "islas/fav"
+ "islas/api"
)
func init() {
@@ 12,10 12,10 @@ func init() {
// ColorFormatter is a very basic time table formatter.
type ColorFormatter struct {
- tts []*fav.TimeTable
+ tts []*api.TimeTable
}
-func (f *ColorFormatter) Add(tt *fav.TimeTable) {
+func (f *ColorFormatter) Add(tt *api.TimeTable) {
f.tts = append(f.tts, tt)
}
M format/interface.go => format/interface.go +2 -2
@@ 3,12 3,12 @@ package format
import (
"errors"
- "islas/fav"
+ "islas/api"
)
// Formatter is the interface for time table formatters.
type Formatter interface {
- Add(tt *fav.TimeTable)
+ Add(tt *api.TimeTable)
Format() string
}
M format/pretty.go => format/pretty.go +2 -2
@@ 8,7 8,7 @@ import (
"github.com/fatih/color"
"github.com/mattn/go-tty"
- "islas/fav"
+ "islas/api"
)
func init() {
@@ 125,7 125,7 @@ func (t *prettyTable) format(width int) []string {
return res
}
-func (f *PrettyFormatter) Add(tt *fav.TimeTable) {
+func (f *PrettyFormatter) Add(tt *api.TimeTable) {
t := prettyTable{}
lineName := abbreviate(tt.LineNumber)
M format/simple.go => format/simple.go +3 -3
@@ 3,7 3,7 @@ package format
import (
"fmt"
- "islas/fav"
+ "islas/api"
)
func init() {
@@ 12,10 12,10 @@ func init() {
// SimpleFormatter is a very basic time table formatter.
type SimpleFormatter struct {
- tts []*fav.TimeTable
+ tts []*api.TimeTable
}
-func (f *SimpleFormatter) Add(tt *fav.TimeTable) {
+func (f *SimpleFormatter) Add(tt *api.TimeTable) {
f.tts = append(f.tts, tt)
}
R db/db.go => network/db.go +7 -6
@@ 1,12 1,13 @@
-package db
+package network
import (
"database/sql"
- "islas/db/natsort"
"github.com/casimir/xdg-go"
"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
+
+ "islas/utils/natsort"
)
func init() {
@@ 19,8 20,8 @@ func init() {
)
}
-// GetPath returns the path to the database file.
-func GetPath() string {
+// GetDBPath returns the path to the database file.
+func GetDBPath() string {
return xdg.CachePath("stan.db")
}
@@ 30,8 31,8 @@ type DB struct {
}
// Open creates a new instance of the Stan database.
-func Open() (*DB, error) {
- path := GetPath()
+func OpenDB() (*DB, error) {
+ path := GetDBPath()
db, err := sqlx.Connect("sqlite3_islas", path)
if err != nil {
return nil, err
R db/direction.go => network/direction.go +1 -1
@@ 1,4 1,4 @@
-package db
+package network
// Direction is the direction of a line.
type Direction struct {
R fav/write.go => network/favorite.go +44 -7
@@ 1,30 1,67 @@
-package fav
+package network
import (
"os"
"path"
+ "github.com/casimir/xdg-go"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
-// AddToFile adds the given favorite to the favorites YAML file.
-func AddToFile(newFav Favorite) error {
+// GetPath returns the path to the favorites file.
+func GetPath() string {
+ return xdg.ConfigPath("favorites.yml")
+}
+
+// Favorite stores everything about a bus/tramway stop.
+type Favorite struct {
+ StopID int
+ LineID int
+ DirectionID int
+}
+
+// FavoritesFromFile reads a list of favorites from a YAML file.
+func FavoritesFromFile() ([]*Favorite, error) {
+ favs := []*Favorite{}
+
+ path := GetPath()
+ favFile, err := os.Open(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return favs, nil
+ }
+ return nil, err
+ }
+ defer favFile.Close()
+
+ dec := yaml.NewDecoder(favFile)
+ err = dec.Decode(&favs)
+ return favs, err
+}
+
+// AsStop returns the DB stop that corresponds to this favorite.
+func (fav *Favorite) AsStop(conn *DB) (Stop, error) {
+ return conn.GetStop(fav.LineID, fav.DirectionID, fav.StopID)
+}
+
+// Save adds the favorite to the favorites YAML file.
+func (fav *Favorite) Save() error {
// Read the current ones
- favs, err := FromFile()
+ favs, err := FavoritesFromFile()
if err != nil {
return err
}
// Is it already in the list?
- for _, fav := range favs {
- if fav.StopID == newFav.StopID && fav.LineID == newFav.LineID && fav.DirectionID == newFav.DirectionID {
+ for _, savedFav := range favs {
+ if savedFav.StopID == fav.StopID && savedFav.LineID == fav.LineID && savedFav.DirectionID == fav.DirectionID {
return nil
}
}
// Nope: add it!
- favs = append(favs, newFav)
+ favs = append(favs, fav)
// And save the fileā¦
favPath := GetPath()
R db/line.go => network/line.go +1 -1
@@ 1,4 1,4 @@
-package db
+package network
// Line is a bus/tramway line.
type Line struct {
R db/stop.go => network/stop.go +1 -1
@@ 1,4 1,4 @@
-package db
+package network
// Stop is a bus/tramway stop.
type Stop struct {
R db/natsort/cmp.go => utils/natsort/cmp.go +0 -0
R db/natsort/cmp_test.go => utils/natsort/cmp_test.go +0 -0
R db/natsort/sort.go => utils/natsort/sort.go +0 -0
R db/natsort/sort_test.go => utils/natsort/sort_test.go +0 -0