-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
205 lines (177 loc) · 5.33 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"flag"
"log"
"os"
"regexp"
"strings"
"github.com/dstotijn/go-notion"
"github.com/go-yaml/yaml"
)
var (
flagCmd = flag.String("cmd", "", "Run command")
flagExecOne = flag.String("one", "", "Run with one ID") // special for some commands
flagMulti = flag.Bool("multi", false, "Multiple configs in the config file")
flagMultiIdx = flag.Int("idx", -1, "Use a specific config by index in the multiple config") // start from index 0
flagRepeat = flag.Int("repeat", 1, "Repeat this command") // start with default 1 time
flagConfigPath = flag.String("config", "", "Path to config file")
flagDebugMode = flag.Bool("debug", false, "Enable debug mode")
)
var (
layoutDate = "2006-01-02" // date format used in journal title
hashIDRegex = regexp.MustCompile("([a-zA-Z0-9]{32})") // to extract the pageID
)
type Config struct {
Flashback FlashbackConfig `yaml:"flashback"`
DailyJournal DailyJournalConfig `yaml:"dailyJournal"`
WeeklyJournal WeeklyJournalConfig `yaml:"weeklyJournal"`
DuplicateChecker DuplicateCheckerConfig `yaml:"duplicateChecker"`
Collector CollectorConfig `yaml:"collector"`
Exporter ExporterConfig `yaml:"exporter"`
LLM LangModelConfig `yaml:"llm"`
}
type Cmd interface {
// Validate check the config are correct
Validate() error
// Run the cmd
Run() error
}
func main() {
flag.Parse()
if *flagExecOne != "" && strings.HasPrefix(*flagExecOne, "https:") {
matches := hashIDRegex.FindStringSubmatch(*flagExecOne)
// use the ID only
if len(matches) > 1 {
*flagExecOne = matches[1]
log.Printf("Parsed ID: %v", *flagExecOne)
}
}
notionClient := newNotionClient()
if *flagMulti {
configs := loadMultiConfig(*flagConfigPath)
if *flagDebugMode {
log.Printf("MultiConfig len: %v", len(configs))
}
repeat(func() {
if *flagMultiIdx >= 0 {
runCmd(notionClient, configs[*flagMultiIdx])
} else {
for _, cfg := range configs {
runCmd(notionClient, cfg)
}
}
}, *flagRepeat)
} else {
config := loadConfig(*flagConfigPath)
repeat(func() {
runCmd(notionClient, config)
}, *flagRepeat)
}
}
func repeat(do func(), times int) {
for i := 0; i < times; i++ {
do()
}
}
func runCmd(notionClient *notion.Client, cfg Config) {
if *flagDebugMode {
log.Printf("Run cmd: %v, config: %+v", *flagCmd, cfg)
} else {
log.Printf("Run cmd: %v", *flagCmd)
}
var cmd Cmd
switch *flagCmd {
case "daily-journal": // create daily journal entries with title YYYY-MM-DD
cmd = &DailyJournal{
DebugMode: *flagDebugMode,
Client: notionClient,
DailyJournalConfig: cfg.DailyJournal,
}
case "weekly-journal": // create weekly journal entries with title like YYYY-MM-DD/YYYY-MM-DD
cmd = &WeeklyJournal{
DebugMode: *flagDebugMode,
Client: notionClient,
WeeklyJournalConfig: cfg.WeeklyJournal,
}
case "flashback": // get a random page from a database and resurface it
cmd = &Flashback{
DebugMode: *flagDebugMode,
Client: notionClient,
FlashbackConfig: cfg.Flashback,
}
case "duplicate": // find duplicated pages (same title) in a database
cmd = &DuplicateChecker{
DebugMode: *flagDebugMode,
Client: notionClient,
DuplicateCheckerConfig: cfg.DuplicateChecker,
}
case "collector": // find certain pages from a database and dump the delta pages in a page
cmd = &Collector{
DebugMode: *flagDebugMode,
Client: notionClient,
CollectorConfig: cfg.Collector,
}
case "export": // export pages from a database into local folders in markdown
cmd = &Exporter{
DebugMode: *flagDebugMode,
ExecOne: *flagExecOne,
Client: notionClient,
ExporterConfig: cfg.Exporter,
}
case "llm": // run custom prompt on pages from a database
cmd = &LangModel{
DebugMode: *flagDebugMode,
ExecOne: *flagExecOne,
Client: notionClient,
LangModelConfig: cfg.LLM,
}
default:
log.Fatalf("Unknown cmd: `%v`", *flagCmd)
}
if err := cmd.Validate(); err != nil {
log.Fatalf("cmd %v validate failed: %+v", *flagCmd, err)
}
if err := cmd.Run(); err != nil {
log.Fatalf("cmd %v error: %+v", *flagCmd, err)
}
log.Printf("cmd %v completed", *flagCmd)
}
func newNotionClient() *notion.Client {
notionToken := os.Getenv("NOTION_TOKEN")
if notionToken == "" {
log.Println("Empty Token in env.NOTION_TOKEN")
os.Exit(1)
}
return notion.NewClient(notionToken)
}
func loadConfig(configPath string) Config {
if configPath == "" && *flagExecOne != "" {
return Config{} // allow empty config for execOne
}
configFile, err := os.ReadFile(configPath)
if err != nil {
log.Printf("Error in Config File (%v): %v", configPath, err)
os.Exit(2)
}
config := Config{}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
log.Printf("Error in unmarshal Config: %v", err)
os.Exit(2)
}
return config
}
func loadMultiConfig(configPath string) []Config {
configFile, err := os.ReadFile(configPath)
if err != nil {
log.Printf("Error in Config File (%v): %v", configPath, err)
os.Exit(2)
}
configs := []Config{}
err = yaml.Unmarshal(configFile, &configs)
if err != nil {
log.Printf("Error in unmarshal Config: %v", err)
os.Exit(2)
}
return configs
}