summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-12-17 16:56:00 +0000
committerMatthew Lemon <y@yulqen.org>2024-12-17 16:56:00 +0000
commit33e68b6c09979cd702c0d5ce9e6e34d6ac45d6d1 (patch)
tree127d87ac3c7bdfcc1553d9310caf8e3e4ad12869
initial commit
-rw-r--r--.gitignore28
-rw-r--r--cmd/tjp.go187
-rw-r--r--go.mod5
-rw-r--r--go.sum2
4 files changed, 222 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..452970a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,28 @@
+### Go template
+# If you prefer the allow list template instead of the deny list, see community template:
+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
+#
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+go.work.sum
+
+# env file
+.env
+
+/tjp
diff --git a/cmd/tjp.go b/cmd/tjp.go
new file mode 100644
index 0000000..1efd5f3
--- /dev/null
+++ b/cmd/tjp.go
@@ -0,0 +1,187 @@
+package main
+
+import (
+ "database/sql"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "time"
+
+ _ "github.com/lib/pq"
+)
+
+// Database connection details
+const (
+ host = "postgres.banded-neon.ts.net"
+ user = "postgresql"
+ dbname = "journal"
+)
+
+var db *sql.DB
+
+func init() {
+ var err error
+ // Connection string
+ psqlInfo := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable", host, user, dbname)
+ db, err = sql.Open("postgres", psqlInfo)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ if err = db.Ping(); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func addMODContact(firstName, lastName string) {
+ var contactId int
+ query := `INSERT INTO contacts (first_name, last_name, contact_type) VALUES ($1, $2, 3) RETURNING id`
+ err := db.QueryRow(query, firstName, lastName).Scan(&contactId)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("MOD contact added with ID: %d\n", contactId)
+}
+
+func addSleepEntry(entry string) {
+ _, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 4)", entry)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
+
+func addPersonalEntry(entry string) {
+ _, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 2)", entry)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
+
+func addMODEntry(entry string) {
+ _, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 1)", entry)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
+
+func listPersonalEntries(orientation string) {
+ // Assume rows is a valid *sql.Rows from a database query
+ var rows *sql.Rows
+
+ // Define variables with sql.Null* types
+ var id sql.NullInt64
+ var dateAdded sql.NullTime
+ var entry sql.NullString
+ var comment sql.NullString
+
+ rows, err := db.Query("SELECT id, date_added, entry, comment FROM journal_entries WHERE type = 2 ORDER BY id ASC")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer rows.Close()
+ fmt.Println("Personal Entries:")
+
+ // Scan for each row
+ for rows.Next() {
+ if err := rows.Scan(&id, &dateAdded, &entry, &comment); err != nil {
+ log.Fatal(err)
+ }
+ if orientation == "vertical" {
+ formatVertically(id, dateAdded, entry, comment)
+ } else if orientation == "horizontal" {
+ formatHorizontally(id, dateAdded, entry, comment)
+ }
+ }
+}
+
+func formatVertically(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
+ fmt.Printf("ID: %s\n", formatInt64(id))
+ fmt.Printf("Date Added: %s\n", formatTime(dateAdded))
+ fmt.Printf("Entry: %s\n", formatString(entry))
+ fmt.Printf("Comment: %s\n", formatString(comment))
+}
+
+func formatHorizontally(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
+ fmt.Printf("%s | %s | %s\n", formatShortTime(dateAdded), formatString(entry), formatString(comment))
+}
+
+func formatShortTime(t sql.NullTime) string {
+ if t.Valid {
+ return t.Time.Format("2006-01-02T15:04")
+ }
+ return "NULL"
+}
+
+// Helper functions to handle sql.Null* types
+func formatInt64(v sql.NullInt64) string {
+ if v.Valid {
+ return fmt.Sprintf("%d", v.Int64)
+ }
+ return "NULL"
+}
+
+func formatTime(v sql.NullTime) string {
+ if v.Valid {
+ return v.Time.Format(time.RFC3339)
+ }
+ return "NULL"
+}
+
+func formatString(v sql.NullString) string {
+ if v.Valid {
+ return v.String
+ }
+ return "NULL"
+}
+
+func main() {
+
+ // Define flags
+ addMODFlag := flag.Bool("addMOD", false, "Add a MOD contact with first name and last name")
+ firstName := flag.String("firstName", "", "First name of the contact")
+ lastName := flag.String("lastName", "", "Last name of the contact")
+
+ orientationFlag := flag.String("orientation", "horizontal", "Orientation of the journal entries (vertical or horizontal)")
+
+ addSleepEntryFlag := flag.Bool("addSleepEntry", false, "Add a sleep entry")
+ sleepEntry := flag.String("sleepEntry", "", "Content of the sleep entry")
+
+ addPersonalEntryFlag := flag.Bool("p", false, "Add a personal entry")
+ entry := flag.String("entry", "", "Content of the journal entry")
+
+ addMODEntryFlag := flag.Bool("addMODEntry", false, "Add a MOD entry")
+
+ listPersonalEntriesFlag := flag.Bool("l", false, "List all personal entries")
+
+ // Parse flags
+ flag.Parse()
+
+ // Call functions based on flags
+ if *addMODFlag {
+ if *firstName == "" || *lastName == "" {
+ log.Fatal("Both firstName and lastName must be provided for adding a MOD contact")
+ }
+ addMODContact(*firstName, *lastName)
+ } else if *addSleepEntryFlag {
+ if *sleepEntry == "" {
+ log.Fatal("Sleep entry content must be provided")
+ }
+ addSleepEntry(*sleepEntry)
+ } else if *addPersonalEntryFlag {
+ if *entry == "" {
+ log.Fatal("Personal entry content must be provided - use the -entry flag")
+ }
+ addPersonalEntry(*entry)
+ } else if *addMODEntryFlag {
+ if *entry == "" {
+ log.Fatal("MOD entry content must be provided - use the -entry flag")
+ }
+ addMODEntry(*entry)
+ } else if *listPersonalEntriesFlag {
+ listPersonalEntries(*orientationFlag)
+ } else {
+ flag.Usage() // Print usage if no valid flags are provided
+ os.Exit(1)
+ }
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..fefe7c0
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module tjp
+
+go 1.23.4
+
+require github.com/lib/pq v1.10.9 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..aeddeae
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
+github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=