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 package main
import ( import (
"bufio"
"encoding/json" "encoding/json"
"errors" "errors"
"flag" "flag"
@ -12,28 +11,11 @@ import (
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/delverlens" "sevenkeys/delverlens"
"sevenkeys/logic" "sevenkeys/logic"
"sevenkeys/logic/scryfall" "sevenkeys/update"
"strings"
"github.com/mtgban/go-mtgban/cardtrader" "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 ( const (
UpdateSubcommand string = "update" UpdateSubcommand string = "update"
CreateStorageAreaSubcommand string = "createstorage" CreateStorageAreaSubcommand string = "createstorage"
@ -56,16 +38,6 @@ func main() {
flag.Parse() flag.Parse()
db := database.GetDatabaseFromConfig("config." + profile + ".json") 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 { if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Please specify a subcommand.") fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
@ -74,33 +46,7 @@ func main() {
switch flag.Args()[0] { switch flag.Args()[0] {
case UpdateSubcommand: case UpdateSubcommand:
fmt.Println("Checking for updates...") update.UpdateScryfallData(db)
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.")
}
break break
case CreateStorageAreaSubcommand: case CreateStorageAreaSubcommand:
createStorageCmd := flag.NewFlagSet(CreateStorageAreaSubcommand, flag.ExitOnError) 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.")
}