Add --interactive mode for remove
This commit is contained in:
parent
dd4b3e4a72
commit
b11c126afb
@ -1,27 +0,0 @@
|
||||
#!/usr/bin/env fish
|
||||
|
||||
function serra_add ()
|
||||
|
||||
# split input args
|
||||
set -l args (string split " " $argv)
|
||||
|
||||
# check if user wants to exit
|
||||
if [ "$args[2]" = "exit" ]
|
||||
exit 0
|
||||
end
|
||||
|
||||
# if usage contains a number at the end, add multiple ones
|
||||
# if not, add a single one
|
||||
if test -n "$args[3]"
|
||||
./serra add $args[1]/$args[2] --count=$args[3] -u
|
||||
echo ./serra add $args[1]/$args[2] --count=$args[3] >> add.log
|
||||
else
|
||||
./serra add $args[1]/$args[2] -u
|
||||
echo ./serra add $args[1]/$args[2] >> add.log
|
||||
end
|
||||
end
|
||||
|
||||
while true;
|
||||
read -l c
|
||||
serra_add $argv[1] $c
|
||||
end
|
||||
@ -2,14 +2,18 @@ package serra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
"github.com/spf13/cobra"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
removeCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to remove")
|
||||
removeCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Spin up interactive terminal")
|
||||
removeCmd.Flags().StringVarP(&set, "set", "s", "", "Filter by set code (usg/mmq/vow)")
|
||||
rootCmd.AddCommand(removeCmd)
|
||||
}
|
||||
|
||||
@ -21,27 +25,65 @@ var removeCmd = &cobra.Command{
|
||||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, cards []string) error {
|
||||
|
||||
client := storage_connect()
|
||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||
defer storage_disconnect(client)
|
||||
|
||||
// Loop over different cards
|
||||
for _, card := range cards {
|
||||
// Fetch card from scryfall
|
||||
c, err := find_card_by_setcollectornumber(coll, strings.Split(card, "/")[0], strings.Split(card, "/")[1])
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||
continue
|
||||
}
|
||||
|
||||
if c.SerraCount > 1 {
|
||||
modify_count_of_card(coll, c, -1)
|
||||
} else {
|
||||
coll.storage_remove(bson.M{"_id": c.ID})
|
||||
LogMessage(fmt.Sprintf("\"%s\" (%.2f %s) removed from the Collection.", c.Name, c.getValue(), getCurrency()), "green")
|
||||
}
|
||||
|
||||
if interactive {
|
||||
removeCardsInteractive(unique, set)
|
||||
} else {
|
||||
removeCards(cards, count)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func removeCardsInteractive(unique bool, set string) {
|
||||
|
||||
if len(set) == 0 {
|
||||
LogMessage("Error: --set must be given in interactive mode", "red")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rl, err := readline.New(fmt.Sprintf("%s> ", set))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rl.Close()
|
||||
|
||||
for {
|
||||
line, err := rl.Readline()
|
||||
if err != nil { // io.EOF
|
||||
break
|
||||
}
|
||||
|
||||
// construct card input for addCards
|
||||
card := []string{}
|
||||
card = append(card, fmt.Sprintf("%s/%s", set, strings.TrimSpace(line)))
|
||||
|
||||
removeCards(card, count)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func removeCards(cards []string, count int64) error {
|
||||
|
||||
client := storage_connect()
|
||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||
defer storage_disconnect(client)
|
||||
|
||||
// Loop over different cards
|
||||
for _, card := range cards {
|
||||
// Fetch card from scryfall
|
||||
c, err := find_card_by_setcollectornumber(coll, strings.Split(card, "/")[0], strings.Split(card, "/")[1])
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||
continue
|
||||
}
|
||||
|
||||
if c.SerraCount > 1 {
|
||||
modify_count_of_card(coll, c, -1)
|
||||
} else {
|
||||
coll.storage_remove(bson.M{"_id": c.ID})
|
||||
LogMessage(fmt.Sprintf("\"%s\" (%.2f %s) removed from the Collection.", c.Name, c.getValue(), getCurrency()), "green")
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user