-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (53 loc) · 1.78 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
package main
import (
"flag"
"log"
"log/slog"
"time"
"github.com/CodedMasonry/cc-printer/common"
"github.com/CodedMasonry/cc-printer/printer"
"github.com/CodedMasonry/cc-printer/providers"
"github.com/CodedMasonry/cc-printer/providers/google"
"github.com/CodedMasonry/cc-printer/providers/protonmail"
)
func main() {
reset := flag.Bool("reset", false, "Reset the Config and State of the program")
flag.Parse()
common.InitLogging()
if *reset {
common.DeleteAppState()
}
common.GlobalConfig = common.FetchConfig()
common.GlobalState = common.FetchState()
common.GlobalConfig.SaveToFile()
common.GlobalState.SaveToFile()
slog.Info("State successfully initialized", "ConfigDir", common.ConfigDir)
provider := fetchProvider(common.GlobalConfig.Provider)
refreshTime := time.Now().Add(12 * time.Hour)
for {
slog.Debug("Fetching files", "last", common.GlobalState.LastFetch)
result := provider.GetAttachments(common.GlobalState.LastFetch, common.GlobalConfig.DeletePrinted)
for _, file := range result {
slog.Info("Attachment Downloaded", "file", file.Name())
printer.PrintFile(file)
}
common.GlobalState.LastFetch = time.Now().UTC()
go common.GlobalState.SaveToFile()
// Restart the provider service if it has been live for over 12 hours (to force a token refresh)
if time.Now().After(refreshTime) {
provider = fetchProvider(common.GlobalConfig.Provider)
}
time.Sleep(1 * time.Minute)
}
}
func fetchProvider(provider string) providers.Provider {
switch provider {
case "google":
return google.InitProvider(common.GlobalConfig.DeletePrinted, common.GlobalConfig.AllowedSenders)
case "protonmail":
return protonmail.InitProvider(common.GlobalConfig.AllowedSenders)
default:
log.Fatal("Unknown or Unsupported provider: ", provider)
}
panic("Unreachable")
}