2024-09-27 07:38:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-09-30 07:34:40 +02:00
|
|
|
"go-mod.ewintr.nl/planner/plan/command"
|
2024-10-03 07:32:48 +02:00
|
|
|
"go-mod.ewintr.nl/planner/plan/storage/sqlite"
|
2024-10-07 11:11:18 +02:00
|
|
|
"go-mod.ewintr.nl/planner/sync/client"
|
2024-09-27 07:38:50 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
confPath, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not get config path: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
conf, err := LoadConfig(filepath.Join(confPath, "planner", "plan", "config.yaml"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not open config file: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
localIDRepo, eventRepo, syncRepo, err := sqlite.NewSqlites(conf.DBPath)
|
2024-09-27 07:38:50 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not open db file: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
syncClient := client.New(conf.SyncURL, conf.ApiKey)
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
cli := command.CLI{
|
|
|
|
Commands: []command.Command{
|
|
|
|
command.NewAdd(localIDRepo, eventRepo, syncRepo),
|
|
|
|
command.NewList(localIDRepo, eventRepo),
|
|
|
|
command.NewUpdate(localIDRepo, eventRepo, syncRepo),
|
|
|
|
command.NewDelete(localIDRepo, eventRepo, syncRepo),
|
|
|
|
command.NewSync(syncClient, syncRepo, localIDRepo, eventRepo),
|
2024-09-27 07:38:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-12-04 07:22:24 +01:00
|
|
|
if err := cli.Run(os.Args[1:]); err != nil {
|
2024-09-27 07:38:50 +02:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Configuration struct {
|
2024-12-03 07:35:27 +01:00
|
|
|
DBPath string `yaml:"db_path"`
|
2024-10-07 11:11:18 +02:00
|
|
|
SyncURL string `yaml:"sync_url"`
|
|
|
|
ApiKey string `yaml:"api_key"`
|
2024-09-27 07:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|