Skip to content

Commit 70c49d1

Browse files
Target dir and config functions return errors instead of failing
Signed-off-by: Chris Cummer <chriscummer@me.com>
1 parent d1354f9 commit 70c49d1

File tree

2 files changed

+69
-70
lines changed

2 files changed

+69
-70
lines changed

til.go

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func loadConfig() {
153153
// getConfigDir returns the string path to the directory that should
154154
// contain the configuration file.
155155
// It tries to be XDG-compatible
156-
func getConfigDir() string {
156+
func getConfigDir() (string, error) {
157157
cDir := os.Getenv("XDG_CONFIG_HOME")
158158
if cDir == "" {
159159
cDir = tilConfigDir
@@ -166,27 +166,32 @@ func getConfigDir() string {
166166
// do not mess with it (because doing so makes the archlinux people
167167
// very cranky)
168168
if cDir[0] != '~' {
169-
return cDir
169+
return cDir, nil
170170
}
171171

172172
dir, err := os.UserHomeDir()
173173
if err != nil {
174-
Defeat(errors.New(errConfigExpandPath))
174+
return "", errors.New(errConfigExpandPath)
175175
}
176176

177-
return filepath.Join(dir, cDir[1:])
177+
return filepath.Join(dir, cDir[1:]), nil
178178
}
179179

180180
// 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+
}
184186

185-
return cPath
187+
return fmt.Sprintf("%s/%s", cDir, tilConfigFile), nil
186188
}
187189

188190
func makeConfigDir() {
189-
cDir := getConfigDir()
191+
cDir, err := getConfigDir()
192+
if err != nil {
193+
Defeat(err)
194+
}
190195

191196
if _, err := os.Stat(cDir); os.IsNotExist(err) {
192197
err := os.MkdirAll(cDir, os.ModePerm)
@@ -199,9 +204,12 @@ func makeConfigDir() {
199204
}
200205

201206
func makeConfigFile() {
202-
cPath := getConfigPath()
207+
cPath, err := getConfigPath()
208+
if err != nil {
209+
Defeat(err)
210+
}
203211

204-
_, err := os.Stat(cPath)
212+
_, err = os.Stat(cPath)
205213

206214
if err != nil {
207215
// Something went wrong trying to find the config file.
@@ -241,7 +249,10 @@ func makeConfigFile() {
241249
// readConfigFile reads the contents of the config file and jams them
242250
// into the global config variable
243251
func readConfigFile() {
244-
cPath := getConfigPath()
252+
cPath, err := getConfigPath()
253+
if err != nil {
254+
Defeat(err)
255+
}
245256

246257
cfg, err := config.ParseYamlFile(cPath)
247258
if err != nil {
@@ -257,7 +268,10 @@ func readConfigFile() {
257268
// the config file, exists and contains a /docs folder for writing pages to.
258269
// If these directories don't exist, it tries to create them
259270
func buildTargetDirectory() {
260-
tDir := getTargetDir(globalConfig, true)
271+
tDir, err := getTargetDir(globalConfig, true)
272+
if err != nil {
273+
Defeat(err)
274+
}
261275

262276
if _, err := os.Stat(tDir); os.IsNotExist(err) {
263277
err := os.MkdirAll(tDir, os.ModePerm)
@@ -269,7 +283,7 @@ func buildTargetDirectory() {
269283

270284
// getTargetDir returns the absolute string path to the directory that the
271285
// content will be written to
272-
func getTargetDir(cfg *config.Config, withDocsDir bool) string {
286+
func getTargetDir(cfg *config.Config, withDocsDir bool) (string, error) {
273287
docsBit := ""
274288
if withDocsDir {
275289
docsBit = "/docs"
@@ -283,7 +297,7 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) string {
283297
// b: ~/Documents/notes
284298
uDirs, err := cfg.Map("targetDirectories")
285299
if err != nil {
286-
Defeat(err)
300+
return "", err
287301
}
288302

289303
// config returns a map of [string]interface{} which is helpful on the
@@ -305,31 +319,31 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) string {
305319
}
306320
} else {
307321
if targetDirFlag == "" {
308-
Defeat(errors.New(errTargetDirFlag))
322+
return "", errors.New(errTargetDirFlag)
309323
}
310324

311325
tDir = tDirs[targetDirFlag]
312326
}
313327

314328
if tDir == "" {
315-
Defeat(errors.New(errTargetDirUndefined))
329+
return "", errors.New(errTargetDirUndefined)
316330
}
317331

318332
// If we're not using a path relative to the user's home directory,
319333
// take the config value as a fully-qualified path and just append the
320334
// name of the write dir to it
321335
if tDir[0] != '~' {
322-
return tDir + docsBit
336+
return tDir + docsBit, nil
323337
}
324338

325339
// We are pathing relative to the home directory, so figure out the
326340
// absolute path for that
327341
dir, err := os.UserHomeDir()
328342
if err != nil {
329-
Defeat(errors.New(errConfigExpandPath))
343+
return "", errors.New(errConfigExpandPath)
330344
}
331345

332-
return filepath.Join(dir, tDir[1:], docsBit)
346+
return filepath.Join(dir, tDir[1:], docsBit), nil
333347
}
334348

335349
/* -------------------- Helper functions -------------------- */
@@ -364,13 +378,18 @@ func buildIndexPage(pages []*Page, tagMap *TagMap) {
364378
content += footer()
365379

366380
// And write the file to disk
381+
tDir, err := getTargetDir(globalConfig, true)
382+
if err != nil {
383+
Defeat(err)
384+
}
385+
367386
filePath := fmt.Sprintf(
368387
"%s/index.%s",
369-
getTargetDir(globalConfig, true),
388+
tDir,
370389
fileExtension,
371390
)
372391

373-
err := ioutil.WriteFile(filePath, []byte(content), 0644)
392+
err = ioutil.WriteFile(filePath, []byte(content), 0644)
374393
if err != nil {
375394
Defeat(err)
376395
}
@@ -402,14 +421,19 @@ func buildTagPages(pages []*Page) *TagMap {
402421
content += footer()
403422

404423
// And write the file to disk
424+
tDir, err := getTargetDir(globalConfig, true)
425+
if err != nil {
426+
Defeat(err)
427+
}
428+
405429
filePath := fmt.Sprintf(
406430
"%s/%s.%s",
407-
getTargetDir(globalConfig, true),
431+
tDir,
408432
tagName,
409433
fileExtension,
410434
)
411435

412-
err := ioutil.WriteFile(filePath, []byte(content), 0644)
436+
err = ioutil.WriteFile(filePath, []byte(content), 0644)
413437
if err != nil {
414438
Defeat(err)
415439
}
@@ -439,15 +463,20 @@ func createNewPage(title string) string {
439463
content := frontMatter + fmt.Sprintf("# %s\n\n\n", title)
440464

441465
// 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+
442471
filePath := fmt.Sprintf(
443472
"%s/%s-%s.%s",
444-
getTargetDir(globalConfig, true),
473+
tDir,
445474
pathDate,
446475
strings.ReplaceAll(strings.ToLower(title), " ", "-"),
447476
fileExtension,
448477
)
449478

450-
err := ioutil.WriteFile(filePath, []byte(content), 0644)
479+
err = ioutil.WriteFile(filePath, []byte(content), 0644)
451480
if err != nil {
452481
Defeat(err)
453482
}
@@ -473,10 +502,15 @@ func createNewPage(title string) string {
473502
func loadPages() []*Page {
474503
pages := []*Page{}
475504

505+
tDir, err := getTargetDir(globalConfig, true)
506+
if err != nil {
507+
Defeat(err)
508+
}
509+
476510
filePaths, _ := filepath.Glob(
477511
fmt.Sprintf(
478512
"%s/*.%s",
479-
getTargetDir(globalConfig, true),
513+
tDir,
480514
fileExtension,
481515
),
482516
)
@@ -526,7 +560,10 @@ func parseTitle(targetFlag string, args []string) string {
526560
func push() {
527561
Info(statusRepoPush)
528562

529-
tDir := getTargetDir(globalConfig, false)
563+
tDir, err := getTargetDir(globalConfig, false)
564+
if err != nil {
565+
Defeat(err)
566+
}
530567

531568
r, err := git.PlainOpen(tDir)
532569
if err != nil {
@@ -563,7 +600,10 @@ func readPage(filePath string) *Page {
563600
func save(commitMsg string) {
564601
Info(statusRepoSave)
565602

566-
tDir := getTargetDir(globalConfig, false)
603+
tDir, err := getTargetDir(globalConfig, false)
604+
if err != nil {
605+
Defeat(err)
606+
}
567607

568608
r, err := git.PlainOpen(tDir)
569609
if err != nil {

til_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,9 @@ package main
33
import (
44
"testing"
55

6-
"github.com/olebedev/config"
76
"github.com/stretchr/testify/assert"
87
)
98

10-
/* -------------------- Target Directory -------------------- */
11-
12-
func Test_getTargetDir(t *testing.T) {
13-
tests := []struct {
14-
name string
15-
config *config.Config
16-
withDocsDir bool
17-
expectedPath string
18-
expectedErr error
19-
}{
20-
{
21-
name: "with no targetDirectories config key",
22-
},
23-
{
24-
name: "with no target directories defined",
25-
},
26-
{
27-
name: "with one target directory defined",
28-
},
29-
{
30-
name: "with multiple target directories defined and invalid key",
31-
},
32-
{
33-
name: "with multiple target directories defined and valid key",
34-
},
35-
{
36-
name: "with a home directory-relative path",
37-
},
38-
{
39-
name: "with an absolute path",
40-
},
41-
}
42-
43-
for _, tt := range tests {
44-
t.Run(tt.name, func(t *testing.T) {
45-
46-
})
47-
}
48-
}
49-
509
/* -------------------- More Helper Functions -------------------- */
5110

5211
func Test_Colour(t *testing.T) {

0 commit comments

Comments
 (0)