108 lines
2.5 KiB
Go
108 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.hrafn.xyz/aether/notes/client"
|
|
"git.hrafn.xyz/aether/notes/internal/repository"
|
|
"git.hrafn.xyz/aether/notes/internal/store/sqlite"
|
|
"git.hrafn.xyz/aether/notes/server"
|
|
)
|
|
|
|
func main() {
|
|
serverMode := flag.Bool("server", false, "Start the application in server mode")
|
|
list := flag.Bool("list", false, "List all notes in the database")
|
|
create := flag.Bool("create", false, "Create a new note")
|
|
update := flag.Bool("update", false, "Update an existing note")
|
|
delete := flag.Bool("delete", false, "Delete a note from the database")
|
|
get := flag.Bool("get", false, "Get a note by ID")
|
|
|
|
databasefile := flag.String("db", filepath.Join(os.Getenv("HOME"), "notes.db"), "Path to the SQLite database file (only used in server mode)")
|
|
id := flag.Int("id", 0, "ID of the note to get, update, or delete (only used with -get, -update, or -delete)")
|
|
content := flag.String("content", "", "Content of the note to create or update (only used with -create or -update)")
|
|
|
|
flag.Parse()
|
|
modes := map[string]bool{
|
|
"server": *serverMode,
|
|
"list": *list,
|
|
"create": *create,
|
|
"update": *update,
|
|
"delete": *delete,
|
|
}
|
|
|
|
enabledCount := 0
|
|
for _, enabled := range modes {
|
|
if enabled {
|
|
enabledCount++
|
|
}
|
|
}
|
|
if enabledCount == 0 {
|
|
log.Fatal("No mode specified. Use -server, -list, -create, -update, -delete, or -get.")
|
|
os.Exit(1)
|
|
}
|
|
if enabledCount > 1 {
|
|
log.Fatal("Multiple modes specified. Please specify only one mode at a time.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *serverMode {
|
|
dbPath := *databasefile
|
|
sqliteStore, err := sqlite.NewSQLiteStore(context.Background(), dbPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer sqliteStore.Close()
|
|
repo := repository.NewNoteRepository(sqliteStore)
|
|
s := server.GetServer(repo, log.Default())
|
|
err = s.Start(":8080")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
if *list {
|
|
client.ListNotes()
|
|
}
|
|
|
|
if *create {
|
|
if *content == "" {
|
|
log.Fatal("Content must be provided with -create")
|
|
os.Exit(1)
|
|
}
|
|
client.CreateNote(*content)
|
|
}
|
|
|
|
if *get {
|
|
if *id == 0 {
|
|
log.Fatal("ID must be provided with -get")
|
|
os.Exit(1)
|
|
}
|
|
client.GetNoteByID(*id)
|
|
}
|
|
|
|
if *update {
|
|
if *id == 0 {
|
|
log.Fatal("ID must be provided with -update")
|
|
os.Exit(1)
|
|
}
|
|
if *content == "" {
|
|
log.Fatal("Content must be provided with -update")
|
|
os.Exit(1)
|
|
}
|
|
client.UpdateNoteByID(*id, *content)
|
|
}
|
|
|
|
if *delete {
|
|
if *id == 0 {
|
|
log.Fatal("ID must be provided with -delete")
|
|
os.Exit(1)
|
|
}
|
|
client.DeleteNoteByID(*id)
|
|
os.Exit(1)
|
|
}
|
|
}
|