package main import ( "errors" "flag" "fmt" "log" "os" "sevenkeys/config" "sevenkeys/database" "sevenkeys/delverlens" "sevenkeys/logic" "sevenkeys/update" "sevenkeys/webui" ) const ( UpdateSubcommand string = "update" CreateStorageAreaSubcommand string = "create-storage" StoreSubcommand string = "store" ImportSubcommand string = "import" SearchPrintingsSubcommand string = "search-printings" SearchStorageSubcommand string = "search-storage" AddSubcommand string = "add" RemoveSubcommand string = "remove" ReplaceSubcommand string = "replace" WebUiSubcommand string = "webui" BlueprintsCommand string = "blueprints" ) func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) var profile string flag.StringVar(&profile, "profile", "dev", "The database profile to use.") flag.Parse() config := config.ReadConfigFromFile("config." + profile + ".json") db := database.GetDatabaseFromConfig(config.DatabaseConfig) if len(flag.Args()) == 0 { fmt.Fprintln(os.Stderr, "Please specify a subcommand.") os.Exit(1) } switch flag.Args()[0] { case UpdateSubcommand: err := update.UpdateCardtraderData(db, config.CardtraderToken) logic.Check(err) 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) id := storeCmd.Int("id", -1, "The CardPrintingId of the card to store.") storageArea := storeCmd.String("storage-area", "", "The name of the StorageArea the card should be inserted to.") 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) } productLocation := database.ProductLocation{ CardtraderBlueprintId: *id, StorageAreaId: storageAreaId, } productLocationId, err := logic.StoreProduct(db, productLocation) logic.Check(err) fmt.Printf("%d\n", productLocationId) break case ImportSubcommand: importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError) storageArea := importCmd.String("storage-area", "", "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) filename := importCmd.Args()[0] delverLensCards, err := delverlens.ParseExportFile(filename) logic.Check(err) err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId) logic.Check(err) break /* TODO: Rewrite this to search Blueprints 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: searchOptions, err := logic.GetAllStorageSearchOptions(db) logic.Check(err) id, _, err := logic.GenericSearch(searchOptions) logic.Check(err) fmt.Println(id) break case AddSubcommand: addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError) cardtraderBlueprintId := addCmd.Int("blueprint-id", -1, "The Cardtrader Bluepritn ID of the product to add to storage.") storageArea := addCmd.String("storage-area", "", "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) productLocation := database.ProductLocation{ CardtraderBlueprintId: *cardtraderBlueprintId, StorageAreaId: storageAreaId, } logic.StoreProduct(db, productLocation) break 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.") cardtraderBlueprintId := replaceCmd.Int("blueprint-id", -1, "The card printing to put at the specified location.") replaceCmd.Parse(flag.Args()[1:]) if *cardLocationId == -1 { log.Fatal(errors.New("No CardLocationId given.")) } err := logic.Replace(db, *cardLocationId, *cardtraderBlueprintId) logic.Check(err) break case WebUiSubcommand: webui.Start(db) /* storageAreas, err := database.GetAllStorageAreas(db) if err != nil { log.Fatal(err) } fmt.Println(storageAreas) */ break case BlueprintsCommand: blueprints, err := database.GetAllCardtraderBlueprints(db) if err != nil { log.Fatal(err) } fmt.Println(blueprints) break default: fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1]) break } }