Implement `lmcli rm`

This commit is contained in:
Matt Low 2023-11-13 06:56:05 +00:00
parent cf0e98f656
commit 6af9377cf5
2 changed files with 40 additions and 0 deletions

View File

@ -122,6 +122,40 @@ var lsCmd = &cobra.Command{
}, },
} }
var rmCmd = &cobra.Command{
Use: "rm [conversation]",
Short: "Remove a conversation",
Long: `Removes a conversation by its short name.`,
Args: func(cmd *cobra.Command, args []string) error {
argCount := 1
if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil {
return err
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
shortName := args[0]
conversation, err := store.ConversationByShortName(shortName)
if err != nil {
Fatal("Cloud not search for conversation: %v\n", err)
}
if conversation.ID == 0 || err != nil {
Fatal("Conversation not found with short name: %s\n", shortName)
}
err = store.DeleteConversation(conversation)
if err != nil {
Fatal("Could not delete conversation: %v\n", err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp
if len(args) != 0 {
return nil, compMode
}
return store.ConversationShortNameCompletions(toComplete), compMode
},
}
var viewCmd = &cobra.Command{ var viewCmd = &cobra.Command{
Use: "view [conversation]", Use: "view [conversation]",
Short: "View messages in a conversation", Short: "View messages in a conversation",
@ -289,6 +323,7 @@ func NewRootCmd() *cobra.Command {
lsCmd, lsCmd,
newCmd, newCmd,
promptCmd, promptCmd,
rmCmd,
viewCmd, viewCmd,
) )
return rootCmd return rootCmd

View File

@ -86,6 +86,11 @@ func (s *Store) SaveConversation(conversation *Conversation) error {
return err return err
} }
func (s *Store) DeleteConversation(conversation *Conversation) error {
s.db.Where("conversation_id = ?", conversation.ID).Delete(&Message{})
return s.db.Delete(&conversation).Error
}
func (s *Store) SaveMessage(message *Message) error { func (s *Store) SaveMessage(message *Message) error {
return s.db.Create(message).Error return s.db.Create(message).Error
} }