planner/plan/main.go

71 lines
1.6 KiB
Go
Raw Normal View History

2024-09-27 07:38:50 +02:00
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
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:32:53 +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-03 07:32:48 +02:00
localIDRepo, eventRepo, 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:32:53 +02:00
syncClient := client.New(conf.SyncURL, conf.ApiKey)
2024-09-27 07:38:50 +02:00
app := &cli.App{
Name: "plan",
Usage: "Plan your day with events",
Commands: []*cli.Command{
2024-10-03 07:32:48 +02:00
command.NewAddCmd(localIDRepo, eventRepo),
command.NewListCmd(localIDRepo, eventRepo),
2024-10-06 11:28:05 +02:00
command.NewUpdateCmd(localIDRepo, eventRepo),
2024-10-07 09:34:17 +02:00
command.NewDeleteCmd(localIDRepo, eventRepo),
2024-10-07 11:32:53 +02:00
command.NewSyncCmd(syncClient, localIDRepo, eventRepo),
2024-09-27 07:38:50 +02:00
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Configuration struct {
2024-10-07 11:32:53 +02:00
DBPath string `yaml:"dbpath"`
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
}