all repos — todo.txt-go @ ecc9536e6199c2e91e6f6f1e6316afcc915f7bbf

CLI tool for todo.txt files written in Go

feat: :wrench: Config can be provided with flag
Tim Izzo tim@octree.ch
Sat, 12 Apr 2025 18:27:53 +0200
commit

ecc9536e6199c2e91e6f6f1e6316afcc915f7bbf

parent

9c189ec93227b6c005edbc66f1a83abe842c6285

4 files changed, 40 insertions(+), 18 deletions(-)

jump to
M .gitignore.gitignore

@@ -18,3 +18,6 @@ # Go workspace file

go.work *.txt + +# Ignore compiled file +todo-txt
M README.mdREADME.md

@@ -7,7 +7,7 @@ - [x] Implement archive mecanism

- [x] Allow task edition - [x] Allow to remove priority on task - [x] Circular navigation on tasks -- [ ] Provide config file with argument +- [x] Provide config file with argument ## Ressources
M config.goconfig.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.gomain.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)