Skip to content

Commit

Permalink
fix: read and write config and csv from XDG_CONFIG_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
ll931217 committed Aug 28, 2024
1 parent dc33b50 commit 568eff4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 50 deletions.
98 changes: 98 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -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()
}
50 changes: 1 addition & 49 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package main

import (
"encoding/csv"
"log"
"os"
"strings"

"github.com/charmbracelet/bubbles/list"
)

Expand All @@ -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: {},
Expand All @@ -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()
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func (s status) String() string {

const margin = 4

var board *Board
var (
board *Board
csvFile string
)

const (
todo status = iota
Expand All @@ -54,6 +57,9 @@ func main() {
}
defer f.Close()

config := readConfig()
csvFile = config.DbPath

board = NewBoard()
board.initLists()
p := tea.NewProgram(board)
Expand Down

0 comments on commit 568eff4

Please sign in to comment.