2024-05-28 14:29:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-28 19:47:23 +00:00
|
|
|
"bufio"
|
2024-10-29 22:07:06 +00:00
|
|
|
"errors"
|
2024-09-25 10:48:01 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-10-29 22:07:06 +00:00
|
|
|
"log"
|
2024-09-12 12:29:43 +00:00
|
|
|
"os"
|
2024-06-10 12:32:45 +00:00
|
|
|
"sevenkeys/database"
|
2024-09-26 12:17:55 +00:00
|
|
|
"sevenkeys/delverlens"
|
|
|
|
"sevenkeys/logic"
|
2024-10-28 19:47:23 +00:00
|
|
|
"sevenkeys/logic/scryfall"
|
|
|
|
"strings"
|
2024-05-28 14:29:13 +00:00
|
|
|
)
|
|
|
|
|
2024-10-28 19:47:23 +00:00
|
|
|
func GetStringResponse(prompt string) string {
|
|
|
|
fmt.Print(prompt + " ")
|
|
|
|
|
|
|
|
var response string
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
scanner.Scan()
|
|
|
|
response = scanner.Text()
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetYesNoResponse(prompt string) bool {
|
|
|
|
response := GetStringResponse(prompt)
|
|
|
|
return strings.ToUpper(response) == "Y"
|
|
|
|
}
|
|
|
|
|
2024-09-25 10:48:01 +00:00
|
|
|
const (
|
2024-10-04 20:51:05 +00:00
|
|
|
UpdateSubcommand string = "update"
|
|
|
|
CreateStorageAreaSubcommand string = "createstorage"
|
2024-10-10 14:43:56 +00:00
|
|
|
StoreSubcommand string = "store"
|
2024-10-04 20:51:05 +00:00
|
|
|
ImportSubcommand string = "import"
|
2024-10-28 19:47:23 +00:00
|
|
|
SearchPrintingsSubcommand string = "search-printings"
|
|
|
|
SearchStorageSubcommand string = "search-storage"
|
2024-10-29 22:07:06 +00:00
|
|
|
RemoveSubcommand string = "remove"
|
|
|
|
ReplaceSubcommand string = "replace"
|
2024-10-09 19:33:47 +00:00
|
|
|
DeckSubcommand string = "deck"
|
2024-09-25 10:48:01 +00:00
|
|
|
)
|
|
|
|
|
2024-05-28 14:29:13 +00:00
|
|
|
func main() {
|
2024-09-26 12:17:55 +00:00
|
|
|
var profile string
|
|
|
|
flag.StringVar(&profile, "profile", "development", "The database profile to use.")
|
|
|
|
|
|
|
|
flag.Parse()
|
2024-09-25 10:48:01 +00:00
|
|
|
|
|
|
|
db := database.GetDatabaseFromConfig("config." + profile + ".json")
|
2024-10-04 20:51:05 +00:00
|
|
|
/* Sad.
|
2024-09-05 11:22:02 +00:00
|
|
|
figlet.ReadFigletFonts()
|
2024-06-10 15:34:16 +00:00
|
|
|
cli.ShowSplashScreen()
|
2024-10-04 20:51:05 +00:00
|
|
|
*/
|
2024-05-28 14:29:13 +00:00
|
|
|
|
2024-09-25 16:49:01 +00:00
|
|
|
// TODO: Decide in what form we need to retain this functionality if any
|
|
|
|
//cli.MainCliLoop(db)
|
|
|
|
|
2024-10-04 12:44:22 +00:00
|
|
|
//searchCmd := flag.NewFlagSet(SearchSubcommand, flag.ExitOnError)
|
|
|
|
//name := searchCmd.String("name", "", "The card name to search for.")
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
if len(flag.Args()) == 0 {
|
2024-09-25 16:49:01 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
switch flag.Args()[0] {
|
2024-10-04 12:44:22 +00:00
|
|
|
case UpdateSubcommand:
|
2024-10-28 19:47:23 +00:00
|
|
|
fmt.Println("Checking for updates...")
|
|
|
|
bulkData, err := scryfall.GetBulkDataByType(scryfall.BulkDataTypeAllCards)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
needsUpdate, err := logic.CheckForUpdates(db, bulkData)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
if !needsUpdate {
|
|
|
|
fmt.Println("No update required.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Update required.")
|
|
|
|
|
|
|
|
if GetYesNoResponse("Run update? (y/N)") {
|
|
|
|
fmt.Println("Running update...")
|
|
|
|
|
|
|
|
logic.CreateCacheDirectories()
|
|
|
|
|
|
|
|
err = logic.UpdateSets(db)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
err = logic.UpdateCards(db, bulkData)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
fmt.Println("Update finished.")
|
|
|
|
}
|
2024-10-04 20:51:05 +00:00
|
|
|
break
|
|
|
|
case CreateStorageAreaSubcommand:
|
|
|
|
createStorageCmd := flag.NewFlagSet(CreateStorageAreaSubcommand, flag.ExitOnError)
|
|
|
|
storageAreaName := createStorageCmd.String("name", "",
|
|
|
|
"The name of the StorageArea to create.")
|
|
|
|
storageAreaType := createStorageCmd.String("type", "",
|
|
|
|
"The name of the StorageArea to create.")
|
|
|
|
|
|
|
|
createStorageCmd.Parse(flag.Args()[1:])
|
|
|
|
|
|
|
|
storageArea := database.StorageArea{Name: *storageAreaName, StorageType: *storageAreaType}
|
|
|
|
err := logic.CreateStorageArea(db, storageArea)
|
|
|
|
logic.Check(err)
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
break
|
|
|
|
case StoreSubcommand:
|
|
|
|
storeCmd := flag.NewFlagSet(StoreSubcommand, flag.ExitOnError)
|
|
|
|
storageArea := storeCmd.String("storagearea", "",
|
|
|
|
"The name of the StorageArea the card should be inserted to.")
|
|
|
|
id := storeCmd.String("id", "", "The CardPrintingId of the card to store.")
|
|
|
|
|
|
|
|
storeCmd.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
cardLocation := database.CardLocation{
|
|
|
|
CardPrintingId: *id,
|
|
|
|
StorageAreaId: storageAreaId,
|
|
|
|
}
|
|
|
|
cardLocationId, err := logic.StoreCard(db, cardLocation)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
fmt.Printf("%d\n", cardLocationId)
|
2024-10-04 12:44:22 +00:00
|
|
|
break
|
2024-09-25 16:49:01 +00:00
|
|
|
case ImportSubcommand:
|
2024-10-04 20:51:05 +00:00
|
|
|
importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError)
|
|
|
|
storageArea := importCmd.String("storagearea", "",
|
|
|
|
"The name of the StorageArea where cards should be imported.")
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
importCmd.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)
|
|
|
|
|
|
|
|
delverLensCards, err := delverlens.ParseExportFile(importCmd.Args()[0])
|
|
|
|
logic.Check(err)
|
2024-09-25 16:49:01 +00:00
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId)
|
|
|
|
logic.Check(err)
|
2024-09-25 10:48:01 +00:00
|
|
|
break
|
2024-10-28 19:47:23 +00:00
|
|
|
case SearchPrintingsSubcommand:
|
|
|
|
searchPrintingsCmd := flag.NewFlagSet(SearchPrintingsSubcommand, flag.ExitOnError)
|
|
|
|
|
|
|
|
setCode := searchPrintingsCmd.String("set-code", "", "The code for the set the card we're searching for belongs to.")
|
|
|
|
collectorNumber := searchPrintingsCmd.String("collector-number", "", "The collector number of the card we're searching for.")
|
|
|
|
foil := searchPrintingsCmd.String("foil", "E", "Whether the card we're searching for is foil.")
|
|
|
|
|
|
|
|
searchPrintingsCmd.Parse(flag.Args()[1:])
|
|
|
|
|
|
|
|
searchCriteria := logic.SearchCriteria{
|
|
|
|
SetCode: *setCode,
|
|
|
|
CollectorNumber: *collectorNumber,
|
|
|
|
Promo: logic.Either,
|
|
|
|
Language: "en",
|
|
|
|
}
|
|
|
|
|
|
|
|
if *foil == "Y" {
|
|
|
|
searchCriteria.Foil = logic.True
|
|
|
|
} else if *foil == "N" {
|
|
|
|
searchCriteria.Foil = logic.False
|
|
|
|
} else {
|
|
|
|
searchCriteria.Foil = logic.Either
|
|
|
|
}
|
|
|
|
|
|
|
|
searchOptions, err := logic.GetAllCardPrintingSearchOptions(db, searchCriteria)
|
|
|
|
logic.Check(err)
|
|
|
|
id, _, err := logic.GenericSearch(searchOptions)
|
|
|
|
logic.Check(err)
|
|
|
|
fmt.Println(id)
|
|
|
|
break
|
|
|
|
case SearchStorageSubcommand:
|
2024-10-04 12:44:22 +00:00
|
|
|
searchOptions, err := logic.GetAllStorageSearchOptions(db)
|
|
|
|
logic.Check(err)
|
2024-10-10 14:43:56 +00:00
|
|
|
id, _, err := logic.GenericSearch(searchOptions)
|
|
|
|
logic.Check(err)
|
|
|
|
fmt.Println(id)
|
2024-10-04 11:06:59 +00:00
|
|
|
break
|
2024-10-29 22:07:06 +00:00
|
|
|
case RemoveSubcommand:
|
|
|
|
removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError)
|
|
|
|
|
|
|
|
cardLocationId := removeCmd.Int("card-location-id", -1, "The card location to remove the card at.")
|
|
|
|
|
|
|
|
removeCmd.Parse(flag.Args()[1:])
|
|
|
|
|
|
|
|
if *cardLocationId == -1 {
|
|
|
|
log.Fatal(errors.New("No CardLocationId given."))
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := logic.GetCardAtLocation(db, *cardLocationId)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
err = logic.RemoveFromStorage(db, location)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
break
|
|
|
|
case ReplaceSubcommand:
|
|
|
|
replaceCmd := flag.NewFlagSet(ReplaceSubcommand, flag.ExitOnError)
|
|
|
|
|
|
|
|
cardLocationId := replaceCmd.Int("card-location-id", -1, "The card location to replace the card at.")
|
|
|
|
cardPrintingId := replaceCmd.String("card-printing-id", "", "The card printing to put at the specified location.")
|
|
|
|
|
|
|
|
replaceCmd.Parse(flag.Args()[1:])
|
|
|
|
|
|
|
|
if *cardLocationId == -1 {
|
|
|
|
log.Fatal(errors.New("No CardLocationId given."))
|
|
|
|
}
|
|
|
|
if *cardPrintingId == "" {
|
|
|
|
log.Fatal(errors.New("No CardPrintingId given."))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := logic.Replace(db, *cardLocationId, *cardPrintingId)
|
|
|
|
logic.Check(err)
|
|
|
|
break
|
2024-10-09 19:33:47 +00:00
|
|
|
case DeckSubcommand:
|
|
|
|
deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError)
|
|
|
|
deckCmd.Parse(flag.Args()[1:])
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
//filename := deckCmd.Args()[0]
|
2024-10-09 19:33:47 +00:00
|
|
|
break
|
2024-09-25 10:48:01 +00:00
|
|
|
default:
|
2024-09-25 16:49:01 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])
|
2024-09-25 10:48:01 +00:00
|
|
|
break
|
|
|
|
}
|
2024-05-28 14:29:13 +00:00
|
|
|
}
|