This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
79 lines (66 loc) · 1.99 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
package main
import (
"flag"
"log"
"os"
"os/exec"
"runtime"
"github.com/dmk2014/momento2dayone/dayone"
"github.com/dmk2014/momento2dayone/momento"
)
func main() {
exportPath := flag.String("path",
"/dev/null",
"The Momento export path, containing Export.txt and attachments directory.")
expected := flag.Int("count",
-1,
"The number of entries that should be parsed from the Momento export file. Negative values are ignored.")
flag.Parse()
if err := initializeLog(); err != nil {
log.Fatal("Logger could not be initialized.")
}
log.Print("Momento2DayOne Session Beginning.")
if !isEnvironmentValid() {
log.Fatal("Invalid runtime environment.")
}
moments, err := momento.ParseFile(*exportPath)
if err != nil {
log.Fatal("Momento parse failed.")
}
if *expected >= 0 && *expected != len(moments) {
log.Fatalf("Moment count mismatch. Expected: %d. Actual: %d.", expected, len(moments))
}
entries := convertMomentToDayOne(moments)
dayone.Import(entries)
os.Exit(0)
}
func initializeLog() (err error) {
file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return
}
log.SetOutput(file)
return
}
func isEnvironmentValid() bool {
if runtime.GOOS != "darwin" {
log.Println("macOS Required (this is the only platform on which the Day One CLI is available).")
return false
}
if err := exec.Command("dayone2").Run(); err != nil {
log.Printf("Day One CLI not found. See %q for install instructions.",
"http://help.dayoneapp.com/day-one-2-0/command-line-interface-cli")
return false
}
return true
}
func convertMomentToDayOne(moments []momento.Moment) []dayone.DayOne {
// https://npf.io/2014/05/intro-to-go-interfaces/
// https://stackoverflow.com/questions/12994679/golang-slice-of-struct-slice-of-interface-it-implements
// TODO: research pointer receivers, conversion and duplication issue when using &m
entries := make([]dayone.DayOne, len(moments))
for i, m := range moments {
entries[i] = dayone.DayOne(m)
}
return entries
}