Make lmcli rm allow removing multiple conversations

This commit is contained in:
Matt Low 2023-12-06 05:47:35 +00:00
parent f6ded3e20e
commit 1966ec881b

View File

@ -2,6 +2,7 @@ package cli
import (
"fmt"
"os"
"slices"
"strings"
"time"
@ -228,9 +229,9 @@ var lsCmd = &cobra.Command{
}
var rmCmd = &cobra.Command{
Use: "rm <conversation>",
Short: "Remove a conversation",
Long: `Removes a conversation by its short name.`,
Use: "rm <conversation>...",
Short: "Remove conversations",
Long: `Remove conversations by their short names.`,
Args: func(cmd *cobra.Command, args []string) error {
argCount := 1
if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil {
@ -239,25 +240,44 @@ var rmCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
shortName := args[0]
conversation, err := store.ConversationByShortName(shortName)
if err != nil {
Fatal("Could not search for conversation: %v\n", err)
var toRemove []*Conversation
for _, shortName := range args {
conversation, err := store.ConversationByShortName(shortName)
if err != nil {
Fatal("Could not search for conversation: %v\n", err)
}
if conversation.ID == 0 {
Fatal("Conversation not found with short name: %s\n", shortName)
}
toRemove = append(toRemove, conversation)
}
if conversation.ID == 0 {
Fatal("Conversation not found with short name: %s\n", shortName)
var errors []error
for _, c := range toRemove {
err := store.DeleteConversation(c)
if err != nil {
errors = append(errors, fmt.Errorf("Could not remove conversation %s: %v", c.ShortName.String, err))
}
}
err = store.DeleteConversation(conversation)
if err != nil {
Fatal("Could not delete conversation: %v\n", err)
for _, err := range errors {
fmt.Fprintln(os.Stderr, err.Error())
}
if len(errors) > 0 {
os.Exit(1)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp
if len(args) != 0 {
return nil, compMode
var completions []string
outer: for _, completion := range store.ConversationShortNameCompletions(toComplete) {
parts := strings.Split(completion, "\t")
for _, arg := range args {
if parts[0] == arg {
continue outer
}
}
completions = append(completions, completion)
}
return store.ConversationShortNameCompletions(toComplete), compMode
return completions, compMode
},
}