hell ya
This commit is contained in:
parent
39a8d9e13e
commit
7da3f23de1
23
config.go
23
config.go
|
@ -14,6 +14,15 @@ import (
|
|||
// writes out the cluster information it seperate files
|
||||
// to make it humanly possible to hand edit things as needed
|
||||
func (m *Repos) ConfigSave() error {
|
||||
if os.Getenv("FORGE_HOME") == "" {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
||||
os.Setenv("FORGE_HOME", fullpath)
|
||||
}
|
||||
// try to backup the current cluster config files
|
||||
if err := backupConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := m.Marshal()
|
||||
if err != nil {
|
||||
log.Info("proto.Marshal() failed len", len(data), err)
|
||||
|
@ -24,6 +33,9 @@ func (m *Repos) ConfigSave() error {
|
|||
|
||||
s := m.FormatTEXT()
|
||||
configWrite("forge.text", []byte(s))
|
||||
|
||||
s = m.FormatJSON()
|
||||
configWrite("forge.json", []byte(s))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -76,9 +88,7 @@ func (m *Repos) ConfigLoad() error {
|
|||
}
|
||||
|
||||
func loadFile(filename string) ([]byte, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
p := filepath.Join(homeDir, ".config/forge")
|
||||
fullname := filepath.Join(p, filename)
|
||||
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
|
||||
data, err := os.ReadFile(fullname)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
// if file does not exist, just return nil. this
|
||||
|
@ -94,10 +104,9 @@ func loadFile(filename string) ([]byte, error) {
|
|||
}
|
||||
|
||||
func configWrite(filename string, data []byte) error {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
p := filepath.Join(homeDir, ".config/forge")
|
||||
fname := filepath.Join(p, filename)
|
||||
cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0666)
|
||||
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
|
||||
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
log.Warn("open config file :", err)
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
package forgepb
|
||||
|
||||
// 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("FORGE_HOME"))
|
||||
destDir := filepath.Join(os.Getenv("FORGE_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
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
build:
|
||||
GO111MODULE=off go build
|
||||
./example
|
||||
FORGE_HOME=/tmp/forge ./example
|
||||
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
|
|
Loading…
Reference in New Issue