-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (60 loc) · 2.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"errors"
"github.com/alecthomas/kong"
"github.com/cranej/ticktock/store"
"github.com/cranej/ticktock/version"
"os"
"path/filepath"
)
var Cli struct {
Db string `type:"path" help:"Path of the db file, if not specified, try environment $TICKTOCK_DB, then default to $XDG_DATA_HOME/ticktock/db. $XDG_DATA_HOME default to $HOME/.local/share if not set."`
Version kong.VersionFlag `help:"Show version"`
Start StartCmd `cmd:"" help:"Start an activity"`
Close CloseCmd `cmd:"" help:"Close the ongoing activity"`
Titles TitlesCmd `cmd:"" help:"Print titles of recent closed activities"`
Ongoing OngoingCmd `cmd:"" help:"Show currently ongoing activity"`
Last LastCmd `cmd:"" help:"Show details of the latest closed activity with given title"`
Report ReportCmd `cmd:"" help:"Show time usage report"`
Server ServerCmd `cmd:"" help:"Start a server"`
Add AddCmd `cmd:"" help:"Add an closed activity"`
}
func main() {
ctx := kong.Parse(&Cli,
kong.Description("Ticktock is a tool for better tracking time usage. "),
kong.Vars{
"version": version.Version,
})
dbPath, err := dbPath(Cli.Db)
ctx.FatalIfErrorf(err)
Cli.Db = dbPath
db, err := store.NewSqliteStore(Cli.Db)
ctx.FatalIfErrorf(err)
ctx.BindTo(db, (*store.Store)(nil))
err = ctx.Run(db)
ctx.FatalIfErrorf(err)
}
func dbPath(fromCmd string) (string, error) {
if fromCmd != "" {
return fromCmd, nil
}
if tickDbEnv := os.Getenv("TICKTOCK_DB"); tickDbEnv != "" {
return tickDbEnv, nil
}
dbDir := ""
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome == "" {
xdgDataHome = os.Getenv("HOME")
if xdgDataHome == "" {
return "", errors.New("neither XDG_DATA_HOME nor HOME was set, could not determine db path")
}
dbDir = filepath.Join(xdgDataHome, ".local/share/ticktock")
} else {
dbDir = filepath.Join(xdgDataHome, "ticktock")
}
err := os.MkdirAll(dbDir, 0750)
if err != nil {
return "", err
}
return filepath.Join(dbDir, "db"), nil
}