vodo/app.go

124 lines
2.7 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
"git.bullercodeworks.com/brian/gask"
userConfig "github.com/br0xen/user-config"
)
// App holds everything we need to handle all application logic
type App struct {
Parms []Parameter
db *gask.GaskDB
cfg *userConfig.Config
}
// Parameter is a cli parameter and it's description
type Parameter struct {
Flag string
Desc []string
}
// NewApp creates the App object, which contains all application logic
func NewApp() *App {
var err error
// Create the App
app := App{}
// First we get the config
app.cfg, err = userConfig.NewConfig(AppName)
if err != nil {
fmt.Println("Creating new config")
app.cfg.Save()
}
wikifile := app.cfg.Get("wikifile")
// Try to open the db
if wikifile != "" {
// We've got a value, is it valid?
app.db, err = gask.NewGaskDB(wikifile)
for err != nil {
wikifile = ""
fmt.Println("Unable to open todos wiki file")
}
} else {
// No value for wikifile
wikifile = app.askForPath()
}
app.db, err = gask.NewGaskDB(wikifile)
if err != nil {
wikifile = ""
}
app.cfg.Set("wikifile", wikifile)
app.cfg.Save()
if wikifile == "" {
fmt.Println("Unable to open todos wiki file")
os.Exit(1)
}
err = app.CreateCliDb()
if err != nil {
fmt.Println("Unable to build CLI DB: " + err.Error())
os.Exit(1)
}
app.Parms = []Parameter{
Parameter{Flag: "add", Desc: []string{"Add a task to an existing list"}},
Parameter{Flag: "addlist", Desc: []string{"Add a list to the database"}},
Parameter{Flag: "done", Desc: []string{"Mark a task done"}},
Parameter{Flag: "help", Desc: []string{"Print this message"}},
Parameter{Flag: "lists", Desc: []string{"Print Lists"}},
Parameter{Flag: "ls", Desc: []string{"Print Tasks"}},
}
return &app
}
func (a *App) Execute(args []string) error {
if len(args) == 0 {
args = append(args, "ls")
}
var opts []string
for i := range a.Parms {
opts = append(opts, a.Parms[i].Flag)
}
fnd := matchString(args[0], opts)
switch fnd {
// Task cases
case "add":
return a.AddTask(args[1:])
case "done":
return a.CompleteTask(args[1:])
case "ls": // List tasks
return a.PrintTasks(args[1:])
// List cases
case "lists": // List lists
return a.PrintLists(args[1:])
case "addlist":
return a.AddList(args[1:])
case "help":
return a.PrintHelp(args[1:])
}
return nil
}
func (a *App) PrintHelp(args []string) error {
return nil
}
// askForPath asks the user to enter the path to their todo wiki file
func (a *App) askForPath() string {
fmt.Println("Please specify the full path to the wiki file that contains your todo lists:")
fmt.Println("(Leave blank to exit)")
fmt.Print("> ")
reader := bufio.NewReader(os.Stdin)
wikifile, _ := reader.ReadString('\n')
wikifile = strings.TrimSpace(wikifile)
return wikifile
}