diff --git a/sevenkeys/main.go b/sevenkeys/main.go index 5b629f8..27b22d3 100644 --- a/sevenkeys/main.go +++ b/sevenkeys/main.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "encoding/json" "errors" "flag" @@ -12,28 +11,11 @@ import ( "sevenkeys/database" "sevenkeys/delverlens" "sevenkeys/logic" - "sevenkeys/logic/scryfall" - "strings" + "sevenkeys/update" "github.com/mtgban/go-mtgban/cardtrader" ) -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" -} - const ( UpdateSubcommand string = "update" CreateStorageAreaSubcommand string = "createstorage" @@ -56,16 +38,6 @@ func main() { 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.") @@ -74,33 +46,7 @@ func main() { switch flag.Args()[0] { case UpdateSubcommand: - 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.") - } + update.UpdateScryfallData(db) break case CreateStorageAreaSubcommand: createStorageCmd := flag.NewFlagSet(CreateStorageAreaSubcommand, flag.ExitOnError) diff --git a/sevenkeys/update/scryfall.go b/sevenkeys/update/scryfall.go new file mode 100644 index 0000000..96da144 --- /dev/null +++ b/sevenkeys/update/scryfall.go @@ -0,0 +1,32 @@ +package update + +import ( + "database/sql" + "fmt" + "sevenkeys/logic" + "sevenkeys/logic/scryfall" +) + +func UpdateScryfallData(db *sql.DB) { + 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 + } + + logic.CreateCacheDirectories() + + err = logic.UpdateSets(db) + logic.Check(err) + + err = logic.UpdateCards(db, bulkData) + logic.Check(err) + + fmt.Println("Update finished.") +}