From c31ab6a01d39784a38d1c11627dd8d82618744d9 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 27 Oct 2024 17:18:43 +0100 Subject: [PATCH] Implement note parsing --- .gitignore | 2 ++ db.go | 2 +- go.mod | 2 ++ go.sum | 2 ++ main.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++--- types.go | 30 +++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 go.sum create mode 100644 types.go diff --git a/.gitignore b/.gitignore index 1269488..52d506e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ data +main.log +input diff --git a/db.go b/db.go index 436c7d2..ea4724b 100644 --- a/db.go +++ b/db.go @@ -7,7 +7,7 @@ import ( "os" "time" - "github.com/mattn/go-sqlite3" + _ "github.com/mattn/go-sqlite3" ) type DB struct { diff --git a/go.mod b/go.mod index 982346c..4a08505 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module stinkinator go 1.23.2 + +require github.com/mattn/go-sqlite3 v1.14.24 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9dcdc9b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go index 47251bb..7f17405 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,13 @@ package main import ( + "flag" "fmt" "io" "log" "os" + "strings" + "time" ) var Error *log.Logger @@ -28,7 +31,59 @@ func init() { } func main() { - log.Println("Hello, World!") - Warning.Println("Hello, World!") - Error.Println("Hello, World!") + inputFile := flag.String("if", "input", "Input file") + flag.Parse() + log.Printf("Input file: %s", *inputFile) + + db := DB{ + path: "data/db.db", + } + err := db.Open() + if err != nil { + Error.Printf("Failed opening database: %v", err) + return + } + defer db.Close() + + inputData, err := os.ReadFile(*inputFile) + if err != nil { + Error.Printf("Failed reading input file: %v", err) + return + } + note, err := ParseNote(string(inputData)) + if err != nil { + Error.Printf("Failed parsing note: %v", err) + return + } + log.Printf("%#v", note) +} + +func ParseNote(data string) (NoteData, error) { + res := NoteData{} + lines := strings.Split(data, "\n") + note := []string{} + for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue + } + parts := strings.Split(line, ":") + log.Printf("%#v", parts) + switch parts[0] { + case "p": + res.Player = parts[1] + case "d": + var err error + res.Date, err = time.Parse(time.DateOnly, parts[1]) + if err != nil { + return res, fmt.Errorf("failed to parse date: %v", err) + } + case "g": + res.Guild = parts[1] + default: + note = append(note, line) + } + } + res.Note = strings.Join(note, "\n") + return res, nil } \ No newline at end of file diff --git a/types.go b/types.go new file mode 100644 index 0000000..ed7b4b9 --- /dev/null +++ b/types.go @@ -0,0 +1,30 @@ +package main + +import "time" + +type ( + Guild struct { + ID int + Name string + } + + Stinky struct { + ID int + Name string + Guild Guild + } + + Note struct { + ID int + Content string + Timestamp time.Time + Stinky Stinky + } +) + +type NoteData struct { + Player string + Date time.Time + Guild string + Note string +}