Re-structure packages
Overhaul of package structure and other cleanup.
This commit is contained in:
@@ -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)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
22
controller/controller.go
Normal file
22
controller/controller.go
Normal 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
29
cui/keybindings.go
Normal 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
|
||||
}
|
@@ -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)
|
@@ -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() {
|
15
file/file.go
Normal file
15
file/file.go
Normal 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()
|
||||
}
|
@@ -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()
|
||||
}
|
Reference in New Issue
Block a user