cal config

This commit is contained in:
Erik Winter 2024-09-25 07:27:39 +02:00
parent 49d45970a4
commit 7a89a9f6f6
3 changed files with 38 additions and 4 deletions

View File

@ -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
}

1
go.mod
View File

@ -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

3
go.sum
View File

@ -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=