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
|
// writes out the cluster information it seperate files
|
||||||
// to make it humanly possible to hand edit things as needed
|
// to make it humanly possible to hand edit things as needed
|
||||||
func (m *Repos) ConfigSave() error {
|
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()
|
data, err := m.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("proto.Marshal() failed len", len(data), err)
|
log.Info("proto.Marshal() failed len", len(data), err)
|
||||||
|
@ -24,6 +33,9 @@ func (m *Repos) ConfigSave() error {
|
||||||
|
|
||||||
s := m.FormatTEXT()
|
s := m.FormatTEXT()
|
||||||
configWrite("forge.text", []byte(s))
|
configWrite("forge.text", []byte(s))
|
||||||
|
|
||||||
|
s = m.FormatJSON()
|
||||||
|
configWrite("forge.json", []byte(s))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,9 +88,7 @@ func (m *Repos) ConfigLoad() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadFile(filename string) ([]byte, error) {
|
func loadFile(filename string) ([]byte, error) {
|
||||||
homeDir, err := os.UserHomeDir()
|
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
|
||||||
p := filepath.Join(homeDir, ".config/forge")
|
|
||||||
fullname := filepath.Join(p, filename)
|
|
||||||
data, err := os.ReadFile(fullname)
|
data, err := os.ReadFile(fullname)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
// if file does not exist, just return nil. this
|
// 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 {
|
func configWrite(filename string, data []byte) error {
|
||||||
homeDir, err := os.UserHomeDir()
|
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
|
||||||
p := filepath.Join(homeDir, ".config/forge")
|
|
||||||
fname := filepath.Join(p, filename)
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
||||||
cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0666)
|
|
||||||
defer cfgfile.Close()
|
defer cfgfile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("open config file :", err)
|
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:
|
build:
|
||||||
GO111MODULE=off go build
|
GO111MODULE=off go build
|
||||||
./example
|
./example
|
||||||
|
FORGE_HOME=/tmp/forge ./example
|
||||||
|
|
||||||
goimports:
|
goimports:
|
||||||
goimports -w *.go
|
goimports -w *.go
|
||||||
|
|
Loading…
Reference in New Issue