Add --interactive mode for add

This commit is contained in:
Florian Baumann 2023-02-20 10:41:11 +01:00
parent 096b76a350
commit dd4b3e4a72
4 changed files with 90 additions and 44 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/noqqe/serra
go 1.14 go 1.14
require ( require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/mitchellh/mapstructure v1.4.3 github.com/mitchellh/mapstructure v1.4.3
github.com/schollz/progressbar/v3 v3.8.5 github.com/schollz/progressbar/v3 v3.8.5
github.com/spf13/cobra v1.3.0 github.com/spf13/cobra v1.3.0

3
go.sum
View File

@ -69,8 +69,11 @@ github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=

View File

@ -2,8 +2,10 @@ 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"
) )
@ -11,6 +13,8 @@ import (
func init() { func init() {
addCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to add") addCmd.Flags().Int64VarP(&count, "count", "c", 1, "Amount of cards to add")
addCmd.Flags().BoolVarP(&unique, "unique", "u", false, "Only add card if not existent yet") addCmd.Flags().BoolVarP(&unique, "unique", "u", false, "Only add card if not existent yet")
addCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Spin up interactive terminal")
addCmd.Flags().StringVarP(&set, "set", "s", "", "Filter by set code (usg/mmq/vow)")
rootCmd.AddCommand(addCmd) rootCmd.AddCommand(addCmd)
} }
@ -22,6 +26,44 @@ var addCmd = &cobra.Command{
SilenceErrors: true, SilenceErrors: true,
RunE: func(cmd *cobra.Command, cards []string) error { RunE: func(cmd *cobra.Command, cards []string) error {
if interactive {
addCardsInteractive(unique, set)
} else {
addCards(cards, unique, count)
}
return nil
},
}
func addCardsInteractive(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)))
addCards(card, unique, count)
}
}
func addCards(cards []string, unique bool, 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)
@ -68,5 +110,4 @@ var addCmd = &cobra.Command{
} }
storage_disconnect(client) storage_disconnect(client)
return nil return nil
},
} }

View File

@ -11,6 +11,7 @@ var Version = "unknown"
var count int64 var count int64
var limit float64 var limit float64
var interactive bool
var name string var name string
var oracle string var oracle string
var rarity string var rarity string