commit 9e4277e3bb45e6c9541823b6887da8b5869309a9
parent 8f8624b0c23bd0c1905cb58c9079482fccab7104
Author: Andrew Laack <andrew@laack.co>
Date: Sat, 16 May 2026 20:39:10 -0500
If not exist, default + write file. If exists, read data + load configuration
Diffstat:
| M | main.go | | | 40 | +++++++++++++++++++++++++++++----------- |
1 file changed, 29 insertions(+), 11 deletions(-)
diff --git a/main.go b/main.go
@@ -2,16 +2,19 @@ package main
import (
"encoding/json"
+ "errors"
"fmt"
"log"
"os"
)
type Config struct {
- Dirs []string `json:"dirs"`
+ NoteDirs []string `json:"noteDirs"`
+ TodoDir string `json:"todoDir"`
}
func readConfig() Config {
+
configBase, err := os.UserConfigDir()
if err != nil {
@@ -30,27 +33,42 @@ func readConfig() Config {
}
configFilePath := appConfigDir + "config.json"
- configFileData, err := os.ReadFile(configFilePath)
-
- if err != nil {
- log.Fatal(err)
- }
configuration := &Config{
- Dirs: []string{"$HOME/.notes"},
+ NoteDirs: []string{"$HOME/.notes"},
+ TodoDir: "$HOME/.todo",
}
- err = json.Unmarshal(configFileData, configuration)
+ if _, err := os.Stat(configFilePath); !errors.Is(err, os.ErrNotExist) {
+ configFileData, err := os.ReadFile(configFilePath)
+
+ if err != nil {
+ log.Fatal(err)
+ }
+ err = json.Unmarshal(configFileData, configuration)
+ if err != nil {
+ log.Fatal(err)
+ }
+ } else {
+ configBytes , err := json.MarshalIndent(configuration, "", " ")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ os.WriteFile(configFilePath, configBytes, 0644)
- if err != nil {
- log.Fatal(err)
}
+
+
+
return *configuration
}
func main() {
// TODO: Ensure all configuration fields are filled in, always in the config
+
conf := readConfig()
- fmt.Println(conf.Dirs)
+ fmt.Println(conf)
+
}