@@ -153,7 +153,7 @@ func loadConfig() {
153
153
// getConfigDir returns the string path to the directory that should
154
154
// contain the configuration file.
155
155
// It tries to be XDG-compatible
156
- func getConfigDir () string {
156
+ func getConfigDir () ( string , error ) {
157
157
cDir := os .Getenv ("XDG_CONFIG_HOME" )
158
158
if cDir == "" {
159
159
cDir = tilConfigDir
@@ -166,27 +166,32 @@ func getConfigDir() string {
166
166
// do not mess with it (because doing so makes the archlinux people
167
167
// very cranky)
168
168
if cDir [0 ] != '~' {
169
- return cDir
169
+ return cDir , nil
170
170
}
171
171
172
172
dir , err := os .UserHomeDir ()
173
173
if err != nil {
174
- Defeat ( errors .New (errConfigExpandPath ) )
174
+ return "" , errors .New (errConfigExpandPath )
175
175
}
176
176
177
- return filepath .Join (dir , cDir [1 :])
177
+ return filepath .Join (dir , cDir [1 :]), nil
178
178
}
179
179
180
180
// getConfigPath returns the string path to the configuration file
181
- func getConfigPath () string {
182
- cDir := getConfigDir ()
183
- cPath := fmt .Sprintf ("%s/%s" , cDir , tilConfigFile )
181
+ func getConfigPath () (string , error ) {
182
+ cDir , err := getConfigDir ()
183
+ if err != nil {
184
+ return "" , err
185
+ }
184
186
185
- return cPath
187
+ return fmt . Sprintf ( "%s/%s" , cDir , tilConfigFile ), nil
186
188
}
187
189
188
190
func makeConfigDir () {
189
- cDir := getConfigDir ()
191
+ cDir , err := getConfigDir ()
192
+ if err != nil {
193
+ Defeat (err )
194
+ }
190
195
191
196
if _ , err := os .Stat (cDir ); os .IsNotExist (err ) {
192
197
err := os .MkdirAll (cDir , os .ModePerm )
@@ -199,9 +204,12 @@ func makeConfigDir() {
199
204
}
200
205
201
206
func makeConfigFile () {
202
- cPath := getConfigPath ()
207
+ cPath , err := getConfigPath ()
208
+ if err != nil {
209
+ Defeat (err )
210
+ }
203
211
204
- _ , err : = os .Stat (cPath )
212
+ _ , err = os .Stat (cPath )
205
213
206
214
if err != nil {
207
215
// Something went wrong trying to find the config file.
@@ -241,7 +249,10 @@ func makeConfigFile() {
241
249
// readConfigFile reads the contents of the config file and jams them
242
250
// into the global config variable
243
251
func readConfigFile () {
244
- cPath := getConfigPath ()
252
+ cPath , err := getConfigPath ()
253
+ if err != nil {
254
+ Defeat (err )
255
+ }
245
256
246
257
cfg , err := config .ParseYamlFile (cPath )
247
258
if err != nil {
@@ -257,7 +268,10 @@ func readConfigFile() {
257
268
// the config file, exists and contains a /docs folder for writing pages to.
258
269
// If these directories don't exist, it tries to create them
259
270
func buildTargetDirectory () {
260
- tDir := getTargetDir (globalConfig , true )
271
+ tDir , err := getTargetDir (globalConfig , true )
272
+ if err != nil {
273
+ Defeat (err )
274
+ }
261
275
262
276
if _ , err := os .Stat (tDir ); os .IsNotExist (err ) {
263
277
err := os .MkdirAll (tDir , os .ModePerm )
@@ -269,7 +283,7 @@ func buildTargetDirectory() {
269
283
270
284
// getTargetDir returns the absolute string path to the directory that the
271
285
// content will be written to
272
- func getTargetDir (cfg * config.Config , withDocsDir bool ) string {
286
+ func getTargetDir (cfg * config.Config , withDocsDir bool ) ( string , error ) {
273
287
docsBit := ""
274
288
if withDocsDir {
275
289
docsBit = "/docs"
@@ -283,7 +297,7 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) string {
283
297
// b: ~/Documents/notes
284
298
uDirs , err := cfg .Map ("targetDirectories" )
285
299
if err != nil {
286
- Defeat ( err )
300
+ return "" , err
287
301
}
288
302
289
303
// config returns a map of [string]interface{} which is helpful on the
@@ -305,31 +319,31 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) string {
305
319
}
306
320
} else {
307
321
if targetDirFlag == "" {
308
- Defeat ( errors .New (errTargetDirFlag ) )
322
+ return "" , errors .New (errTargetDirFlag )
309
323
}
310
324
311
325
tDir = tDirs [targetDirFlag ]
312
326
}
313
327
314
328
if tDir == "" {
315
- Defeat ( errors .New (errTargetDirUndefined ) )
329
+ return "" , errors .New (errTargetDirUndefined )
316
330
}
317
331
318
332
// If we're not using a path relative to the user's home directory,
319
333
// take the config value as a fully-qualified path and just append the
320
334
// name of the write dir to it
321
335
if tDir [0 ] != '~' {
322
- return tDir + docsBit
336
+ return tDir + docsBit , nil
323
337
}
324
338
325
339
// We are pathing relative to the home directory, so figure out the
326
340
// absolute path for that
327
341
dir , err := os .UserHomeDir ()
328
342
if err != nil {
329
- Defeat ( errors .New (errConfigExpandPath ) )
343
+ return "" , errors .New (errConfigExpandPath )
330
344
}
331
345
332
- return filepath .Join (dir , tDir [1 :], docsBit )
346
+ return filepath .Join (dir , tDir [1 :], docsBit ), nil
333
347
}
334
348
335
349
/* -------------------- Helper functions -------------------- */
@@ -364,13 +378,18 @@ func buildIndexPage(pages []*Page, tagMap *TagMap) {
364
378
content += footer ()
365
379
366
380
// And write the file to disk
381
+ tDir , err := getTargetDir (globalConfig , true )
382
+ if err != nil {
383
+ Defeat (err )
384
+ }
385
+
367
386
filePath := fmt .Sprintf (
368
387
"%s/index.%s" ,
369
- getTargetDir ( globalConfig , true ) ,
388
+ tDir ,
370
389
fileExtension ,
371
390
)
372
391
373
- err : = ioutil .WriteFile (filePath , []byte (content ), 0644 )
392
+ err = ioutil .WriteFile (filePath , []byte (content ), 0644 )
374
393
if err != nil {
375
394
Defeat (err )
376
395
}
@@ -402,14 +421,19 @@ func buildTagPages(pages []*Page) *TagMap {
402
421
content += footer ()
403
422
404
423
// And write the file to disk
424
+ tDir , err := getTargetDir (globalConfig , true )
425
+ if err != nil {
426
+ Defeat (err )
427
+ }
428
+
405
429
filePath := fmt .Sprintf (
406
430
"%s/%s.%s" ,
407
- getTargetDir ( globalConfig , true ) ,
431
+ tDir ,
408
432
tagName ,
409
433
fileExtension ,
410
434
)
411
435
412
- err : = ioutil .WriteFile (filePath , []byte (content ), 0644 )
436
+ err = ioutil .WriteFile (filePath , []byte (content ), 0644 )
413
437
if err != nil {
414
438
Defeat (err )
415
439
}
@@ -439,15 +463,20 @@ func createNewPage(title string) string {
439
463
content := frontMatter + fmt .Sprintf ("# %s\n \n \n " , title )
440
464
441
465
// Write out the stub file, explode if we can't do that
466
+ tDir , err := getTargetDir (globalConfig , true )
467
+ if err != nil {
468
+ Defeat (err )
469
+ }
470
+
442
471
filePath := fmt .Sprintf (
443
472
"%s/%s-%s.%s" ,
444
- getTargetDir ( globalConfig , true ) ,
473
+ tDir ,
445
474
pathDate ,
446
475
strings .ReplaceAll (strings .ToLower (title ), " " , "-" ),
447
476
fileExtension ,
448
477
)
449
478
450
- err : = ioutil .WriteFile (filePath , []byte (content ), 0644 )
479
+ err = ioutil .WriteFile (filePath , []byte (content ), 0644 )
451
480
if err != nil {
452
481
Defeat (err )
453
482
}
@@ -473,10 +502,15 @@ func createNewPage(title string) string {
473
502
func loadPages () []* Page {
474
503
pages := []* Page {}
475
504
505
+ tDir , err := getTargetDir (globalConfig , true )
506
+ if err != nil {
507
+ Defeat (err )
508
+ }
509
+
476
510
filePaths , _ := filepath .Glob (
477
511
fmt .Sprintf (
478
512
"%s/*.%s" ,
479
- getTargetDir ( globalConfig , true ) ,
513
+ tDir ,
480
514
fileExtension ,
481
515
),
482
516
)
@@ -526,7 +560,10 @@ func parseTitle(targetFlag string, args []string) string {
526
560
func push () {
527
561
Info (statusRepoPush )
528
562
529
- tDir := getTargetDir (globalConfig , false )
563
+ tDir , err := getTargetDir (globalConfig , false )
564
+ if err != nil {
565
+ Defeat (err )
566
+ }
530
567
531
568
r , err := git .PlainOpen (tDir )
532
569
if err != nil {
@@ -563,7 +600,10 @@ func readPage(filePath string) *Page {
563
600
func save (commitMsg string ) {
564
601
Info (statusRepoSave )
565
602
566
- tDir := getTargetDir (globalConfig , false )
603
+ tDir , err := getTargetDir (globalConfig , false )
604
+ if err != nil {
605
+ Defeat (err )
606
+ }
567
607
568
608
r , err := git .PlainOpen (tDir )
569
609
if err != nil {
0 commit comments