-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (82 loc) · 2.05 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/makarski/gcaler/cmd"
"github.com/makarski/gcaler/cmd/list"
"github.com/makarski/gcaler/cmd/plan"
"github.com/makarski/gcaler/google/auth"
gcal "github.com/makarski/gcaler/google/calendar"
)
const (
appName = "gcaler"
planCmdName = "plan"
listCmdName = "list"
)
var (
fls *flag.FlagSet
tokenCacheDir string
tokenCacheFile string
templatesDir string
credentialsFile string
calId string
)
func init() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
tokenCacheDir = filepath.Join(os.Getenv("HOME"), "."+appName)
tokenCacheFile = filepath.Join(tokenCacheDir, "access_token.json")
fls = flag.NewFlagSet("", flag.ExitOnError)
fls.StringVar(&templatesDir, "templates", filepath.Join(wd, "templates"), "Path to templates directory")
fls.StringVar(&credentialsFile, "credentials", filepath.Join(wd, "client_secret.json"), "Credentials file name: absolute or relative path")
fls.StringVar(&calId, "email", calId, "Optional: email (calendar id) - used for 'list' subcmd")
fls.Usage = printHelp
}
func printHelp() {
txt := `
Calendar planner
USAGE:
gcaler [OPTIONS] [SUBCOMMAND]
SUBCOMMANDS:
plan Schedule an based on the template config
list List calendar events
OPTIONS:
`
fmt.Fprint(fls.Output(), txt)
fls.PrintDefaults()
}
func main() {
// parse flags
fls.Parse(os.Args[1:])
// parse subcommand
cmdName := fls.Arg(0)
cmdRun, err := func() (cmd.CmdFunc, error) {
switch cmdName {
case planCmdName:
return plan.Plan(templatesDir), nil
case listCmdName, "":
if calId == "" {
return nil, fmt.Errorf("`-email` option must be provided")
}
return list.List(calId), nil
}
return nil, fmt.Errorf("cmd: `%s` not found", cmdName)
}()
if err != nil {
panic(err)
}
gToken := auth.NewGToken(credentialsFile, tokenCacheFile, tokenCacheDir)
credCfg, err := gToken.Credentials()
if err != nil {
panic(err)
}
gCalendar := gcal.NewGCalerndar(&gToken, credCfg)
// execute
if err := cmdRun(gCalendar); err != nil {
panic(err)
}
}