-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
138 lines (114 loc) · 3.49 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/jrswab/lsq/config"
"github.com/jrswab/lsq/system"
"github.com/jrswab/lsq/trie"
)
const semVer string = "1.0.0"
func main() {
// File Path Override
lsqDirPath := flag.String("d", "", "The path to the Logseq directory to use.")
apnd := flag.String("a", "", "Append text to the current journal page. This will not open $EDITOR.")
editorType := flag.String("e", "", "The external editor to use. Will use $EDITOR when blank or omitted.")
cliSearch := flag.String("f", "", "Search by file name in your pages directory.")
openFirstResult := flag.Bool("o", false, "Open the first result from search automatically.")
pageToOpen := flag.String("p", "", "Open a specific page from the pages directory. Must be a file name with extension.")
specDate := flag.String("s", "", "Open a specific journal. Use yyyy-MM-dd after the flag.")
version := flag.Bool("v", false, "Display current lsq version")
yesterday := flag.Bool("y", false, "Open yesterday's journal page")
flag.Parse()
if *version {
fmt.Println(semVer)
os.Exit(0)
}
cfg, err := config.Load()
if err != nil && !os.IsNotExist(err) {
// The user has a config file but we couldn't read it.
// Report the error instead of ignoring their configuration.
log.Printf("Error loading configuration: %v\n", err)
os.Exit(1)
}
// When this flag is used override the config.
if *lsqDirPath != "" {
cfg.DirPath = *lsqDirPath
cfg.JournalsDir = filepath.Join(cfg.DirPath, "journals")
cfg.PagesDir = filepath.Join(cfg.DirPath, "pages")
}
if *pageToOpen != "" {
pagePath := filepath.Join(cfg.PagesDir, *pageToOpen)
// Append to page and exit.
if *apnd != "" {
err := system.AppendToFile(pagePath, *apnd)
if err != nil {
log.Printf("Error appending data to file: %v\n", err)
os.Exit(1)
}
// Don't open $EDITOR when append flag is used.
return
}
// Open page in default editor if specified:
system.LoadEditor(*editorType, pagePath)
return
}
// Init Search only when "-f" is passed
var searchTrie *trie.Trie
if !strings.EqualFold(*cliSearch, "") {
searchTrie, err = trie.Init(cfg.PagesDir)
if err != nil {
log.Printf("error loading pages directory for search: %v\n", err)
os.Exit(1)
}
}
if *cliSearch != "" {
results := searchTrie.Search(*cliSearch)
if len(results) < 1 {
fmt.Println("No results found")
return
}
if *openFirstResult {
system.LoadEditor(*editorType, fmt.Sprintf("%s/%s", cfg.PagesDir, results[0]))
return
}
fmt.Fprintf(os.Stdout, "Search Results:\n")
for _, val := range results {
fmt.Println(val)
}
return
}
// Check that the journals directory exists
_, err = os.Stat(cfg.DirPath)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Could not find Logseq files at '%s'.\n", cfg.DirPath)
fmt.Printf("Make sure the path is correct and the directories exist./n")
os.Exit(0)
}
log.Printf("Error loading the main directory: %v\n", err)
os.Exit(1)
}
if *yesterday {
*specDate = time.Now().Add(-24 * time.Hour).Format("2006-01-02")
}
journalPath, err := system.GetJournal(cfg, cfg.JournalsDir, *specDate)
if err != nil {
log.Printf("Error setting journal path: %v\n", err)
os.Exit(1)
}
if *apnd != "" {
err := system.AppendToFile(journalPath, *apnd)
if err != nil {
log.Printf("Error appending data to file: %v\n", err)
os.Exit(1)
}
// Don't open $EDITOR when append flag is used.
return
}
system.LoadEditor(*editorType, journalPath)
}