Add the ability to select storage area
This commit is contained in:
parent
de116aee20
commit
eb5ecd5fb4
|
@ -43,8 +43,10 @@ func MainCliLoop(db *sql.DB) {
|
|||
err = logic.CreateStorageArea(db, storageArea)
|
||||
logic.Check(err)
|
||||
break
|
||||
case "l", "location":
|
||||
cardStorageLocation.StorageBox = GetStringResponse("Storage location:")
|
||||
case "a", "area":
|
||||
area, err := logic.SelectStorageArea(db)
|
||||
logic.Check(err)
|
||||
selectedStorageArea = area
|
||||
break
|
||||
case "r", "source":
|
||||
cardStorageLocation.Source = GetStringResponse("Card source:")
|
||||
|
|
|
@ -8,6 +8,29 @@ type StorageArea struct {
|
|||
Type string
|
||||
}
|
||||
|
||||
func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
|
||||
var storageAreas []StorageArea
|
||||
|
||||
query := `SELECT * FROM StorageArea;`
|
||||
rows, err := db.Query(query)
|
||||
defer rows.Close()
|
||||
if err != nil {
|
||||
return storageAreas, err
|
||||
}
|
||||
|
||||
var area StorageArea
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&area.Id, &area.Name, &area.Type)
|
||||
if err != nil {
|
||||
return storageAreas, err
|
||||
}
|
||||
|
||||
storageAreas = append(storageAreas, area)
|
||||
}
|
||||
|
||||
return storageAreas, nil
|
||||
}
|
||||
|
||||
func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
|
||||
query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);`
|
||||
|
||||
|
|
|
@ -2,7 +2,11 @@ package logic
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sevenkeys/database"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
|
||||
|
@ -16,6 +20,45 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
|
||||
var selectedStorageArea database.StorageArea
|
||||
|
||||
storageAreas, err := database.GetAllStorageAreas(db)
|
||||
if err != nil {
|
||||
return selectedStorageArea, err
|
||||
}
|
||||
|
||||
cmd := exec.Command("fzf")
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
fzfStdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return selectedStorageArea, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer fzfStdin.Close()
|
||||
for _, area := range storageAreas {
|
||||
io.WriteString(fzfStdin, area.Name)
|
||||
}
|
||||
}()
|
||||
|
||||
fzfOutput, err := cmd.Output()
|
||||
if err != nil {
|
||||
return selectedStorageArea, err
|
||||
}
|
||||
|
||||
key := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||
for _, area := range storageAreas {
|
||||
if area.Name == key {
|
||||
selectedStorageArea = area
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return selectedStorageArea, nil
|
||||
}
|
||||
|
||||
func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error {
|
||||
lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue