-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: read and write config and csv from XDG_CONFIG_HOME
- Loading branch information
Showing
3 changed files
with
106 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters