package main import ( "flag" "log" "os" "path/filepath" ) type Config struct { todoDir string taskFilePath string doneFilePath string } func NewConfig() Config { todoDirArg := flag.String("dir", "", "Path to Todo directory") flag.Parse() todoDir := GetTodoDir(*todoDirArg) taskFilePath := filepath.Join(todoDir, "todo.txt") doneFilePath := filepath.Join(todoDir, "done.txt") if _, err := os.Stat(taskFilePath); os.IsNotExist(err) { _, e := os.Create(taskFilePath) if e != nil { log.Fatal(e) } } return Config{ todoDir: todoDir, taskFilePath: taskFilePath, doneFilePath: doneFilePath, } } func GetTodoDir(todoDirArg string) string { var todoDir string if todoDirArg == "" { envPath, exists := os.LookupEnv("TODO_DIR") if exists { todoDir = envPath } else { homePath := os.Getenv("HOME") todoDir = filepath.Join(homePath, ".todo") } } else { todoDir = todoDirArg } err := os.MkdirAll(todoDir, os.ModePerm) if err != nil { log.Fatal(err) } return todoDir }