Add "add" and "product" subcommands

This commit is contained in:
The Magician 2024-11-24 17:24:42 +00:00
parent 93184d4c52
commit 7fd6ed07f3
1 changed files with 68 additions and 0 deletions

View File

@ -2,9 +2,11 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"sevenkeys/database" "sevenkeys/database"
@ -12,6 +14,8 @@ import (
"sevenkeys/logic" "sevenkeys/logic"
"sevenkeys/logic/scryfall" "sevenkeys/logic/scryfall"
"strings" "strings"
"github.com/mtgban/go-mtgban/cardtrader"
) )
func GetStringResponse(prompt string) string { func GetStringResponse(prompt string) string {
@ -37,9 +41,12 @@ const (
ImportSubcommand string = "import" ImportSubcommand string = "import"
SearchPrintingsSubcommand string = "search-printings" SearchPrintingsSubcommand string = "search-printings"
SearchStorageSubcommand string = "search-storage" SearchStorageSubcommand string = "search-storage"
AddSubcommand string = "add"
RemoveSubcommand string = "remove" RemoveSubcommand string = "remove"
ReplaceSubcommand string = "replace" ReplaceSubcommand string = "replace"
DeckSubcommand string = "deck" DeckSubcommand string = "deck"
GetProductIdSubcommand string = "products"
) )
func main() { func main() {
@ -189,6 +196,28 @@ func main() {
logic.Check(err) logic.Check(err)
fmt.Println(id) fmt.Println(id)
break break
case AddSubcommand:
addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError)
cardPrintingId := addCmd.String("card-printing-id", "", "The ID of the card printing to add to storage.")
storageArea := addCmd.String("storagearea", "",
"The name of the StorageArea where cards should be imported.")
addCmd.Parse(flag.Args()[1:])
storageAreaId, err := logic.GetStorageAreaId(db, *storageArea)
if err == logic.ErrCouldNotGetStorageAreaId {
fmt.Fprintf(os.Stderr, "[sevenkeys] No storage area was selected, exiting.\n")
os.Exit(1)
}
logic.Check(err)
cardLocation := database.CardLocation{
CardPrintingId: *cardPrintingId,
StorageAreaId: storageAreaId,
}
logic.StoreCard(db, cardLocation)
break
case RemoveSubcommand: case RemoveSubcommand:
removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError) removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError)
@ -231,6 +260,45 @@ func main() {
//filename := deckCmd.Args()[0] //filename := deckCmd.Args()[0]
break break
case GetProductIdSubcommand:
blbBlueprintsBytes, err := ioutil.ReadFile("blb_blueprints.json")
logic.Check(err)
var blbBlueprints []cardtrader.Blueprint
err = json.Unmarshal(blbBlueprintsBytes, &blbBlueprints)
logic.Check(err)
productsBytes, err := ioutil.ReadFile("products.json")
logic.Check(err)
var products []cardtrader.Product
err = json.Unmarshal(productsBytes, &products)
logic.Check(err)
for _, product := range products {
var productBlueprint cardtrader.Blueprint
for _, blueprint := range blbBlueprints {
if blueprint.Id == product.BlueprintId {
productBlueprint = blueprint
break
}
}
fmt.Printf("%s %s %d ",
productBlueprint.Name,
product.Properties.Number,
product.Id,
)
if product.Properties.MTGFoil {
fmt.Printf("FOIL ")
} else {
fmt.Printf("NONFOIL ")
}
fmt.Printf("x%d\n", product.Quantity)
}
break
default: default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1]) fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])
break break