Move update code to its own package

This commit is contained in:
The Magician 2024-11-25 20:11:10 +00:00
parent 1f93aef749
commit 93c2b55754
2 changed files with 34 additions and 56 deletions

View File

@ -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)

View File

@ -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.")
}