-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
300 lines (253 loc) · 7.37 KB
/
Copy pathmain.go
File metadata and controls
300 lines (253 loc) · 7.37 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
////////////////////////////////////////////////////////////////////////////////
// Domain Specific Functionality
////////////////////////////////////////////////////////////////////////////////
func getPreviousWorkingDay(today time.Time) time.Time {
baseDate := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, time.Local)
for i := -1; i > -14; i-- {
adjustedDate := baseDate.AddDate(0, 0, i)
dow := adjustedDate.Weekday()
if dow >= time.Monday && dow <= time.Friday {
return adjustedDate
}
}
return today
}
func dateOffsetsToPaths(today time.Time, days []string) (outputPaths []string) {
parentPath := viper.GetString("DateBasePath")
for _, request := range days {
if request == "--" {
// We will get this because we tell cobra to not parse flags
continue
}
offset, err := strconv.Atoi(request)
var offDate time.Time
if err == nil {
offDate = today.AddDate(0, 0, offset)
} else {
if request == "y" {
offDate = getPreviousWorkingDay(today)
} else {
outputPaths = append(outputPaths, request)
continue
}
}
fileName := getFormattedFileName(offDate)
path := filepath.Join(parentPath, fileName)
outputPaths = append(outputPaths, path)
}
return
}
func getFormattedFileName(today time.Time) string {
var fileNameFormat string
if viper.IsSet("FileNameFormat") {
fileNameFormat = viper.GetString("FileNameFormat")
} else {
fileNameFormat = "YYYY-MM-DD"
}
// Convert config (obsidian) format to Golang
fmtString := strings.ReplaceAll(fileNameFormat, "YYYY", "2006")
fmtString = strings.ReplaceAll(fmtString, "MMM", "Jan")
fmtString = strings.ReplaceAll(fmtString, "MM", "01")
fmtString = strings.ReplaceAll(fmtString, "ddd", "Mon")
fmtString = strings.ReplaceAll(fmtString, "DD", "02")
fileName := today.Format(fmtString)
if !strings.HasSuffix(fileName, ".md") {
fileName += ".md"
}
return fileName
}
func runEditor(paths []string) {
var editorPath string
// Get editor path:
// 1. Use VIMLOG_EDITOR
// 2. Use $EDITOR
// 3. Look for nvim in path
if viper.IsSet("Editor") {
editorPath = viper.GetString("Editor")
} else if envPath := os.ExpandEnv("$EDITOR"); len(envPath) > 0 {
editorPath = envPath
} else {
editorPath = "nvim"
}
// We need a full path so make sure we have one
if expandedPath, err := exec.LookPath(editorPath); err == nil {
editorPath = expandedPath
} else {
ensureOutput()
log.Fatalf("Could not find editor %s", editorPath)
}
// options
editor_options := viper.GetStringSlice("EditorOptions")
// Setup VIMLOG_NOEDIT so user gets output
noEditMode := viper.GetBool("NoEdit")
if noEditMode {
ensureOutput()
}
// Put JUST the exename (not path) in $0
// If this is wrong then it will mess up tmux navigation
_, exeName := filepath.Split(editorPath)
if exeName == "" {
log.Fatalf("Could not get exename from %s", editorPath)
}
// Build entire command
args := []string{exeName}
args = append(args, editor_options...)
args = append(args, paths...)
log.Printf("Executing: %v", args)
if noEditMode {
log.Fatal("VIMLOG_NOEDIT=1 exiting")
}
// This syscall should replace the process, assuming it succeeds
err := syscall.Exec(editorPath, args, os.Environ())
if err != nil {
ensureOutput()
log.Printf("FATAL: spawning editor failed, make sure that %s exists and is executable", editorPath)
log.Fatal(err)
}
}
////////////////////////////////////////////////////////////////////////////////
// Options + Logging
////////////////////////////////////////////////////////////////////////////////
func loadOptions() {
//
// Defaults
//
viper.SetDefault("DateBasePath", "")
viper.SetDefault("EditorOptions", []string{})
//
// ENV overloading
//
viper.SetEnvPrefix("VIMLOG")
viper.AutomaticEnv()
//
// Config file
//
viper.SetConfigName(".vimlog")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config")
if err := viper.ReadInConfig(); err == nil {
log.Printf("Read in config file: %s\b", viper.ConfigFileUsed())
} else {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Println("No config file found, using defaults")
// No config file... that is okay, we will use defaults
} else {
log.Fatalf("Config file error %v", err)
}
}
//
// Logging (default no logging)
//
logFlags := 0
if viper.IsSet("LogFlags") {
// We don't want LogFlags in the example config so set it this way
logFlags = viper.GetInt("LogFlags")
} else if viper.IsSet("Silent") && viper.GetBool("Silent") {
// VIMLOG_LOGFLAGS will override VIMLOG_SILENT
// But setting VIMLOG_LOGFLAGS=0 != VIMLOG_SILENT=1
// as VIMLOG_SILENT=1 will not allow output to be enabled by certain
// commands (like config print)
logFlags = 0
}
log.SetFlags(logFlags)
if logFlags == 0 {
// LogFlags only control prefix (timestamp, etc)
// if LogFlags == 0 then also make sure that we don't output message
log.SetOutput(io.Discard)
}
//
// Debug / output
//
if viper.GetBool("Debug") {
log.Printf("Config: %v", viper.AllSettings())
}
}
// Called by functions that output text by the nature of their design
// (i.e. config print). This will ensure that logging output is
// actually enabled (default is disabled) for these commands
// Can be disabled via VIMLOG_SILENT=1 if you REALLY don't want output
func ensureOutput() {
if log.Flags() > 0 || viper.GetBool("Silent") {
return
}
log.SetFlags(log.LstdFlags)
log.SetOutput(os.Stderr)
}
////////////////////////////////////////////////////////////////////////////////
// Commands
////////////////////////////////////////////////////////////////////////////////
var configCmd = &cobra.Command{
Use: "config",
Run: func(_ *cobra.Command, args []string) {
log.Printf("config %v", args)
},
}
var configPrintCmd = &cobra.Command{
Use: "print",
Run: func(_ *cobra.Command, _ []string) {
ensureOutput()
log.Printf("Config file in use: %s", viper.ConfigFileUsed())
log.Printf("Config: %v", viper.AllSettings())
},
}
var configWriteCmd = &cobra.Command{
Use: "write",
Run: func(_ *cobra.Command, _ []string) {
if viper.ConfigFileUsed() == "" {
ensureOutput()
viper.SetConfigFile("./.vimlog.yaml")
log.Printf("No config file found...")
}
viper.WriteConfig()
log.Printf("Wrote config %s", viper.ConfigFileUsed())
},
}
var rootCmd = &cobra.Command{
Use: "vimlog",
Run: func(_ *cobra.Command, args []string) {
today := time.Now()
outputPaths := dateOffsetsToPaths(today, args)
if len(outputPaths) <= 0 {
outputPaths = dateOffsetsToPaths(today, []string{"0"})
log.Printf("No args, opening todays log; %s\n", outputPaths[0])
}
if viper.GetBool("Debug") {
log.Printf("Debug mode enabled, not running vim")
log.Printf("%v", outputPaths)
os.Exit(0)
}
runEditor(outputPaths)
},
DisableFlagParsing: true,
}
////////////////////////////////////////////////////////////////////////////////
// Entry Point
////////////////////////////////////////////////////////////////////////////////
func main() {
rootCmd.AddCommand(configCmd)
configCmd.AddCommand(configPrintCmd)
configCmd.AddCommand(configWriteCmd)
// load options and THEN output log line (in case options suppress logging)
loadOptions()
log.Println("vimlog - starting")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}