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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/chzyer/readline"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
removeCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to remove")
|
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)
|
rootCmd.AddCommand(removeCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,6 +25,45 @@ var removeCmd = &cobra.Command{
|
|||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
RunE: func(cmd *cobra.Command, cards []string) error {
|
RunE: func(cmd *cobra.Command, cards []string) error {
|
||||||
|
|
||||||
|
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()
|
client := storage_connect()
|
||||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||||
defer storage_disconnect(client)
|
defer storage_disconnect(client)
|
||||||
@ -43,5 +86,4 @@ var removeCmd = &cobra.Command{
|
|||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user