From 08757bc3150953870b2e56ec82bb82027ffbaa7a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 31 Oct 2024 09:19:15 -0500 Subject: code cleanups Signed-off-by: Jeff Carr --- add.go | 6 ++--- backup.go | 64 ------------------------------------------------ config.go | 14 +---------- configBackup.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 80 deletions(-) delete mode 100644 backup.go create mode 100644 configBackup.go diff --git a/add.go b/add.go index 8c86322..e1c0298 100644 --- a/add.go +++ b/add.go @@ -27,7 +27,7 @@ func (x *Hypervisor) GetMemoryPrintable() string { return fmt.Sprintf("%d GB", i) } -func (all *Droplets) FindDroplet(name string) *Droplet { +func (all *Droplets) oldFindDroplet(name string) *Droplet { for _, d := range all.Droplets { if d.Hostname == name { return d @@ -45,7 +45,7 @@ func (c *Cluster) FindDropletByName(name string) *Droplet { return nil } -func (c *Cluster) FindHypervisor(name string) *Hypervisor { +func (c *Cluster) FindHypervisorByName(name string) *Hypervisor { for _, h := range c.Hypervisors { if h.Hostname == name { return h @@ -55,7 +55,7 @@ func (c *Cluster) FindHypervisor(name string) *Hypervisor { } func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor { - h := c.FindHypervisor(hostname) + h := c.FindHypervisorByName(hostname) if h != nil { return h } diff --git a/backup.go b/backup.go deleted file mode 100644 index 2834993..0000000 --- a/backup.go +++ /dev/null @@ -1,64 +0,0 @@ -package virtbuf - -// thank chatgpt for this because why. why write this if you can have it -// kick this out in 30 seconds - -import ( - "errors" - "fmt" - "io" - "log" - "os" - "path/filepath" -) - -func backupFiles(srcDir string, destDir string) error { - // Create the destination directory - err := os.MkdirAll(destDir, os.ModePerm) - if err != nil { - return errors.New(fmt.Sprintf("Failed to create directory: %v", err)) - } - - // Read the contents of the source directory - entries, err := os.ReadDir(srcDir) - if err != nil { - return errors.New(fmt.Sprintf("Failed to read directory: %v", err)) - } - - // Iterate over the entries in the source directory - for _, entry := range entries { - // Skip directories and files that do not have the .test extension - if entry.IsDir() { - continue - } - - log.Println("backing up file", entry.Name()) - srcPath := filepath.Join(srcDir, entry.Name()) - destPath := filepath.Join(destDir, entry.Name()) - - // Copy the file - if err := copyFile(srcPath, destPath); err != nil { - return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err)) - } - } - return nil -} - -// copyFile copies a file from src to dest -func copyFile(src, dest string) error { - srcFile, err := os.Open(src) - if err != nil { - return err - } - defer srcFile.Close() - - destFile, err := os.Create(dest) - if err != nil { - return err - } - defer destFile.Close() - - // Copy the content - _, err = io.Copy(destFile, srcFile) - return err -} diff --git a/config.go b/config.go index 2fd4c33..7973da9 100644 --- a/config.go +++ b/config.go @@ -8,7 +8,6 @@ import ( "fmt" "os" "path/filepath" - "time" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/prototext" @@ -19,7 +18,7 @@ import ( // to make it humanly possible to hand edit things as needed func (c *Cluster) ConfigSave() error { // try to backup the current cluster config files - if err := backupConfigFiles(); err != nil { + if err := backupConfig(); err != nil { return err } @@ -83,17 +82,6 @@ func (c *Cluster) ConfigSave() error { return nil } -func backupConfigFiles() error { - // make a new dir to backup the files - now := time.Now() - // timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R - timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work? - srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME")) - destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp) - - return backupFiles(srcDir, destDir) -} - func (c *Cluster) ConfigLoad() error { if c == nil { return errors.New("It's not safe to run ConfigLoad() on a nil cluster") diff --git a/configBackup.go b/configBackup.go new file mode 100644 index 0000000..6e58aa9 --- /dev/null +++ b/configBackup.go @@ -0,0 +1,76 @@ +package virtbuf + +// thank chatgpt for this because why. why write this if you can have it +// kick this out in 30 seconds + +import ( + "errors" + "fmt" + "io" + "log" + "os" + "path/filepath" + "time" +) + +func backupConfig() error { + // make a new dir to backup the files + now := time.Now() + // timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R + timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work? + srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME")) + destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp) + + return backupFiles(srcDir, destDir) +} + +func backupFiles(srcDir string, destDir string) error { + // Create the destination directory + err := os.MkdirAll(destDir, os.ModePerm) + if err != nil { + return errors.New(fmt.Sprintf("Failed to create directory: %v", err)) + } + + // Read the contents of the source directory + entries, err := os.ReadDir(srcDir) + if err != nil { + return errors.New(fmt.Sprintf("Failed to read directory: %v", err)) + } + + // Iterate over the entries in the source directory + for _, entry := range entries { + // Skip directories and files that do not have the .test extension + if entry.IsDir() { + continue + } + + log.Println("backing up file", entry.Name()) + srcPath := filepath.Join(srcDir, entry.Name()) + destPath := filepath.Join(destDir, entry.Name()) + + // Copy the file + if err := copyFile(srcPath, destPath); err != nil { + return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err)) + } + } + return nil +} + +// copyFile copies a file from src to dest +func copyFile(src, dest string) error { + srcFile, err := os.Open(src) + if err != nil { + return err + } + defer srcFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + return err + } + defer destFile.Close() + + // Copy the content + _, err = io.Copy(destFile, srcFile) + return err +} -- cgit v1.2.3