diff options
author | Matthew Lemon <y@yulqen.org> | 2024-12-20 05:22:37 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-12-20 05:22:37 +0000 |
commit | b4fb83a947cf0ade5b01a6e82cc78c82f07ec2ce (patch) | |
tree | 3065a4b46a64f87632dee7958b559aa25594919f | |
parent | ad96831a072d192b34aa897473b5df3fb673cf07 (diff) |
Breaks into add and list subcommands
-rw-r--r-- | tjp.go | 138 |
1 files changed, 65 insertions, 73 deletions
@@ -79,6 +79,25 @@ func addJournalEntry(entry string, journalType int) { } } +// List meetings +func listMeetings() { + rows, err := db.Query("SELECT id, name, date, subject FROM meetings ORDER BY date ASC") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + fmt.Println("Meetings:") + for rows.Next() { + var id int + var date time.Time + var name, subject string + if err := rows.Scan(&id, &name, &date, &subject); err != nil { + log.Fatal(err) + } + fmt.Printf("ID: %d\t %s\t %s (%s)\n", id, date.Format("2006-01-02"), name, subject) + } +} + // List journal entries, dependent on journalType func listjournalEntries(orientation string, journalType int) { var rows *sql.Rows @@ -184,27 +203,6 @@ func formatString(v sql.NullString) string { 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") - - dateOfEntry := flag.String("date", "", "Date of entry") - - // orientationFlag is used to set the orientation of the journal entries (vertical or horizontal) - orientationFlag := flag.String("orientation", "horizontal", "Orientation of the journal entries (vertical or horizontal)") - - addMODMeetingFlag := flag.Bool("addmeeting", false, "Add a MOD meeting") - - addMODEntryFlag := flag.Bool("addmod", false, "Add a MOD entry") - addJobbylogEntryFlag := flag.Bool("addjobby", false, "Add a Jobbylog entry") - addSleepEntryFlag := flag.Bool("addsleep", false, "Add a sleep entry") - - listPersonalEntriesFlag := flag.Bool("listpersonal", false, "List all personal entries") - listMODEntriesFlag := flag.Bool("listmod", false, "List all MOD entries, not including those associated with a meeting") - listJobbylogEntriesFlag := flag.Bool("listjobby", false, "List all Jobblylog entries") - listSleepEntriesFlag := flag.Bool("listsleep", false, "List all sleep entries") - // Parse flags flag.Parse() @@ -215,15 +213,26 @@ func main() { // - delete // ref: https://www.golinuxcloud.com/golang-flags-examples/ - // add commands + // add command addCmd := flag.NewFlagSet("add", flag.ExitOnError) // add flags addPersonalEntryFlag := addCmd.Bool("personal", false, "Add a Personal entry") + addMODEntryFlag := addCmd.Bool("mod", false, "Add a MOD entry") + addMODMeetingFlag := addCmd.Bool("meeting", false, "Add a MOD meeting") + addJobbylogEntryFlag := addCmd.Bool("jobby", false, "Add a Jobbylog entry") + addSleepEntryFlag := addCmd.Bool("sleep", false, "Add a sleep entry") entry := addCmd.String("entry", "", "Content of the journal entry") + dateOfEntry := addCmd.String("date", "", "Date of entry") - // list commands + // list command listCmd := flag.NewFlagSet("list", flag.ExitOnError) + listPersonalEntriesFlag := listCmd.Bool("personal", false, "List all personal entries") + listMODEntriesFlag := listCmd.Bool("mod", false, "List all MOD entries, not including those associated with a meeting") + listMODMeetingsFlag := listCmd.Bool("meetings", false, "List all MOD meetings") + listJobbylogEntriesFlag := listCmd.Bool("jobby", false, "List all Jobblylog entries") + listSleepEntriesFlag := listCmd.Bool("sleep", false, "List all sleep entries") + orientationFlag := listCmd.String("orientation", "horizontal", "Orientation of the journal entries (vertical or horizontal)") if len(os.Args) < 2 { fmt.Println("expected 'add' or 'list' subcommands") @@ -233,70 +242,53 @@ func main() { switch os.Args[1] { case "add": addCmd.Parse(os.Args[2:]) - // create personal entry if *addPersonalEntryFlag { if *entry == "" { log.Fatal("Personal entry content must be provided - use the -entry flag") } addJournalEntry(*entry, JOURNAL_TYPE_PERSONAL) - } - // create MOD entry - if *addMODEntryFlag { + } else if *addSleepEntryFlag { + if *entry == "" { + log.Fatal("Sleep entry content must be provided - use the -entry flag") + } + addJournalEntry(*entry, JOURNAL_TYPE_SLEEP) + } else if *addJobbylogEntryFlag { + if *entry == "" { + log.Fatal("Jobbylog entry content must be provided - use the -entry flag") + } + addJournalEntry(*entry, JOURNAL_TYPE_JOBBY) + } else if *addMODEntryFlag { if *entry == "" { log.Fatal("MOD entry content must be provided - use the -entry flag") } addJournalEntry(*entry, JOURNAL_TYPE_MOD) + } else if *addMODMeetingFlag { + if *entry == "" { + log.Fatal("Meeting name must be provided with -entry flag") + } + addMODMeeting(*entry, *dateOfEntry, "Default meeting") + } else { + addCmd.PrintDefaults() } + os.Exit(1) case "list": listCmd.Parse(os.Args[2:]) + if *listPersonalEntriesFlag { + listjournalEntries(*orientationFlag, JOURNAL_TYPE_PERSONAL) + } else if *listMODEntriesFlag { + listjournalEntries(*orientationFlag, JOURNAL_TYPE_MOD) + } else if *listJobbylogEntriesFlag { + listjournalEntries(*orientationFlag, JOURNAL_TYPE_JOBBY) + } else if *listSleepEntriesFlag { + listjournalEntries(*orientationFlag, JOURNAL_TYPE_SLEEP) + } else if *listMODMeetingsFlag { + listMeetings() + } else { + listCmd.PrintDefaults() + } + os.Exit(1) default: flag.PrintDefaults() os.Exit(1) } - - // 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 *addMODMeetingFlag { - if *entry == "" { - log.Fatal("Meeting name must be provided with -entry flag") - } - if *dateOfEntry == "" { - log.Fatal("Meeting date must be provided with -date flag") - } - addMODMeeting(*entry, *dateOfEntry, "Meeting") - } else if *addSleepEntryFlag { - if *entry == "" { - log.Fatal("Sleep entry content must be provided") - } - addSleepEntry(*entry, JOURNAL_TYPE_SLEEP) - } else if *addPersonalEntryFlag { - if *entry == "" { - log.Fatal("Personal entry content must be provided - use the -entry flag") - } - addJournalEntry(*entry, JOURNAL_TYPE_PERSONAL) - } else if *addMODEntryFlag { - if *entry == "" { - log.Fatal("MOD entry content must be provided - use the -entry flag") - } - } else if *addJobbylogEntryFlag { - if *entry == "" { - log.Fatal("Jobblylog entry content must be provided - use the -entry flag") - } - addJournalEntry(*entry, JOURNAL_TYPE_JOBBY) - } else if *listPersonalEntriesFlag { - listjournalEntries(*orientationFlag, JOURNAL_TYPE_PERSONAL) - } else if *listMODEntriesFlag { - listjournalEntries(*orientationFlag, JOURNAL_TYPE_MOD) - } else if *listJobbylogEntriesFlag { - listjournalEntries(*orientationFlag, JOURNAL_TYPE_JOBBY) - } else if *listSleepEntriesFlag { - listjournalEntries(*orientationFlag, JOURNAL_TYPE_SLEEP) - } else { - flag.Usage() // Print usage if no valid flags are provided - os.Exit(1) - } } |