2024-06-10 15:34:16 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-06-11 09:43:30 +00:00
|
|
|
"database/sql"
|
2024-06-12 20:33:50 +00:00
|
|
|
"errors"
|
2024-06-10 15:34:16 +00:00
|
|
|
"fmt"
|
2024-08-20 14:28:49 +00:00
|
|
|
"os"
|
2024-06-12 20:33:50 +00:00
|
|
|
"os/exec"
|
2024-08-20 11:57:36 +00:00
|
|
|
"sevenkeys/database"
|
2024-06-12 20:33:50 +00:00
|
|
|
"sevenkeys/logic"
|
2024-06-10 15:34:16 +00:00
|
|
|
)
|
|
|
|
|
2024-06-12 22:37:13 +00:00
|
|
|
var output string
|
|
|
|
|
|
|
|
func showOutput() {
|
|
|
|
if output != "" {
|
|
|
|
fmt.Println(output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 09:57:02 +00:00
|
|
|
func MainCliLoop(db *sql.DB) {
|
2024-06-10 15:34:16 +00:00
|
|
|
var command string
|
2024-08-20 14:28:49 +00:00
|
|
|
var selectedStorageArea database.StorageArea
|
2024-09-05 09:53:14 +00:00
|
|
|
var cardLocation database.CardLocation
|
2024-06-10 15:34:16 +00:00
|
|
|
|
|
|
|
for {
|
2024-06-12 20:33:50 +00:00
|
|
|
ShowSplashScreen()
|
2024-08-20 14:28:49 +00:00
|
|
|
showStorageInfo(os.Stdout, selectedStorageArea)
|
2024-06-12 22:37:13 +00:00
|
|
|
showSearchCriteria()
|
|
|
|
showSelectedCard()
|
|
|
|
showCopiesInserted()
|
2024-06-12 20:33:50 +00:00
|
|
|
|
2024-06-10 15:34:16 +00:00
|
|
|
command = GetStringResponse("SEVENKEYS $")
|
|
|
|
|
2024-08-20 11:57:36 +00:00
|
|
|
var err error
|
|
|
|
|
2024-06-10 15:34:16 +00:00
|
|
|
switch command {
|
2024-06-14 09:50:46 +00:00
|
|
|
case "q", "quit":
|
2024-06-10 15:34:16 +00:00
|
|
|
return
|
2024-08-20 11:57:36 +00:00
|
|
|
case "n", "newstorage":
|
|
|
|
var storageArea database.StorageArea
|
|
|
|
storageArea.Name = GetStringResponse("Storage area name:")
|
2024-08-20 17:50:38 +00:00
|
|
|
storageArea.StorageType = GetStringResponse("Storage area type (Binder/Box):")
|
2024-08-20 11:57:36 +00:00
|
|
|
err = logic.CreateStorageArea(db, storageArea)
|
|
|
|
logic.Check(err)
|
|
|
|
break
|
2024-08-20 17:00:23 +00:00
|
|
|
case "a", "area":
|
|
|
|
area, err := logic.SelectStorageArea(db)
|
|
|
|
logic.Check(err)
|
|
|
|
selectedStorageArea = area
|
2024-09-05 09:53:14 +00:00
|
|
|
cardLocation.StorageAreaId = area.Id
|
2024-06-14 09:50:46 +00:00
|
|
|
break
|
|
|
|
case "c", "criteria":
|
2024-06-12 20:33:50 +00:00
|
|
|
getSearchCriteria()
|
|
|
|
break
|
2024-06-14 09:50:46 +00:00
|
|
|
case "s", "search":
|
2024-06-12 20:33:50 +00:00
|
|
|
getSearchOptions(db)
|
2024-06-12 22:37:13 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
var previousCardPrintingId = cardLocation.CardPrintingId
|
2024-06-12 22:37:13 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
|
2024-06-12 20:33:50 +00:00
|
|
|
var exitError *exec.ExitError
|
|
|
|
if errors.As(err, &exitError) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
logic.Check(err)
|
2024-06-12 22:37:13 +00:00
|
|
|
|
|
|
|
output = ""
|
2024-09-05 09:53:14 +00:00
|
|
|
if cardLocation.CardPrintingId != previousCardPrintingId {
|
2024-06-12 22:37:13 +00:00
|
|
|
copiesInserted = 0
|
|
|
|
}
|
2024-06-10 15:34:16 +00:00
|
|
|
break
|
2024-06-14 09:50:46 +00:00
|
|
|
case "i", "insert":
|
2024-09-05 09:53:14 +00:00
|
|
|
insertSelectedCard(db, selectedStorageArea, cardLocation)
|
|
|
|
break
|
|
|
|
case "l", "locate":
|
|
|
|
cardName := GetStringResponse("Card name:")
|
|
|
|
locations, err := logic.LocateCards(db, cardName)
|
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
for _, location := range locations {
|
|
|
|
fmt.Printf("%s (%s %s) [%s]", location.CardName, location.SetCode, location.CollectorNumber, location.Language)
|
|
|
|
|
|
|
|
if location.IsFoil {
|
|
|
|
fmt.Printf(" FOIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if location.IsPromo {
|
|
|
|
fmt.Printf(" PROMO")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(" in %s \"%s\" at position %d\n", location.StorageAreaType, location.StorageAreaName, location.Position)
|
|
|
|
}
|
|
|
|
fmt.Scanln()
|
|
|
|
|
2024-06-10 15:34:16 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Println("Unrecognized command:", command)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|