Skip to content

Commit

Permalink
Add list conversation option
Browse files Browse the repository at this point in the history
  • Loading branch information
f-ewald committed May 22, 2022
1 parent fa888fe commit e1a07ea
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/conversation.go
Original file line number Diff line number Diff line change
@@ -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))
}
},
}
20 changes: 20 additions & 0 deletions pkg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e1a07ea

Please sign in to comment.