From e1a07ea74becf9a752c0ebcc5d2d91f429ff214e Mon Sep 17 00:00:00 2001 From: Freddy Ewald Date: Sat, 21 May 2022 22:18:51 -0700 Subject: [PATCH] Add list conversation option --- cmd/conversation.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ pkg/db.go | 20 ++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 cmd/conversation.go diff --git a/cmd/conversation.go b/cmd/conversation.go new file mode 100644 index 0000000..f0c3759 --- /dev/null +++ b/cmd/conversation.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "context" + hermes "github.com/f-ewald/hermes/pkg" + "github.com/spf13/cobra" + "log" +) + +func init() { + rootCmd.AddCommand(conversationCmd) + conversationCmd.AddCommand(conversationListCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // conversationCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // conversationCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + +// conversationCmd represents the conversation command. +var conversationCmd = &cobra.Command{ + Use: "conversation", + Short: "Show conversations, find participants", + //Long: `TODO`, + //Run: func(cmd *cobra.Command, args []string) { + // fmt.Println("conversation called") + //}, +} + +var conversationListCmd = &cobra.Command{ + Use: "list", + Short: "List all conversations", + Long: "List all conversations with ", + Run: func(cmd *cobra.Command, args []string) { + messageDB, err := hermes.MessageDBFilename() + if err != nil { + log.Fatal(err) + } + db := hermes.NewDatabase(messageDB) + err = db.Connect() + if err != nil { + log.Fatal(err) + } + defer func() { + err = db.Close() + if err != nil { + log.Fatal(err) + } + }() + + conversations, err := db.ListConversations(context.Background()) + if err != nil { + log.Fatal(err) + } + var printer hermes.Printer + printer = &hermes.StdoutPrinter{} + for _, conversation := range conversations { + printer.Print([]byte(conversation)) + } + }, +} diff --git a/pkg/db.go b/pkg/db.go index 523d0a6..fe0ba6a 100644 --- a/pkg/db.go +++ b/pkg/db.go @@ -91,3 +91,23 @@ func (db *Database) Conversation(ctx context.Context) (conversation *Conversatio return nil, nil } + +func (db *Database) ListConversations(ctx context.Context) (conversations []string, err error) { + rows, err := db.db.QueryContext(ctx, "SELECT guid FROM chat") + if err != nil { + return nil, err + } + defer func() { + _ = rows.Close() + }() + conversations = make([]string, 0) + for rows.Next() { + var guid string + err = rows.Scan(&guid) + if err != nil { + return nil, err + } + conversations = append(conversations, guid) + } + return conversations, nil +}