From 7a89a9f6f6c5eaa54e2586ccbb45fd454d6ddfe2 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Wed, 25 Sep 2024 07:27:39 +0200 Subject: [PATCH] cal config --- cal/main.go | 38 ++++++++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 3 +++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cal/main.go b/cal/main.go index 54faf9b..4dd4b37 100644 --- a/cal/main.go +++ b/cal/main.go @@ -3,17 +3,30 @@ package main import ( "fmt" "os" + "path/filepath" "time" "go-mod.ewintr.nl/planner/item" + "gopkg.in/yaml.v3" ) func main() { fmt.Println("cal") - - repo, err := NewSqlite("test.db") + confPath, err := os.UserConfigDir() if err != nil { - fmt.Println(err) + fmt.Printf("could not get config path: %s\n", err) + os.Exit(1) + } + conf, err := LoadConfig(filepath.Join(confPath, "planner", "cal", "config.yaml")) + if err != nil { + fmt.Printf("could not open config file: %s\n", err) + os.Exit(1) + } + fmt.Printf("conf: %+v\n", conf) + + repo, err := NewSqlite(conf.DBPath) + if err != nil { + fmt.Printf("could not open db file: %s\n", err) os.Exit(1) } @@ -25,7 +38,7 @@ func main() { End: time.Now().Add(-5 * time.Second), }, } - if err := repo.Delete(one.ID); err != nil { + if err := repo.Store(one); err != nil { fmt.Println(err) os.Exit(1) } @@ -66,3 +79,20 @@ func main() { // fmt.Printf("%+v\n", items) } + +type Configuration struct { + DBPath string `yaml:"dbpath"` +} + +func LoadConfig(path string) (Configuration, error) { + confFile, err := os.ReadFile(path) + if err != nil { + return Configuration{}, fmt.Errorf("could not open file: %s", err) + } + var conf Configuration + if err := yaml.Unmarshal(confFile, &conf); err != nil { + return Configuration{}, fmt.Errorf("could not unmarshal config: %s", err) + } + + return conf, nil +} diff --git a/go.mod b/go.mod index 1f60790..b90d726 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/ncruces/go-strftime v0.1.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect golang.org/x/sys v0.22.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect modernc.org/libc v1.55.3 // indirect modernc.org/mathutil v1.6.0 // indirect diff --git a/go.sum b/go.sum index dd81e53..5eeabd8 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,9 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= modernc.org/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U=