~kris/9p

glenda

ac5d697efa679487ceda0658945cf5653eaf35ed — Sean Hinchee 8 years ago a94054f
migrated config to mux, this makes more sense
4 files changed, 34 insertions(+), 37 deletions(-)

M main.go
M misc.go
R {config => x/mux/config}.go
M x/mux/misc.go
M main.go => main.go +1 -1
@@ 80,7 80,7 @@ func main() {
	go mux.Listener()
	go mux.CommMux()
	go CommMux()
	Config.Init()
	go mux.Config.Init()

	Session.UpdateStatus(0, "with #cat-v")


M misc.go => misc.go +1 -10
@@ 3,7 3,7 @@ package main
// Miscellaneous utilities for communicating with Mux

import (
	"bitbucket.org/henesy/glenda/x/mux"
//	"bitbucket.org/henesy/glenda/x/mux"
	"time"
)



@@ 11,15 11,6 @@ import (
func CommMux() {
	for {
		select {
		case r := <-mux.GlendaChan:
			if r == "dump" {
				err := Config.Write()
				if err != nil {
					mux.MuxChan <- "dump failed"
				} else {
					mux.MuxChan <- "dump ok"
				}
			}
		default:
		}


R config.go => x/mux/config.go +26 -16
@@ 1,4 1,4 @@
package main
package mux

// General configuration utilities for Glenda



@@ 7,11 7,12 @@ import (
	"os"
	"fmt"
	"strings"
	"github.com/SlyMarbo/rss"
)

// Stores config for current state
type Configuration struct {
	LastChange string
	Feeds	[]rss.Feed
}

// Initializes current config (called once at start) ­ just .Read()?


@@ 21,15 22,15 @@ func (c *Configuration) Init() {

// Writes current config
func (c *Configuration) Write() (rerr error) {
	WRT: 
	WRT:
	rerr = nil
	f, err := os.OpenFile("./cfg/glenda.cfg", os.O_RDWR, 0666)
	defer f.Close()
	if err != nil {
		if strings.Contains(err.Error(), "no such file or directory") {
			// danger: this can go infinite
			Config.Setup()
			goto WRT
			return
		} else {
			fmt.Println("Error opening config (w), see: config.go")
			fmt.Printf("%s\n", err)


@@ 50,21 51,30 @@ func (c *Configuration) Write() (rerr error) {

// Reads current config into memory
func (c *Configuration) Read() (rerr error) {
	RD:
	f, err := os.Open("./cfg/glenda.cfg")
	if err != nil {
		fmt.Println("Error opening config (r), see: config.go")
		fmt.Printf("%s\n", err)
		rerr = err
	}
	
	d := json.NewDecoder(f)
	err = d.Decode(&Config)
	if err != nil {
		fmt.Println("Error reading config, see: config.go")
		fmt.Printf("%s\n", err)
		rerr = err
		if strings.Contains(err.Error(), "no such file or directory") {
			// danger: this can go infinite
			Config.Setup()
			goto RD
		} else {
			fmt.Println("Error opening config (r), see: config.go")
			fmt.Printf("%s\n", err)
			rerr = err
		}
	} else {
		d := json.NewDecoder(f)
		err = d.Decode(&Config)
		if err != nil {
			fmt.Println("Error reading config, see: config.go")
			fmt.Printf("%s\n", err)
			rerr = err
			Config.Write()
			goto RD
		}
	}
	

	return
}


M x/mux/misc.go => x/mux/misc.go +6 -10
@@ 24,14 24,6 @@ func CommMux() {
	// Listen for signals till death do us part
	for {
		select {
		case r := <-MuxChan:
			if r == "dump ok" {
				dumpChan <- "Ok."
			} else if r == "dump failed" {
				dumpChan <- "Dump Failed. Check Logs."
			}
		case <-dumpChan:
			GlendaChan <- "dump"
		default:
		}



@@ 43,8 35,12 @@ func CommMux() {
func (m *Mux) Dump(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""

	dumpChan <- ""
	resp += <-dumpChan
	err := Config.Write()
	if err != nil {
		resp += "Dump failed. Check logs."
	} else {
		resp += "Ok."
	}

	resp += "\n"