Re-structure packages

Overhaul of package structure and other cleanup.
This commit is contained in:
Dave Gallant
2020-04-03 22:29:12 -04:00
parent 025b68de93
commit 49ea48976d
9 changed files with 87 additions and 71 deletions

View File

@@ -1,4 +1,4 @@
package internal package config
import ( import (
"errors" "errors"
@@ -7,6 +7,7 @@ import (
"os" "os"
"runtime" "runtime"
"github.com/davegallant/srv/file"
"gopkg.in/yaml.v2" "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) { func DetermineExternalViewer() (string, error) {
switch os := runtime.GOOS; os { switch os := runtime.GOOS; os {
case "linux": case "linux":
@@ -38,17 +39,14 @@ func DetermineExternalViewer() (string, error) {
} }
// LoadConfiguration takes a filename (configuration) and loads it. // LoadConfiguration takes a filename (configuration) and loads it.
func LoadConfiguration(file string) Configuration { func LoadConfiguration(f string) Configuration {
var config Configuration var config Configuration
// If the configuration file does not exist, if !file.Exists(f) {
// write a default config WriteConfig(DefaultConfiguration, f)
_, err := os.Stat(file)
if os.IsNotExist(err) {
WriteConfig(DefaultConfiguration, file)
} }
data, err := ioutil.ReadFile(file) data, err := ioutil.ReadFile(f)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@@ -77,7 +75,7 @@ func WriteConfig(config Configuration, file string) error {
log.Fatalf("Unable to marshal default config: %v", err) log.Fatalf("Unable to marshal default config: %v", err)
} }
err = ioutil.WriteFile(file, c, 0644) err = ioutil.WriteFile(file, c, 0600)
if err != nil { if err != nil {
log.Fatalf("Unable to write default config: %v", err) log.Fatalf("Unable to write default config: %v", err)
} }

View File

@@ -1,4 +1,4 @@
package internal package config
import ( import (
"testing" "testing"

22
controller/controller.go Normal file
View File

@@ -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)
}

29
cui/keybindings.go Normal file
View File

@@ -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
}

View File

@@ -1,17 +1,18 @@
package cmd package cui
import ( import (
"fmt" "fmt"
"log" "log"
"os/exec" "os/exec"
"os/user" "os/user"
"path"
"github.com/davegallant/srv/internal" "github.com/davegallant/srv/controller"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
// Controller can access Feeds and Config // Controller can access Feeds and Config
var Controller *internal.Controller var Controller *controller.Controller
var ( var (
viewArr = []string{"feeds", "Items"} viewArr = []string{"feeds", "Items"}
@@ -94,37 +95,11 @@ func hideLoading(g *gocui.Gui) error {
func refreshFeeds(g *gocui.Gui, v *gocui.View) error { func refreshFeeds(g *gocui.Gui, v *gocui.View) error {
showLoading(g) showLoading(g)
Controller.Rss.Update() Controller.Rss.Update(Controller.Config.Feeds)
//hideLoading(g) //hideLoading(g)
return nil 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) { func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
if _, err := g.SetCurrentView(name); err != nil { if _, err := g.SetCurrentView(name); err != nil {
return nil, err return nil, err
@@ -195,9 +170,9 @@ func Start() {
if err != nil { if err != nil {
log.Fatal(err) 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) Controller.Init(configPath)
g, err := gocui.NewGui(gocui.OutputNormal) g, err := gocui.NewGui(gocui.OutputNormal)

View File

@@ -1,4 +1,4 @@
package internal package feeds
import ( import (
"fmt" "fmt"
@@ -12,20 +12,15 @@ import (
type RSS struct { type RSS struct {
Feeds []*gofeed.Feed Feeds []*gofeed.Feed
c *Controller
}
func (r *RSS) New(c *Controller) {
r.c = c
} }
// Update fetches all articles for all feeds // Update fetches all articles for all feeds
func (r *RSS) Update() { func (r *RSS) Update(feeds []string) {
fp := gofeed.NewParser() fp := gofeed.NewParser()
var wg sync.WaitGroup var wg sync.WaitGroup
var mux sync.Mutex var mux sync.Mutex
r.Feeds = []*gofeed.Feed{} r.Feeds = []*gofeed.Feed{}
for _, f := range r.c.Config.Feeds { for _, f := range feeds {
f := f f := f
wg.Add(1) wg.Add(1)
go func() { go func() {

15
file/file.go Normal file
View File

@@ -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()
}

View File

@@ -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()
}

View File

@@ -1,9 +1,9 @@
package main package main
import "github.com/davegallant/srv/cmd" import cui "github.com/davegallant/srv/cui"
func main() { func main() {
cmd.Start() cui.Start()
} }