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
1 changed files with 35 additions and 15 deletions

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,7 +240,8 @@ 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
for _, shortName := range args {
conversation, err := store.ConversationByShortName(shortName) conversation, err := store.ConversationByShortName(shortName)
if err != nil { if err != nil {
Fatal("Could not search for conversation: %v\n", err) Fatal("Could not search for conversation: %v\n", err)
@ -247,17 +249,35 @@ var rmCmd = &cobra.Command{
if conversation.ID == 0 { if conversation.ID == 0 {
Fatal("Conversation not found with short name: %s\n", shortName) Fatal("Conversation not found with short name: %s\n", shortName)
} }
err = store.DeleteConversation(conversation) toRemove = append(toRemove, conversation)
}
var errors []error
for _, c := range toRemove {
err := store.DeleteConversation(c)
if err != nil { if err != nil {
Fatal("Could not delete conversation: %v\n", err) errors = append(errors, fmt.Errorf("Could not remove conversation %s: %v", c.ShortName.String, 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) { 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
} }
return store.ConversationShortNameCompletions(toComplete), compMode }
completions = append(completions, completion)
}
return completions, compMode
}, },
} }