-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
172 lines (166 loc) · 3.89 KB
/
Copy pathcli.go
File metadata and controls
172 lines (166 loc) · 3.89 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"log"
"os"
"runtime"
"sort"
"time"
"github.com/urfave/cli/v2"
)
const RELEASE = "v20240323"
const ARCH = string(runtime.GOARCH)
const OS = string(runtime.GOOS + " ")
const VERSION = RELEASE + ", built for: " + OS + ARCH
func initCli() {
var filename string
app := &cli.App{
// app definition
Name: "ttt",
Version: VERSION,
Compiled: time.Now(),
Authors: []*cli.Author{
&cli.Author{
Name: "Stefan Rohrbacher (thefenriswolf)",
},
},
Usage: "CLI time tracker tool",
EnableBashCompletion: true,
Suggest: true,
HideVersion: false,
// flags
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "database .csv file to use",
Destination: &filename,
},
},
//commands
Commands: []*cli.Command{
&cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "create a template file in the current directory",
Action: func(cCtx *cli.Context) error {
writeTemplate(filename)
return nil
},
},
&cli.Command{
Name: "print",
Aliases: []string{"p"},
Usage: "print cleaned up source file to stdout",
Action: func(cCtx *cli.Context) error {
prettyPrint(filename, settings)
return nil
},
},
&cli.Command{
Name: "export",
Aliases: []string{"e"},
Usage: "export worktime report as PDF",
Description: "If no command is specified ttt will report in weekly mode",
Action: func(cCtx *cli.Context) error {
exportWorktime(filename, 2)
return nil
},
// subcommands for: graph
Subcommands: []*cli.Command{
&cli.Command{
Name: "month",
Usage: "generate monthly PDF report",
Aliases: []string{"m"},
Action: func(cCtx *cli.Context) error {
exportWorktime(filename, 1)
return nil
},
},
&cli.Command{
Name: "week",
Usage: "generate weekly graph",
Aliases: []string{"y"},
Action: func(cCtx *cli.Context) error {
exportWorktime(filename, 2)
return nil
},
},
},
},
&cli.Command{
Name: "report",
Aliases: []string{"rep", "r"},
Usage: "report worktime",
Description: "If no command is specified ttt will report in weekly mode",
Action: func(cCtx *cli.Context) error {
reportWorktime(filename, 1)
return nil
},
// subcommands for: report
Subcommands: []*cli.Command{
&cli.Command{
Name: "week",
Usage: "generate weekly report",
Aliases: []string{"w"},
Action: func(cCtx *cli.Context) error {
reportWorktime(filename, 1)
return nil
},
},
&cli.Command{
Name: "month",
Usage: "generate monthly report",
Aliases: []string{"m"},
Action: func(cCtx *cli.Context) error {
reportWorktime(filename, 2)
return nil
},
},
&cli.Command{
Name: "year",
Usage: "generate monthly report",
Aliases: []string{"y"},
Action: func(cCtx *cli.Context) error {
reportWorktime(filename, 3)
return nil
},
},
},
},
},
}
// sort flags and commands
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
// run flag parser and handle errors
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// helper report function
func reportWorktime(fn string, timeframe int) {
switch {
case timeframe == 1:
weekReport(fn, settings)
case timeframe == 2:
monthReport(fn, settings)
case timeframe == 3:
fmt.Println("yearly not implemented yet")
default:
fmt.Println("default not implemented yet")
return
}
}
func exportWorktime(fn string, timeframe int) {
switch {
case timeframe == 1:
createPDF(fn, "month")
case timeframe == 2:
createPDF(fn, "week")
default:
createPDF(fn, "week")
return
}
}