standardize paths, use config for sub commands

This commit is contained in:
Kevin 2019-07-04 18:57:41 -04:00
parent 6644f7106c
commit 00ea73813a
5 changed files with 59 additions and 46 deletions

View File

@ -17,7 +17,10 @@ const (
type Config struct { type Config struct {
Colors *ColorMap `json:"colors"` Colors *ColorMap `json:"colors"`
DateTimeFmt string `json:"dateTimeFmt"` DateTimeFmt string `json:"dateTimeFmt"`
BasePath string `json:"basePath"`
DBPath string `json:"dbPath"` DBPath string `json:"dbPath"`
SocketPath string `json:"socketPath"`
IconPath string `json:"iconPath"`
} }
type ColorMap struct { type ColorMap struct {
@ -72,31 +75,40 @@ func (c *ColorMap) UnmarshalJSON(raw []byte) error {
return nil return nil
} }
func NewConfig(configPath string) (*Config, error) { func LoadConfig(configPath string, config *Config) error {
raw, err := ioutil.ReadFile(configPath) raw, err := ioutil.ReadFile(configPath)
if err != nil { if err != nil {
os.MkdirAll(path.Dir(configPath), 0755)
// Create an empty config file // Create an empty config file
// if it does not already exist. // if it does not already exist.
if os.IsNotExist(err) { if os.IsNotExist(err) {
raw, _ := json.Marshal(map[string]string{}) raw, _ := json.Marshal(map[string]string{})
err := ioutil.WriteFile(configPath, raw, 0644) err := ioutil.WriteFile(configPath, raw, 0644)
if err != nil { if err != nil {
return nil, err return err
} }
return NewConfig(configPath) return LoadConfig(configPath, config)
} }
return nil, err return err
} }
config := &Config{}
err = json.Unmarshal(raw, config) err = json.Unmarshal(raw, config)
if err != nil { if err != nil {
return nil, err return err
} }
if config.DateTimeFmt == "" { if config.DateTimeFmt == "" {
config.DateTimeFmt = defaultDateTimeFmt config.DateTimeFmt = defaultDateTimeFmt
} }
if config.DBPath == "" { if config.BasePath == "" {
config.DBPath = path.Dir(configPath) + "/pomo.db" config.BasePath = path.Dir(configPath)
} }
return config, nil if config.DBPath == "" {
config.DBPath = path.Join(config.BasePath, "/pomo.db")
}
if config.SocketPath == "" {
config.SocketPath = path.Join(config.BasePath, "/pomo.sock")
}
if config.IconPath == "" {
config.IconPath = path.Join(config.BasePath, "/icon.png")
}
return nil
} }

63
main.go
View File

@ -11,7 +11,7 @@ import (
cli "github.com/jawher/mow.cli" cli "github.com/jawher/mow.cli"
) )
func start(path *string) func(*cli.Cmd) { func start(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" cmd.Spec = "[OPTIONS] MESSAGE"
var ( var (
@ -23,7 +23,7 @@ func start(path *string) func(*cli.Cmd) {
cmd.Action = func() { cmd.Action = func() {
parsed, err := time.ParseDuration(*duration) parsed, err := time.ParseDuration(*duration)
maybe(err) maybe(err)
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
task := &Task{ task := &Task{
@ -40,9 +40,9 @@ func start(path *string) func(*cli.Cmd) {
task.ID = id task.ID = id
return nil return nil
})) }))
runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png")) runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
maybe(err) maybe(err)
server, err := NewServer(*path+"/pomo.sock", runner) server, err := NewServer(config.SocketPath, runner)
maybe(err) maybe(err)
server.Start() server.Start()
defer server.Stop() defer server.Stop()
@ -52,7 +52,7 @@ func start(path *string) func(*cli.Cmd) {
} }
} }
func create(path *string) func(*cli.Cmd) { func create(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" cmd.Spec = "[OPTIONS] MESSAGE"
var ( var (
@ -64,7 +64,7 @@ func create(path *string) func(*cli.Cmd) {
cmd.Action = func() { cmd.Action = func() {
parsed, err := time.ParseDuration(*duration) parsed, err := time.ParseDuration(*duration)
maybe(err) maybe(err)
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
task := &Task{ task := &Task{
@ -85,7 +85,7 @@ func create(path *string) func(*cli.Cmd) {
} }
} }
func begin(path *string) func(*cli.Cmd) { func begin(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] TASK_ID" cmd.Spec = "[OPTIONS] TASK_ID"
var ( var (
@ -93,7 +93,7 @@ func begin(path *string) func(*cli.Cmd) {
) )
cmd.Action = func() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
var task *Task var task *Task
@ -110,9 +110,9 @@ func begin(path *string) func(*cli.Cmd) {
task.Pomodoros = []*Pomodoro{} task.Pomodoros = []*Pomodoro{}
return nil return nil
})) }))
runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png")) runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
maybe(err) maybe(err)
server, err := NewServer(*path+"/pomo.sock", runner) server, err := NewServer(config.SocketPath, runner)
maybe(err) maybe(err)
server.Start() server.Start()
defer server.Stop() defer server.Stop()
@ -122,11 +122,11 @@ func begin(path *string) func(*cli.Cmd) {
} }
} }
func initialize(path *string) func(*cli.Cmd) { func initialize(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" cmd.Spec = "[OPTIONS]"
cmd.Action = func() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
maybe(initDB(db)) maybe(initDB(db))
@ -134,7 +134,7 @@ func initialize(path *string) func(*cli.Cmd) {
} }
} }
func list(path *string) func(*cli.Cmd) { func list(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" cmd.Spec = "[OPTIONS]"
var ( var (
@ -147,7 +147,7 @@ func list(path *string) func(*cli.Cmd) {
cmd.Action = func() { cmd.Action = func() {
duration, err := time.ParseDuration(*duration) duration, err := time.ParseDuration(*duration)
maybe(err) maybe(err)
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
maybe(db.With(func(tx *sql.Tx) error { maybe(db.With(func(tx *sql.Tx) error {
@ -166,7 +166,6 @@ func list(path *string) func(*cli.Cmd) {
maybe(json.NewEncoder(os.Stdout).Encode(tasks)) maybe(json.NewEncoder(os.Stdout).Encode(tasks))
return nil return nil
} }
config, err := NewConfig(*path + "/config.json")
maybe(err) maybe(err)
summerizeTasks(config, tasks) summerizeTasks(config, tasks)
return nil return nil
@ -175,12 +174,12 @@ func list(path *string) func(*cli.Cmd) {
} }
} }
func _delete(path *string) func(*cli.Cmd) { func _delete(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] TASK_ID" cmd.Spec = "[OPTIONS] TASK_ID"
var taskID = cmd.IntArg("TASK_ID", -1, "task to delete") var taskID = cmd.IntArg("TASK_ID", -1, "task to delete")
cmd.Action = func() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
maybe(db.With(func(tx *sql.Tx) error { maybe(db.With(func(tx *sql.Tx) error {
@ -190,11 +189,11 @@ func _delete(path *string) func(*cli.Cmd) {
} }
} }
func _status(path *string) func(*cli.Cmd) { func _status(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" cmd.Spec = "[OPTIONS]"
cmd.Action = func() { cmd.Action = func() {
client, err := NewClient(*path + "/pomo.sock") client, err := NewClient(config.SocketPath)
if err != nil { if err != nil {
outputStatus(Status{}) outputStatus(Status{})
return return
@ -207,12 +206,10 @@ func _status(path *string) func(*cli.Cmd) {
} }
} }
func config(path *string) func(*cli.Cmd) { func _config(config *Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" cmd.Spec = "[OPTIONS]"
cmd.Action = func() { cmd.Action = func() {
config, err := NewConfig(*path + "/config.json")
maybe(err)
maybe(json.NewEncoder(os.Stdout).Encode(config)) maybe(json.NewEncoder(os.Stdout).Encode(config))
} }
} }
@ -223,16 +220,20 @@ func main() {
app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take." app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take."
app.Spec = "[OPTIONS]" app.Spec = "[OPTIONS]"
var ( var (
path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory") config = &Config{}
path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
) )
app.Before = func() {
maybe(LoadConfig(*path, config))
}
app.Version("v version", Version) app.Version("v version", Version)
app.Command("start s", "start a new task", start(path)) app.Command("start s", "start a new task", start(config))
app.Command("init", "initialize the sqlite database", initialize(path)) app.Command("init", "initialize the sqlite database", initialize(config))
app.Command("config cf", "display the current configuration", config(path)) app.Command("config cf", "display the current configuration", _config(config))
app.Command("create c", "create a new task without starting", create(path)) app.Command("create c", "create a new task without starting", create(config))
app.Command("begin b", "begin requested pomodoro", begin(path)) app.Command("begin b", "begin requested pomodoro", begin(config))
app.Command("list l", "list historical tasks", list(path)) app.Command("list l", "list historical tasks", list(config))
app.Command("delete d", "delete a stored task", _delete(path)) app.Command("delete d", "delete a stored task", _delete(config))
app.Command("status st", "output the current status", _status(path)) app.Command("status st", "output the current status", _status(config))
app.Run(os.Args) app.Run(os.Args)
} }

View File

@ -3,13 +3,14 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path"
"testing" "testing"
"time" "time"
) )
func TestTaskRunner(t *testing.T) { func TestTaskRunner(t *testing.T) {
path, _ := ioutil.TempDir("/tmp", "") baseDir, _ := ioutil.TempDir("/tmp", "")
store, err := NewStore(path) store, err := NewStore(path.Join(baseDir, "pomo.db"))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"database/sql" "database/sql"
"os"
"strings" "strings"
"time" "time"
@ -19,8 +18,7 @@ type Store struct {
} }
func NewStore(path string) (*Store, error) { func NewStore(path string) (*Store, error) {
os.Mkdir(path, 0755) db, err := sql.Open("sqlite3", path)
db, err := sql.Open("sqlite3", path+"/pomo.db")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/user" "os/user"
"path"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
@ -19,7 +20,7 @@ func maybe(err error) {
func defaultConfigPath() string { func defaultConfigPath() string {
u, err := user.Current() u, err := user.Current()
maybe(err) maybe(err)
return u.HomeDir + "/.pomo" return path.Join(u.HomeDir, "/.pomo/config.json")
} }
func summerizeTasks(config *Config, tasks []*Task) { func summerizeTasks(config *Config, tasks []*Task) {