This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (153 loc) · 3.23 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"os"
"github.com/codegangsta/cli"
)
const (
appName = "hranoprovod-cli"
appUsage = "Lifestyle tracker"
appVersion = "2.0.2"
appAuthor = "aquilax"
appEmail = "aquilax@gmail.com"
defaultDbFilename = "food.yaml"
defaultLogFilename = "log.yaml"
defaultResolverMaxDepth = 10
)
func main() {
app := cli.NewApp()
app.Name = appName
app.Usage = appUsage
app.Version = appVersion
app.Author = appAuthor
app.Email = appEmail
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "database, d",
Value: defaultDbFilename,
Usage: "database file name",
EnvVar: "HR_DATABASE",
},
cli.StringFlag{
Name: "logfile, l",
Value: defaultLogFilename,
Usage: "log file name",
EnvVar: "HR_LOGFILE",
},
cli.StringFlag{
Name: "config, c",
Value: GetDefaultFileName(),
Usage: "Configuration file",
EnvVar: "HR_CONFIG",
},
cli.StringFlag{
Name: "date-format",
Value: "2006/01/02",
Usage: "Date format for parsing and printing dates",
EnvVar: "HR_DATE_FORMAT",
},
}
app.Commands = []cli.Command{
{
Name: "register",
ShortName: "reg",
Usage: "Shows the log register report",
Flags: []cli.Flag{
cli.StringFlag{
Name: "begin, b",
Usage: "Beginning of period",
},
cli.StringFlag{
Name: "end, e",
Usage: "End of period",
},
cli.StringFlag{
Name: "single-food, f",
Usage: "Show only single element",
},
cli.StringFlag{
Name: "single-element, s",
Usage: "Show only single element",
},
cli.BoolFlag{
Name: "csv",
Usage: "Export as CSV",
},
cli.BoolFlag{
Name: "no-color",
Usage: "Disable color output",
},
cli.BoolFlag{
Name: "no-totals",
Usage: "Disable totals",
},
cli.BoolFlag{
Name: "totals-only",
Usage: "Disable totals",
},
cli.BoolFlag{
Name: "unresolved",
Usage: "Show unresolved elements only",
},
cli.IntFlag{
Name: "maxdepth",
Value: defaultResolverMaxDepth,
Usage: "resolve depth",
EnvVar: "HR_MAXDEPTH",
},
},
Action: func(c *cli.Context) {
o := NewOptions()
if err := o.Load(c); err != nil {
handleExit(err)
}
handleExit(NewHranoprovod(o).Register())
},
},
{
Name: "add",
Usage: "Adds new item to the log",
Action: func(c *cli.Context) {
o := NewOptions()
if err := o.Load(c); err != nil {
handleExit(err)
}
handleExit(NewHranoprovod(o).Add(c.Args().First(), c.Args().Get(1)))
},
},
{
Name: "api",
Usage: "Service API commands",
Subcommands: []cli.Command{
{
Name: "search",
Usage: "Search for food online",
Action: func(c *cli.Context) {
o := NewOptions()
if err := o.Load(c); err != nil {
handleExit(err)
}
handleExit(NewHranoprovod(o).Search(c.Args().First()))
},
},
},
},
{
Name: "lint",
Usage: "Lints file",
Action: func(c *cli.Context) {
o := NewOptions()
if err := o.Load(c); err != nil {
handleExit(err)
}
handleExit(NewHranoprovod(o).Lint(c.Args().First()))
},
},
}
app.Run(os.Args)
}
func handleExit(err error) {
if err != nil {
println(err.Error())
os.Exit(1)
}
}