feat: :wrench: Config can be provided with flag
Tim Izzo tim@octree.ch
Sat, 12 Apr 2025 18:27:53 +0200
4 files changed,
40 insertions(+),
18 deletions(-)
M
.gitignore
→
.gitignore
@@ -18,3 +18,6 @@ # Go workspace file
go.work *.txt + +# Ignore compiled file +todo-txt
M
config.go
→
config.go
@@ -1,27 +1,23 @@
package main import ( + "flag" "log" "os" "path/filepath" ) -type config struct { +type Config struct { todoDir string taskFilePath string doneFilePath string } -func NewConfig() config { - homePath := os.Getenv("HOME") - todoDir, exists := os.LookupEnv("TODO_DIR") - - if !exists { - todoDir = filepath.Join(homePath, ".todo") - } - - _ = os.MkdirAll(todoDir, os.ModePerm) +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")@@ -33,9 +29,33 @@ }
} - return config{ + 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 +}
M
main.go
→
main.go
@@ -23,7 +23,7 @@ )
type model struct { interfaceState InterfaceState - config config + config Config keys keyMap help help.Model@@ -34,9 +34,7 @@
textInput textinput.Model } -func initialModel() model { - - config := NewConfig() +func initialModel(config Config) model { // New task input ti := textinput.New()@@ -278,7 +276,8 @@
} func main() { - p := tea.NewProgram(initialModel(), tea.WithAltScreen()) + config := NewConfig() + p := tea.NewProgram(initialModel(config), tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1)