From 568eff4218015fdb9336ac5978edacc923d25690 Mon Sep 17 00:00:00 2001 From: Liang-Shih Lin Date: Wed, 28 Aug 2024 09:53:45 +0800 Subject: [PATCH] fix: read and write config and csv from XDG_CONFIG_HOME --- config.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ data.go | 50 +--------------------------- main.go | 8 ++++- 3 files changed, 106 insertions(+), 50 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..0cabc1a --- /dev/null +++ b/config.go @@ -0,0 +1,98 @@ +package main + +import ( + "encoding/csv" + "encoding/json" + "fmt" + "log" + "os" + "strings" +) + +type Config struct { + DbPath string +} + +var ( + configHome = os.Getenv("XDG_CONFIG_HOME") + configDir = configHome + "/kancli" + configFile = configDir + "/config.json" +) + +func readConfig() Config { + mkdirErr := os.MkdirAll(configDir, 0755) + if mkdirErr != nil { + log.Fatal(mkdirErr) + } + + var config Config + if _, err := os.Stat(configFile); os.IsNotExist(err) { + file, err := os.Create(configFile) + if err != nil { + log.Fatal(err) + } + defer file.Close() + csvFile := configDir + "/tasks.csv" + file.WriteString(fmt.Sprintf("{\"dbPath\": \"%s\"}", csvFile)) + } + + data, readJSONFileErr := os.ReadFile(configFile) + if readJSONFileErr != nil { + log.Fatal(readJSONFileErr) + } + + if readJSONerr := json.Unmarshal(data, &config); readJSONerr != nil { + log.Fatal(readJSONerr) + } + + return config +} + +func readCSV() [][]string { + if _, err := os.Stat(csvFile); os.IsNotExist(err) { + file, err := os.Create(csvFile) + if err != nil { + log.Fatal(err) + } + defer file.Close() + file.WriteString("title,description,status\n") + } + + content, err := os.ReadFile(csvFile) + if err != nil { + log.Fatal(err) + } + + r := csv.NewReader(strings.NewReader(string(content))) + r.Comma = ',' + + // Skip the header + _, readErr := r.Read() + if readErr != nil { + log.Fatal(readErr) + } + + records, readAllErr := r.ReadAll() + if readAllErr != nil { + log.Fatal(readAllErr) + } + + return records +} + +func updateCSV() { + file, err := os.Create(csvFile) + if err != nil { + log.Fatal(err) + } + defer file.Close() + file.WriteString("title,description,status\n") + w := csv.NewWriter(file) + for _, col := range board.cols { + for _, item := range col.list.Items() { + task := item.(Task) + w.Write([]string{task.Title(), task.Description(), task.status.String()}) + } + } + w.Flush() +} diff --git a/data.go b/data.go index 62d2240..af3f6fb 100644 --- a/data.go +++ b/data.go @@ -1,11 +1,6 @@ package main import ( - "encoding/csv" - "log" - "os" - "strings" - "github.com/charmbracelet/bubbles/list" ) @@ -29,33 +24,7 @@ func (b *Board) initLists() { newColumn(done), } - if _, err := os.Stat("tasks.csv"); os.IsNotExist(err) { - file, err := os.Create("tasks.csv") - if err != nil { - log.Fatal(err) - } - defer file.Close() - file.WriteString("title,description,status\n") - } - - content, err := os.ReadFile("tasks.csv") - if err != nil { - log.Fatal(err) - } - - r := csv.NewReader(strings.NewReader(string(content))) - r.Comma = ',' - - // Skip the header - _, readErr := r.Read() - if readErr != nil { - log.Fatal(err) - } - - records, readAllErr := r.ReadAll() - if readAllErr != nil { - log.Fatal(err) - } + records := readCSV() listItems := [][]list.Item{ todo: {}, @@ -80,20 +49,3 @@ func (b *Board) initLists() { b.cols[done].list.Title = "Done" b.cols[done].list.SetItems(listItems[done]) } - -func updateCSV() { - file, err := os.Create("tasks.csv") - if err != nil { - log.Fatal(err) - } - defer file.Close() - file.WriteString("title,description,status\n") - w := csv.NewWriter(file) - for _, col := range board.cols { - for _, item := range col.list.Items() { - task := item.(Task) - w.Write([]string{task.Title(), task.Description(), task.status.String()}) - } - } - w.Flush() -} diff --git a/main.go b/main.go index 04f116d..6de37e2 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,10 @@ func (s status) String() string { const margin = 4 -var board *Board +var ( + board *Board + csvFile string +) const ( todo status = iota @@ -54,6 +57,9 @@ func main() { } defer f.Close() + config := readConfig() + csvFile = config.DbPath + board = NewBoard() board.initLists() p := tea.NewProgram(board)