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
|
|
|
|
|
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-09-07 18:07:48 +00:00
|
|
|
|
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
|
|
|
|
2024-09-07 18:07:48 +00:00
|
|
|
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
|
|
|
SetCode: "",
|
|
|
|
Foil: logic.Either,
|
|
|
|
Promo: logic.Either,
|
|
|
|
Language: "en",
|
|
|
|
}
|
|
|
|
var insertSearchOptions logic.InsertSearchOptions
|
|
|
|
|
2024-09-07 23:24:24 +00:00
|
|
|
// TODO: Make these do something and add the ability to modify them
|
|
|
|
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
|
|
|
SetCode: "",
|
|
|
|
Foil: logic.True,
|
|
|
|
Promo: logic.Either,
|
|
|
|
Language: "en",
|
|
|
|
}
|
|
|
|
|
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-09-07 18:07:48 +00:00
|
|
|
showInsertSearchCriteria(insertSearchCriteria)
|
2024-06-12 22:37:13 +00:00
|
|
|
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-09-07 18:07:48 +00:00
|
|
|
insertSearchCriteria = getSearchCriteria()
|
2024-06-12 20:33:50 +00:00
|
|
|
break
|
2024-06-14 09:50:46 +00:00
|
|
|
case "s", "search":
|
2024-09-07 18:07:48 +00:00
|
|
|
if shouldRefreshSearch {
|
|
|
|
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
|
|
|
|
}
|
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-07 18:07:48 +00:00
|
|
|
pk, err := logic.GenericSearch(insertSearchOptions)
|
|
|
|
cardLocation.CardPrintingId = pk
|
|
|
|
|
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":
|
2024-09-07 23:24:24 +00:00
|
|
|
filename := GetStringResponse("Filename:")
|
|
|
|
cardNames, err := logic.GetCardNamesFromFile(filename)
|
|
|
|
logic.Check(err)
|
2024-09-07 18:07:48 +00:00
|
|
|
|
2024-09-07 23:24:24 +00:00
|
|
|
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
|
2024-09-05 09:53:14 +00:00
|
|
|
logic.Check(err)
|
|
|
|
|
|
|
|
for _, location := range locations {
|
2024-09-07 23:24:24 +00:00
|
|
|
fmt.Println(location)
|
2024-09-05 09:53:14 +00:00
|
|
|
}
|
|
|
|
fmt.Scanln()
|
|
|
|
|
2024-06-10 15:34:16 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Println("Unrecognized command:", command)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|