-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.68 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
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"track/storage"
"track/tracker"
"track/types"
)
var tags string
var storageType string
const fileTimew = "file-timew"
const fileJSON = "file-json"
func main() {
startCmd := flag.NewFlagSet("start", flag.ExitOnError)
startCmd.StringVar(&tags, "tags", "", "Supply tags as comma separated list. Example: a,b,c")
startCmd.StringVar(&storageType, "storage", "file-json", "Supply the type of storage to be used (file-json or file-timew)")
stopCmd := flag.NewFlagSet("stop", flag.ExitOnError)
stopCmd.StringVar(&storageType, "storage", "file-json", "Supply the type of storage to be used (file-json or file-timew)")
_ = flag.NewFlagSet("stop", flag.ExitOnError)
if len(os.Args) < 2 {
_, _ = fmt.Fprintf(os.Stderr, "Use command `start` or `stop`\n")
os.Exit(1)
}
switch os.Args[1] {
case "start":
start(startCmd)
case "stop":
stop(stopCmd)
}
}
func start(startCmd *flag.FlagSet) {
_ = startCmd.Parse(os.Args[2:])
switch storageType {
case "file-json":
err := tracker.Start(getContext(), types.MakeTags(strings.Split(tags, ",")...))
if err != nil {
panic(err)
}
}
}
func stop(stopCmd *flag.FlagSet) {
_ = stopCmd.Parse(os.Args[2:])
err := tracker.Stop(getContext())
if err != nil {
panic(err)
}
}
func getContext() tracker.Context {
switch storageType {
case fileJSON:
return tracker.Context{
Storage: storage.NewDefaultJSONFileStorage(),
Time: time.Now(),
}
case fileTimew:
return tracker.Context{
Storage: storage.NewDefaultTimewFileStorage(),
Time: time.Now(),
}
default:
return tracker.Context{
Storage: storage.NewDefaultJSONFileStorage(),
Time: time.Now(),
}
}
}