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}
13
14func NewConfig() config {
15 homePath := os.Getenv("HOME")
16 todoDir, exists := os.LookupEnv("TODO_DIR")
17
18 if !exists {
19 todoDir = filepath.Join(homePath, ".todo")
20 }
21
22 _ = os.MkdirAll(todoDir, os.ModePerm)
23
24 taskFilePath := filepath.Join(todoDir, "todo.txt")
25
26 if _, err := os.Stat(taskFilePath); os.IsNotExist(err) {
27 _, e := os.Create(taskFilePath)
28 if e != nil {
29 log.Fatal(e)
30 }
31
32 }
33
34 return config{
35 todoDir: todoDir,
36 taskFilePath: taskFilePath,
37 }
38}