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