From 49ea48976d5621dc7fb26534fa2d71e7740fd1df Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Fri, 3 Apr 2020 22:29:12 -0400 Subject: [PATCH] Re-structure packages Overhaul of package structure and other cleanup. --- {internal => config}/config.go | 18 ++++++------- {internal => config}/config_test.go | 2 +- controller/controller.go | 22 ++++++++++++++++ cui/keybindings.go | 29 +++++++++++++++++++++ cmd/start.go => cui/main.go | 39 ++++++----------------------- {internal => feeds}/rss.go | 11 +++----- file/file.go | 15 +++++++++++ internal/controller.go | 18 ------------- main.go | 4 +-- 9 files changed, 87 insertions(+), 71 deletions(-) rename {internal => config}/config.go (80%) rename {internal => config}/config_test.go (97%) create mode 100644 controller/controller.go create mode 100644 cui/keybindings.go rename cmd/start.go => cui/main.go (77%) rename {internal => feeds}/rss.go (89%) create mode 100644 file/file.go delete mode 100644 internal/controller.go diff --git a/internal/config.go b/config/config.go similarity index 80% rename from internal/config.go rename to config/config.go index 689bbca..c29ff3a 100644 --- a/internal/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package internal +package config import ( "errors" @@ -7,6 +7,7 @@ import ( "os" "runtime" + "github.com/davegallant/srv/file" "gopkg.in/yaml.v2" ) @@ -25,7 +26,7 @@ var DefaultConfiguration = Configuration{ }, } -// Determines the default viewer +// DetermineExternalViewer checks the OS to decide the default viewer func DetermineExternalViewer() (string, error) { switch os := runtime.GOOS; os { case "linux": @@ -38,17 +39,14 @@ func DetermineExternalViewer() (string, error) { } // LoadConfiguration takes a filename (configuration) and loads it. -func LoadConfiguration(file string) Configuration { +func LoadConfiguration(f string) Configuration { var config Configuration - // If the configuration file does not exist, - // write a default config - _, err := os.Stat(file) - if os.IsNotExist(err) { - WriteConfig(DefaultConfiguration, file) + if !file.Exists(f) { + WriteConfig(DefaultConfiguration, f) } - data, err := ioutil.ReadFile(file) + data, err := ioutil.ReadFile(f) if err != nil { log.Println(err) @@ -77,7 +75,7 @@ func WriteConfig(config Configuration, file string) error { log.Fatalf("Unable to marshal default config: %v", err) } - err = ioutil.WriteFile(file, c, 0644) + err = ioutil.WriteFile(file, c, 0600) if err != nil { log.Fatalf("Unable to write default config: %v", err) } diff --git a/internal/config_test.go b/config/config_test.go similarity index 97% rename from internal/config_test.go rename to config/config_test.go index 77fb8f0..aded4d4 100644 --- a/internal/config_test.go +++ b/config/config_test.go @@ -1,4 +1,4 @@ -package internal +package config import ( "testing" diff --git a/controller/controller.go b/controller/controller.go new file mode 100644 index 0000000..9ff4de9 --- /dev/null +++ b/controller/controller.go @@ -0,0 +1,22 @@ +package controller + +import ( + "time" + + config "github.com/davegallant/srv/config" + feeds "github.com/davegallant/srv/feeds" +) + +// Controller keeps everything together +type Controller struct { + Config config.Configuration + lastUpdate time.Time + Rss *feeds.RSS +} + +// Init initiates the controller with config +func (c *Controller) Init(conf string) { + c.Config = config.LoadConfiguration(conf) + c.Rss = &feeds.RSS{} + c.Rss.Update(c.Config.Feeds) +} diff --git a/cui/keybindings.go b/cui/keybindings.go new file mode 100644 index 0000000..9bf6fd0 --- /dev/null +++ b/cui/keybindings.go @@ -0,0 +1,29 @@ +package cui + +import "github.com/jroimartin/gocui" + +// Map keys to actions +func keybindings(g *gocui.Gui) error { + if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + return err + } + if err := g.SetKeybinding("feeds", gocui.KeyEnter, gocui.ModNone, openFeed); err != nil { + return err + } + if err := g.SetKeybinding("Items", gocui.KeyEnter, gocui.ModNone, openItem); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyF5, gocui.ModNone, refreshFeeds); err != nil { + return err + } + return nil +} diff --git a/cmd/start.go b/cui/main.go similarity index 77% rename from cmd/start.go rename to cui/main.go index 64e0e96..9ed01cf 100644 --- a/cmd/start.go +++ b/cui/main.go @@ -1,17 +1,18 @@ -package cmd +package cui import ( "fmt" "log" "os/exec" "os/user" + "path" - "github.com/davegallant/srv/internal" + "github.com/davegallant/srv/controller" "github.com/jroimartin/gocui" ) // Controller can access Feeds and Config -var Controller *internal.Controller +var Controller *controller.Controller var ( viewArr = []string{"feeds", "Items"} @@ -94,37 +95,11 @@ func hideLoading(g *gocui.Gui) error { func refreshFeeds(g *gocui.Gui, v *gocui.View) error { showLoading(g) - Controller.Rss.Update() + Controller.Rss.Update(Controller.Config.Feeds) //hideLoading(g) return nil } -// statically map all of the keys -func keybindings(g *gocui.Gui) error { - if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { - return err - } - if err := g.SetKeybinding("feeds", gocui.KeyEnter, gocui.ModNone, openFeed); err != nil { - return err - } - if err := g.SetKeybinding("Items", gocui.KeyEnter, gocui.ModNone, openItem); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyF5, gocui.ModNone, refreshFeeds); err != nil { - return err - } - return nil -} - func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { if _, err := g.SetCurrentView(name); err != nil { return nil, err @@ -195,9 +170,9 @@ func Start() { if err != nil { log.Fatal(err) } - configPath := usr.HomeDir + "/.config/srv/config.yaml" + configPath := path.Join(usr.HomeDir, ".config", "srv", "config.yaml") - Controller = &internal.Controller{} + Controller = &controller.Controller{} Controller.Init(configPath) g, err := gocui.NewGui(gocui.OutputNormal) diff --git a/internal/rss.go b/feeds/rss.go similarity index 89% rename from internal/rss.go rename to feeds/rss.go index bd08b71..da72462 100644 --- a/internal/rss.go +++ b/feeds/rss.go @@ -1,4 +1,4 @@ -package internal +package feeds import ( "fmt" @@ -12,20 +12,15 @@ import ( type RSS struct { Feeds []*gofeed.Feed - c *Controller -} - -func (r *RSS) New(c *Controller) { - r.c = c } // Update fetches all articles for all feeds -func (r *RSS) Update() { +func (r *RSS) Update(feeds []string) { fp := gofeed.NewParser() var wg sync.WaitGroup var mux sync.Mutex r.Feeds = []*gofeed.Feed{} - for _, f := range r.c.Config.Feeds { + for _, f := range feeds { f := f wg.Add(1) go func() { diff --git a/file/file.go b/file/file.go new file mode 100644 index 0000000..c25183d --- /dev/null +++ b/file/file.go @@ -0,0 +1,15 @@ +// Package file contains filesystem functions +package file + +import ( + "os" +) + +// Exists returns true if a file exists +func Exists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/internal/controller.go b/internal/controller.go deleted file mode 100644 index 4e28fb3..0000000 --- a/internal/controller.go +++ /dev/null @@ -1,18 +0,0 @@ -package internal - -import "time" - -// Controller keeps everything together -type Controller struct { - Config Configuration - lastUpdate time.Time - Rss *RSS -} - -// Init initiates the controller with config -func (c *Controller) Init(config string) { - c.Config = LoadConfiguration(config) - c.Rss = &RSS{} - c.Rss.New(c) - c.Rss.Update() -} diff --git a/main.go b/main.go index 676d7c9..d810095 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main -import "github.com/davegallant/srv/cmd" +import cui "github.com/davegallant/srv/cui" func main() { - cmd.Start() + cui.Start() }