all repos — todo.txt-go @ 79fc5d605316916243adc3f0e252a2ec5a1a314e

config.go (view raw)

 1package main
 2
 3import (
 4	"log"
 5	"os"
 6	"path/filepath"
 7)
 8
 9type config struct {
10	todoDir      string
11	taskFilePath string
12	doneFilePath string
13}
14
15func NewConfig() config {
16	homePath := os.Getenv("HOME")
17	todoDir, exists := os.LookupEnv("TODO_DIR")
18
19	if !exists {
20		todoDir = filepath.Join(homePath, ".todo")
21	}
22
23	_ = os.MkdirAll(todoDir, os.ModePerm)
24
25	taskFilePath := filepath.Join(todoDir, "todo.txt")
26	doneFilePath := filepath.Join(todoDir, "done.txt")
27
28	if _, err := os.Stat(taskFilePath); os.IsNotExist(err) {
29		_, e := os.Create(taskFilePath)
30		if e != nil {
31			log.Fatal(e)
32		}
33
34	}
35
36	return config{
37		todoDir:      todoDir,
38		taskFilePath: taskFilePath,
39		doneFilePath: doneFilePath,
40	}
41}