130 lines
2.8 KiB
Go
130 lines
2.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"sevenkeys/database"
|
|
"sevenkeys/logic"
|
|
)
|
|
|
|
var output string
|
|
|
|
func MainCliLoop(db *sql.DB) {
|
|
var command string
|
|
|
|
var selectedStorageArea database.StorageArea
|
|
var cardLocation database.CardLocation
|
|
|
|
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
|
SetCode: "",
|
|
Foil: logic.Either,
|
|
Promo: logic.Either,
|
|
Language: "en",
|
|
}
|
|
var insertSearchOptions logic.InsertSearchOptions
|
|
|
|
// TODO: Add the ability to modify this
|
|
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
|
SetCode: "",
|
|
Foil: logic.Either,
|
|
Promo: logic.Either,
|
|
Language: "en",
|
|
}
|
|
|
|
for {
|
|
ShowSplashScreen()
|
|
showStorageInfo(os.Stdout, selectedStorageArea)
|
|
showInsertSearchCriteria(insertSearchCriteria)
|
|
showSelectedCard()
|
|
showCopiesInserted()
|
|
|
|
command = GetStringResponse("SEVENKEYS $")
|
|
|
|
var err error
|
|
|
|
switch command {
|
|
case "q", "quit":
|
|
return
|
|
case "n", "newstorage":
|
|
var storageArea database.StorageArea
|
|
storageArea.Name = GetStringResponse("Storage area name:")
|
|
storageArea.StorageType = GetStringResponse("Storage area type (Binder/Box):")
|
|
err = logic.CreateStorageArea(db, storageArea)
|
|
logic.Check(err)
|
|
break
|
|
case "a", "area":
|
|
area, err := logic.SelectStorageArea(db)
|
|
logic.Check(err)
|
|
selectedStorageArea = area
|
|
cardLocation.StorageAreaId = area.Id
|
|
break
|
|
case "c", "criteria":
|
|
insertSearchCriteria = getSearchCriteria()
|
|
break
|
|
case "s", "search":
|
|
if shouldRefreshSearch {
|
|
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
|
|
}
|
|
|
|
var previousCardPrintingId = cardLocation.CardPrintingId
|
|
|
|
pk, err := logic.GenericSearch(insertSearchOptions)
|
|
cardLocation.CardPrintingId = pk
|
|
|
|
var exitError *exec.ExitError
|
|
if errors.As(err, &exitError) {
|
|
break
|
|
}
|
|
logic.Check(err)
|
|
|
|
output = ""
|
|
if cardLocation.CardPrintingId != previousCardPrintingId {
|
|
copiesInserted = 0
|
|
}
|
|
break
|
|
case "i", "insert":
|
|
insertSelectedCard(db, selectedStorageArea, cardLocation)
|
|
break
|
|
case "l", "locate":
|
|
filename := GetStringResponse("Filename:")
|
|
cardNames, err := logic.GetCardNamesFromFile(filename)
|
|
logic.Check(err)
|
|
|
|
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
|
|
logic.Check(err)
|
|
|
|
if len(locations) == 0 {
|
|
fmt.Println("No results found")
|
|
fmt.Scanln()
|
|
break
|
|
}
|
|
|
|
for _, location := range locations {
|
|
description := logic.GetLocationDescription(location)
|
|
fmt.Println(description)
|
|
|
|
for true {
|
|
todo := GetStringResponse("TODO:")
|
|
|
|
if todo == "r" {
|
|
logic.RemoveFromStorage(db, location)
|
|
} else if todo == "n" {
|
|
continue
|
|
} else {
|
|
fmt.Printf("Unrecognized option: %s\n", todo)
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Scanln()
|
|
break
|
|
default:
|
|
fmt.Println("Unrecognized command:", command)
|
|
break
|
|
}
|
|
}
|
|
}
|