From 1b42efb856c15e65de803b0c8d74f90c1a496fe2 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 24 Sep 2024 12:12:11 -0500 Subject: [PATCH] remove peer dbs if they exist --- cmd/network/switch.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/network/switch.go b/cmd/network/switch.go index e9fcdfe..b3bee64 100644 --- a/cmd/network/switch.go +++ b/cmd/network/switch.go @@ -166,6 +166,11 @@ var switchCmd = &cobra.Command{ slogs.Logr.Fatal("error saving chia config", "error", err) } + err = removeFileIfExists(path.Join(chiaRoot, "db", peersFilePath)) + if err != nil { + slogs.Logr.Error("error removing old peers.dat file", "path", peersFilePath, "error", err) + } + slogs.Logr.Info("Complete") }, } @@ -215,3 +220,21 @@ func moveAndOverwriteFile(sourcePath, destPath string) error { slogs.Logr.Debug("moved successfully", "source", sourcePath, "dest", destPath) return nil } + +func removeFileIfExists(path string) error { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + slogs.Logr.Debug("source path doesn't exist, skipping delete", "path", path) + return nil + } + return fmt.Errorf("error checking source file: %w", err) + } + + slogs.Logr.Debug("removing file at path", "path", path) + err := os.Remove(path) + if err != nil { + return fmt.Errorf("error removing file: %w", err) + } + + return nil +}