0001-scum-new-subcommand.patch

#665
Raw
Created
Jan. 29, 2023, 6:09 a.m.
Expires
Never
Size
4.7 KB
Hits
169
Syntax
Diff
Private
✗ No
From ad11b0d1062ad56553253d707f398e597cd3eab5 Mon Sep 17 00:00:00 2001
From: Winston Weinert <git@winny.tech>
Date: Sat, 28 Jan 2023 22:21:09 -0600
Subject: [PATCH] scum: new subcommand

* Manage the save scum database
  * use list to see entries
  * use restore to restore an entry
---
 cmd/root.go            |  2 ++
 cmd/savecmd/save.go    |  5 ++--
 cmd/scumcmd/scumcmd.go | 55 ++++++++++++++++++++++++++++++++++++++++++
 gui/main.go            | 29 +++++++++++++---------
 gui/storedsavefile.go  | 12 +++++++++
 5 files changed, 89 insertions(+), 14 deletions(-)
 create mode 100644 cmd/scumcmd/scumcmd.go

diff --git a/cmd/root.go b/cmd/root.go
index a74bbc9..0a79267 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -7,6 +7,7 @@ import (
 	"github.com/sector-f/jhmod/cmd/managercmd"
 	"github.com/sector-f/jhmod/cmd/nvccmd"
 	"github.com/sector-f/jhmod/cmd/savecmd"
+	"github.com/sector-f/jhmod/cmd/scumcmd"
 	"github.com/spf13/cobra"
 )
 
@@ -20,6 +21,7 @@ func init() {
 	rootCmd.AddCommand(nvccmd.Cmd())
 	rootCmd.AddCommand(savecmd.Cmd())
 	rootCmd.AddCommand(managercmd.Cmd())
+	rootCmd.AddCommand(scumcmd.Cmd())
 }
 
 func Execute() {
diff --git a/cmd/savecmd/save.go b/cmd/savecmd/save.go
index d083691..added76 100644
--- a/cmd/savecmd/save.go
+++ b/cmd/savecmd/save.go
@@ -9,7 +9,8 @@ import (
 )
 
 func init() {
-	saveCmd.AddCommand(saveInfoCmd())
+	saveCmd.AddCommand(info())
+
 }
 
 var saveCmd = &cobra.Command{
@@ -17,7 +18,7 @@ var saveCmd = &cobra.Command{
 	Short: "Work with save files",
 }
 
-func saveInfoCmd() *cobra.Command {
+func info() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "info FILE ...",
 		Short: "Show information about a save file",
diff --git a/cmd/scumcmd/scumcmd.go b/cmd/scumcmd/scumcmd.go
new file mode 100644
index 0000000..9885ab4
--- /dev/null
+++ b/cmd/scumcmd/scumcmd.go
@@ -0,0 +1,55 @@
+package scumcmd
+
+import (
+	"fmt"
+
+	"github.com/sector-f/jhmod/gui"
+	"github.com/spf13/cobra"
+)
+
+func init() {
+	scumCmd.AddCommand(list())
+	scumCmd.AddCommand(restore())
+}
+
+func Cmd() *cobra.Command {
+	return scumCmd
+}
+
+var scumCmd = &cobra.Command{
+	Use:   "scum",
+	Short: "Manage save scums",
+}
+
+func restore() *cobra.Command {
+	cmd := &cobra.Command{
+		Use:   "restore ID",
+		Short: "Restores savescum db entry ID",
+		Args:  cobra.ExactArgs(1),
+		Run: func(cmd *cobra.Command, args []string) {
+			db := gui.Connect()
+			var saveFile gui.StoredSaveFile
+			db.First(&saveFile, args[0])
+			if err := saveFile.Restore(); err != nil {
+				panic(err)
+			}
+			fmt.Printf("Restored %s to %s\n", saveFile.AbsPath(), saveFile.OriginalBase)
+		},
+	}
+	return cmd
+}
+
+func list() *cobra.Command {
+	cmd := &cobra.Command{
+		Use:   "list",
+		Short: "List savescum database entries",
+		Run: func(cmd *cobra.Command, args []string) {
+			var saveFiles []gui.StoredSaveFile
+			gui.Connect().Find(&saveFiles)
+			for _, saveFile := range saveFiles {
+				fmt.Println(saveFile)
+			}
+		},
+	}
+	return cmd
+}
diff --git a/gui/main.go b/gui/main.go
index 7e1b8d7..6b82568 100644
--- a/gui/main.go
+++ b/gui/main.go
@@ -31,6 +31,22 @@ func sha256File(path string) ([]byte, error) {
 	return h.Sum(nil), nil
 }
 
+func Connect() *gorm.DB {
+
+	dbPath := path.Join(
+		getSaveScumDir(),
+		"db.sqlite3",
+	)
+	db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
+	if err != nil {
+		panic(fmt.Sprintf("Unable to open db \"%s\" %v", dbPath, err))
+	}
+	if err = db.AutoMigrate(&StoredSaveFile{}); err != nil {
+		panic(fmt.Sprintf("Could not migrate db.\n"))
+	}
+	return db
+}
+
 func Run() {
 	a := app.New()
 	w := a.NewWindow("jhmod manager")
@@ -67,18 +83,7 @@ func Run() {
 		fmt.Fprintf(os.Stderr, "Cannot make savescumdir %v\n", mkdirErr)
 		return
 	}
-
-	dbPath := path.Join(
-		getSaveScumDir(),
-		"db.sqlite3",
-	)
-	db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
-	if err != nil {
-		panic(fmt.Sprintf("Unable to open db \"%s\" %v", dbPath, err))
-	}
-	if err = db.AutoMigrate(&StoredSaveFile{}); err != nil {
-		panic(fmt.Sprintf("Could not migrate db.\n"))
-	}
+	db := Connect()
 
 	db.Last(&last)
 	updateLast(last)
diff --git a/gui/storedsavefile.go b/gui/storedsavefile.go
index 0db1ef9..15bc8d3 100644
--- a/gui/storedsavefile.go
+++ b/gui/storedsavefile.go
@@ -1,6 +1,7 @@
 package gui
 
 import (
+	"fmt"
 	"os"
 	path "path/filepath"
 
@@ -26,6 +27,17 @@ type StoredSaveFile struct {
 	Seed uint32
 }
 
+func (f StoredSaveFile) String() string {
+	return fmt.Sprintf("% 3d [%s] %s %s %s (%d)",
+		f.Id,
+		f.CreatedAt.Format("2006-01-02 15:04:06"),
+		f.GameMode,
+		f.PlayerName,
+		f.CurrentLevel,
+		f.Seed,
+	)
+}
+
 func (f StoredSaveFile) AbsPath() string {
 	return path.Join(getSaveScumDir(), f.StoredRelPath)
 }
-- 
2.34.1.windows.1