TheMathemagicians/sevenkeys/main.go

124 lines
3.5 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"sevenkeys/cli"
"sevenkeys/database"
"sevenkeys/delverlens"
"sevenkeys/logic"
)
const (
UpdateSubcommand string = "update"
CreateStorageAreaSubcommand string = "createstorage"
StoreSubcommand string = "store"
ImportSubcommand string = "import"
SearchSubcommand string = "search"
DeckSubcommand string = "deck"
)
func main() {
var profile string
flag.StringVar(&profile, "profile", "development", "The database profile to use.")
flag.Parse()
db := database.GetDatabaseFromConfig("config." + profile + ".json")
/* Sad.
figlet.ReadFigletFonts()
cli.ShowSplashScreen()
*/
// TODO: Decide in what form we need to retain this functionality if any
//cli.MainCliLoop(db)
//searchCmd := flag.NewFlagSet(SearchSubcommand, flag.ExitOnError)
//name := searchCmd.String("name", "", "The card name to search for.")
if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
os.Exit(1)
}
switch flag.Args()[0] {
case UpdateSubcommand:
cli.RunUpdateCheck(db)
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)
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)
break
case ImportSubcommand:
importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError)
storageArea := importCmd.String("storagearea", "",
"The name of the StorageArea where cards should be imported.")
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)
err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId)
logic.Check(err)
break
case SearchSubcommand:
searchOptions, err := logic.GetAllStorageSearchOptions(db)
logic.Check(err)
id, _, err := logic.GenericSearch(searchOptions)
logic.Check(err)
fmt.Println(id)
break
case DeckSubcommand:
deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError)
deckCmd.Parse(flag.Args()[1:])
//filename := deckCmd.Args()[0]
break
default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])
break
}
}