generated from bitfield/habit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
130 lines (117 loc) · 2.78 KB
/
cli.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package habit
import (
"flag"
"fmt"
"github.com/mitchellh/go-homedir"
"io"
)
//RunCLI parses arguments and passes them to habit.Controller
func RunCLI(args []string, output io.Writer) {
flagSet := flag.NewFlagSet("habit", flag.ContinueOnError)
flagSet.SetOutput(output)
flagSet.Usage = func() {
fmt.Fprintln(output,
`habit is an application to assist you in building habits
Usage: habit <Option Flags> <HABIT_NAME> -- to create/update a new habit
habit all -- to list all habits
Option Flags:`)
flagSet.PrintDefaults()
}
frequency := flagSet.String("f", "daily", "Set the frequency of the habit: daily, weekly.")
storeType := flagSet.String("s", "db", "Set the store backend for habit tracker: db, file.")
homeDir, err := homedir.Dir()
if err != nil {
fmt.Fprintln(output, err)
return
}
storeDir := flagSet.String("d", homeDir, "Set the store directory.")
err = flagSet.Parse(args)
if err != nil {
fmt.Fprintln(output, err)
return
}
if len(flagSet.Args()) == 0 {
flagSet.Usage()
return
}
if len(flagSet.Args()) > 1 {
fmt.Fprintln(output, "too many args")
flagSet.Usage()
return
}
store, err := storeFactory(*storeType, *storeDir)
if err != nil {
fmt.Fprintln(output, err)
flagSet.Usage()
return
}
controller, err := NewController(store)
if err != nil {
fmt.Fprintln(output, err)
flagSet.Usage()
return
}
if flagSet.Args()[0] == "all" {
fmt.Fprintln(output, controller.GetAllHabits())
return
}
h, err := parseHabit(flagSet.Args()[0], *frequency)
if err != nil {
fmt.Fprintln(output, err)
flagSet.Usage()
return
}
h, err = controller.Handle(h)
if err != nil {
fmt.Fprintln(output, err)
return
}
fmt.Fprintln(output, h)
}
//RunServer parses args and starts HTTP habit server on provided address
func RunServer(args []string, output io.Writer) {
if len(args) == 0 {
fmt.Fprintln(output, "no address provided")
return
}
if len(args) > 1 {
fmt.Fprintln(output, "too many args provided")
return
}
homeDir, err := homedir.Dir()
if err != nil {
fmt.Fprintln(output, err)
return
}
store, err := OpenDBStore(homeDir + "/.habitTracker.db")
if err != nil {
fmt.Fprintln(output, err)
return
}
controller, err := NewController(store)
if err != nil {
fmt.Fprintln(output, err)
return
}
server, err := NewServer(&controller, args[0])
if err != nil {
fmt.Fprintln(output, err)
return
}
fmt.Fprintln(output, "Starting HTTP server")
server.Run()
}
func storeFactory(storeType string, dir string) (store Store, err error) {
switch storeType {
case "db":
store, err = OpenDBStore(dir + "/.habitTracker.db")
case "file":
store, err = OpenFileStore(dir + "/.habitTracker")
default:
return nil, fmt.Errorf("unknown store type %s", storeType)
}
if err != nil {
return nil, err
}
return store, nil
}