all repos — todo.txt-go @ 24dab3aa6d3fb735b8ea16d425ca32fe7c6e115f

CLI tool for todo.txt files written in Go

config.go (view raw)

 1package main
 2
 3import (
 4	"flag"
 5	"log"
 6	"os"
 7	"path/filepath"
 8)
 9
10type Config struct {
11	todoDir      string
12	taskFilePath string
13	doneFilePath string
14}
15
16func NewConfig() Config {
17	todoDirArg := flag.String("dir", "", "Path to Todo directory")
18	flag.Parse()
19
20	todoDir := GetTodoDir(*todoDirArg)
21	taskFilePath := filepath.Join(todoDir, "todo.txt")
22	doneFilePath := filepath.Join(todoDir, "done.txt")
23
24	if _, err := os.Stat(taskFilePath); os.IsNotExist(err) {
25		_, e := os.Create(taskFilePath)
26		if e != nil {
27			log.Fatal(e)
28		}
29
30	}
31
32	return Config{
33		todoDir:      todoDir,
34		taskFilePath: taskFilePath,
35		doneFilePath: doneFilePath,
36	}
37}
38
39func GetTodoDir(todoDirArg string) string {
40	var todoDir string
41
42	if todoDirArg == "" {
43		envPath, exists := os.LookupEnv("TODO_DIR")
44		if exists {
45			todoDir = envPath
46		} else {
47			homePath := os.Getenv("HOME")
48			todoDir = filepath.Join(homePath, ".todo")
49		}
50	} else {
51		todoDir = todoDirArg
52	}
53
54	err := os.MkdirAll(todoDir, os.ModePerm)
55
56	if err != nil {
57		log.Fatal(err)
58	}
59
60	return todoDir
61}