package cmd import ( "fmt" cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util" "git.mlow.ca/mlow/lmcli/pkg/lmcli" "git.mlow.ca/mlow/lmcli/pkg/lmcli/model" "github.com/spf13/cobra" ) func CloneCmd(ctx *lmcli.Context) *cobra.Command { cmd := &cobra.Command{ Use: "clone ", Short: "Clone conversations", Long: `Clones the provided conversation.`, Args: func(cmd *cobra.Command, args []string) error { argCount := 1 if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil { return err } return nil }, RunE: func(cmd *cobra.Command, args []string) error { shortName := args[0] toClone, err := cmdutil.LookupConversationE(ctx, shortName) if err != nil { return err } messagesToCopy, err := ctx.Store.Messages(toClone) if err != nil { return fmt.Errorf("Could not retrieve messages for conversation: %s", toClone.ShortName.String) } clone := &model.Conversation{ Title: toClone.Title + " - Clone", } if err := ctx.Store.SaveConversation(clone); err != nil { return fmt.Errorf("Cloud not create clone: %s", err) } var errors []error messageCnt := 0 for _, message := range messagesToCopy { newMessage := message newMessage.ConversationID = clone.ID newMessage.ID = 0 if err := ctx.Store.SaveMessage(&newMessage); err != nil { errors = append(errors, err) } else { messageCnt++ } } if len(errors) > 0 { return fmt.Errorf("Messages failed to be cloned: %v", errors) } fmt.Printf("Cloned %d messages to: %s\n", messageCnt, clone.Title) return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { compMode := cobra.ShellCompDirectiveNoFileComp if len(args) != 0 { return nil, compMode } return ctx.Store.ConversationShortNameCompletions(toComplete), compMode }, } return cmd }