cleanup config file handling
This commit is contained in:
parent
ac107331fc
commit
dbde2f51b8
30
config.go
30
config.go
|
@ -18,13 +18,15 @@ import (
|
||||||
|
|
||||||
func (f *Forge) ConfigSave() error {
|
func (f *Forge) ConfigSave() error {
|
||||||
var err error
|
var err error
|
||||||
// backup the current config files
|
/*
|
||||||
if e := f.backupConfig(); e != nil {
|
// backup the current config files
|
||||||
log.Info("forge.BackupConfig() error", e)
|
if e := f.backupConfig(); e != nil {
|
||||||
err = e
|
log.Info("forge.BackupConfig() error", e)
|
||||||
// continue here? notsure. could be bad either way
|
err = e
|
||||||
// out of disk space?
|
// continue here? notsure. could be bad either way
|
||||||
}
|
// out of disk space?
|
||||||
|
}
|
||||||
|
*/
|
||||||
if f.Config != nil {
|
if f.Config != nil {
|
||||||
if e := f.Config.ConfigSave(); e != nil {
|
if e := f.Config.ConfigSave(); e != nil {
|
||||||
log.Info("forge.Config.ConfigSave() error", e)
|
log.Info("forge.Config.ConfigSave() error", e)
|
||||||
|
@ -101,9 +103,6 @@ func (c *ForgeConfigs) ConfigLoad(fullpath string) error {
|
||||||
log.Info("However, the config files could not be loaded")
|
log.Info("However, the config files could not be loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
// first time user. make a template config file
|
|
||||||
c.sampleConfig()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,17 +160,6 @@ func configWrite(filename string, data []byte) error {
|
||||||
cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n"))
|
cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n"))
|
||||||
cfgfile.Write([]byte("\n"))
|
cfgfile.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
if filename == "forge.json" {
|
|
||||||
// add header
|
|
||||||
cfgfile.Write([]byte("\n"))
|
|
||||||
cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\n"))
|
|
||||||
cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n"))
|
|
||||||
cfgfile.Write([]byte("\n"))
|
|
||||||
cfgfile.Write([]byte("# this file is parsed only if forge.text is missing\n"))
|
|
||||||
cfgfile.Write([]byte("# also, these comment lines don't work in json files and have to be removed for Marshal() to work\n"))
|
|
||||||
cfgfile.Write([]byte("# probably, JSON syntax for this is just going to be deprecated for the TEXT syntax\n"))
|
|
||||||
cfgfile.Write([]byte("\n"))
|
|
||||||
}
|
|
||||||
cfgfile.Write(data)
|
cfgfile.Write(data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,74 +0,0 @@
|
||||||
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"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (f *Forge) backupConfig() error {
|
|
||||||
// make a new dir to backup the files
|
|
||||||
srcDir := filepath.Join(f.configDir)
|
|
||||||
destDir := filepath.Join(f.configDir, "backup")
|
|
||||||
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()
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
|
|
||||||
dest = dest + timestamp
|
|
||||||
destFile, err := os.Create(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer destFile.Close()
|
|
||||||
|
|
||||||
// Copy the content
|
|
||||||
_, err = io.Copy(destFile, srcFile)
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package forgepb
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (all *ForgeConfigs) sampleConfig() {
|
|
||||||
new1 := new(ForgeConfig)
|
|
||||||
new1.GoPath = "go.wit.com"
|
|
||||||
new1.Writable = true
|
|
||||||
new1.Directory = true
|
|
||||||
all.Append(new1)
|
|
||||||
|
|
||||||
fmt.Println("first time user. adding an example config file with", len(all.ForgeConfigs), "repos")
|
|
||||||
}
|
|
101
init.go
101
init.go
|
@ -3,14 +3,10 @@
|
||||||
package forgepb
|
package forgepb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.wit.com/lib/fhelp"
|
"go.wit.com/lib/fhelp"
|
||||||
"go.wit.com/lib/gui/shell"
|
|
||||||
"go.wit.com/lib/protobuf/gitpb"
|
"go.wit.com/lib/protobuf/gitpb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
@ -25,15 +21,10 @@ func Default(opts ...OptionFunc) *Engine {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
func Init() *Forge {
|
func Init() *Forge {
|
||||||
f := InitPB()
|
f := InitPB()
|
||||||
|
|
||||||
/*
|
|
||||||
f.Machine = new(zoopb.Machine)
|
|
||||||
if err := f.Machine.ConfigLoad(); err != nil {
|
|
||||||
log.Log(WARN, "zoopb.ConfigLoad() failed", err)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if f.Config.Username == "" {
|
if f.Config.Username == "" {
|
||||||
usr, _ := user.Current()
|
usr, _ := user.Current()
|
||||||
f.Config.Username = usr.Username
|
f.Config.Username = usr.Username
|
||||||
|
@ -82,24 +73,31 @@ func Init() *Forge {
|
||||||
log.Log(INFO, "update() check took", shell.FormatDuration(time.Since(now)))
|
log.Log(INFO, "update() check took", shell.FormatDuration(time.Since(now)))
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func FirstTimeUser() bool {
|
func InitFromConfig(cfg *ForgeConfigs) *Forge {
|
||||||
if checkenv() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup the env
|
|
||||||
f := new(Forge)
|
f := new(Forge)
|
||||||
f.setenv()
|
f.Config = cfg
|
||||||
|
if f.configENV() {
|
||||||
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), "forge.text")
|
log.Info("ENV changed config. todo: save config here")
|
||||||
_, err := os.ReadFile(fullname)
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
|
f.Repos = gitpb.NewRepos()
|
||||||
|
f.Repos.ConfigLoad()
|
||||||
|
if f.Repos.HasFullScan {
|
||||||
|
f.hasFullScan = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// init the Patchsets
|
||||||
|
f.Patchsets = NewPatchsets()
|
||||||
|
|
||||||
|
// todo: play with these / determine good values based on user's machine
|
||||||
|
f.rillX = 10
|
||||||
|
f.rillY = 20
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (f *Forge) InitPB() {
|
func (f *Forge) InitPB() {
|
||||||
f.setenv()
|
f.setenv()
|
||||||
|
|
||||||
|
@ -122,6 +120,7 @@ func (f *Forge) InitPB() {
|
||||||
f.rillX = 10
|
f.rillX = 10
|
||||||
f.rillY = 20
|
f.rillY = 20
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func (f *Forge) InitMachine() {
|
func (f *Forge) InitMachine() {
|
||||||
if f.Config.Username == "" {
|
if f.Config.Username == "" {
|
||||||
|
@ -132,6 +131,7 @@ func (f *Forge) InitMachine() {
|
||||||
// log.Info(hostname, err)
|
// log.Info(hostname, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// only init's the protobuf. intended to not scan or change anything
|
// only init's the protobuf. intended to not scan or change anything
|
||||||
func InitPB() *Forge {
|
func InitPB() *Forge {
|
||||||
f := new(Forge)
|
f := new(Forge)
|
||||||
|
@ -139,6 +139,7 @@ func InitPB() *Forge {
|
||||||
f.InitPB()
|
f.InitPB()
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func (f *Forge) SetConfigSave(b bool) {
|
func (f *Forge) SetConfigSave(b bool) {
|
||||||
f.configSave = b
|
f.configSave = b
|
||||||
|
@ -154,6 +155,7 @@ func (f *Forge) Exit() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func RawInitPB() *Forge {
|
func RawInitPB() *Forge {
|
||||||
f := new(Forge)
|
f := new(Forge)
|
||||||
f.RawInitPB()
|
f.RawInitPB()
|
||||||
|
@ -163,6 +165,7 @@ func RawInitPB() *Forge {
|
||||||
func (f *Forge) RawInitPB() {
|
func (f *Forge) RawInitPB() {
|
||||||
f.InitPB()
|
f.InitPB()
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// the first thing done is process any ENV settings
|
// the first thing done is process any ENV settings
|
||||||
// try to NOT use the ENV settings anywhere but here
|
// try to NOT use the ENV settings anywhere but here
|
||||||
|
@ -173,15 +176,20 @@ func (f *Forge) setenv() {
|
||||||
log.Info("forge ConfigureENV() failed", err)
|
log.Info("forge ConfigureENV() failed", err)
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
if f.Config == nil {
|
||||||
|
log.Info("forge.Config() was nil")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
f.configDir = os.Getenv("FORGE_CONFIG")
|
f.configDir = os.Getenv("FORGE_CONFIG")
|
||||||
f.goSrc = os.Getenv("FORGE_GOSRC")
|
f.goSrc = os.Getenv("FORGE_GOSRC")
|
||||||
f.repoPB = os.Getenv("FORGE_REPOPB")
|
|
||||||
f.forgeURL = os.Getenv("FORGE_URL")
|
f.forgeURL = os.Getenv("FORGE_URL")
|
||||||
f.patchDir = os.Getenv("FORGE_PATCHDIR")
|
|
||||||
f.hostname = os.Getenv("HOSTNAME")
|
f.hostname = os.Getenv("HOSTNAME")
|
||||||
if os.Getenv("FORGE_GOWORK") == "true" {
|
if os.Getenv("FORGE_GOWORK") == "true" {
|
||||||
f.goWork = true
|
f.goWork = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.Config.ReposPB = os.Getenv("FORGE_REPOPB")
|
||||||
|
f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,3 +232,46 @@ func (f *Forge) SetForgeURL(url string) {
|
||||||
os.Setenv("FORGE_URL", f.forgeURL)
|
os.Setenv("FORGE_URL", f.forgeURL)
|
||||||
log.Info("Forge URL has been set to", f.forgeURL)
|
log.Info("Forge URL has been set to", f.forgeURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the first thing done is process any ENV settings
|
||||||
|
// try to NOT use the ENV settings anywhere but here
|
||||||
|
// all initial ENV settings should be stored in the forge struct
|
||||||
|
func (f *Forge) configENV() bool {
|
||||||
|
var changed bool
|
||||||
|
f.once.Do(func() {
|
||||||
|
if err := fhelp.ConfigureENV(); err != nil {
|
||||||
|
log.Info("forge ConfigureENV() failed", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
if os.Getenv("FORGE_REPOPB") != "" && f.Config.ReposPB != os.Getenv("FORGE_REPOPB") {
|
||||||
|
log.Info("ENV: updating FORGE_REPOSPB from", f.Config.ReposPB, "to", os.Getenv("FORGE_REPOPB"))
|
||||||
|
f.Config.ReposPB = os.Getenv("FORGE_REPOPB")
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("FORGE_GOSRC") != "" && f.Config.ReposDir != os.Getenv("FORGE_GOSRC") {
|
||||||
|
log.Info("ENV: updating FORGE_GOSRC from", f.Config.ReposDir, "to", os.Getenv("FORGE_GOSRC"))
|
||||||
|
f.Config.ReposDir = os.Getenv("FORGE_GOSRC")
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("FORGE_PATCHDIR") != "" && f.Config.PatchDir != os.Getenv("FORGE_PATCHDIR") {
|
||||||
|
log.Info("ENV: updating FORGE_PATCHDIR from", f.Config.PatchDir, "to", os.Getenv("FORGE_PATCHDIRC"))
|
||||||
|
f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR")
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
|
||||||
|
f.configDir = os.Getenv("FORGE_CONFIG")
|
||||||
|
f.goSrc = os.Getenv("FORGE_GOSRC")
|
||||||
|
f.forgeURL = os.Getenv("FORGE_URL")
|
||||||
|
// f.patchDir = os.Getenv("FORGE_PATCHDIR")
|
||||||
|
f.hostname = os.Getenv("HOSTNAME")
|
||||||
|
if os.Getenv("FORGE_GOWORK") == "true" {
|
||||||
|
f.goWork = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if changed {
|
||||||
|
// save config here
|
||||||
|
}
|
||||||
|
return changed
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
func (f *Forge) LoadPatchsets() error {
|
func (f *Forge) LoadPatchsets() error {
|
||||||
f.Patchsets = NewPatchsets()
|
f.Patchsets = NewPatchsets()
|
||||||
|
|
||||||
filename := filepath.Join(f.patchDir, "all-patches.pb")
|
filename := filepath.Join(f.Config.PatchDir, "all-patches.pb")
|
||||||
|
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -45,7 +45,7 @@ func (f *Forge) InitPatchsets() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forge) SavePatchsets() error {
|
func (f *Forge) SavePatchsets() error {
|
||||||
filename := filepath.Join(f.patchDir, "all-patches.pb")
|
filename := filepath.Join(f.Config.PatchDir, "all-patches.pb")
|
||||||
regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("SavePatchsets() filename open error:", filename, err)
|
log.Info("SavePatchsets() filename open error:", filename, err)
|
||||||
|
|
|
@ -13,8 +13,6 @@ type Forge struct {
|
||||||
once sync.Once
|
once sync.Once
|
||||||
initErr error // init error, if any
|
initErr error // init error, if any
|
||||||
goSrc string // the path to go/src
|
goSrc string // the path to go/src
|
||||||
repoPB string // the path to the repos.pb cache file
|
|
||||||
configDir string // normally ~/.config/forge
|
|
||||||
goWork bool // means the user is currently using a go.work file
|
goWork bool // means the user is currently using a go.work file
|
||||||
Config *ForgeConfigs // config repos for readonly, private, etc
|
Config *ForgeConfigs // config repos for readonly, private, etc
|
||||||
Repos *gitpb.Repos // the repo protobufs
|
Repos *gitpb.Repos // the repo protobufs
|
||||||
|
@ -26,7 +24,8 @@ type Forge struct {
|
||||||
rillX int // used for Rill()
|
rillX int // used for Rill()
|
||||||
rillY int // used for Rill()
|
rillY int // used for Rill()
|
||||||
Patchsets *Patchsets // patches that are in progress
|
Patchsets *Patchsets // patches that are in progress
|
||||||
patchDir string // where patches are stored
|
configDir string // normally ~/.config/forge
|
||||||
|
// patchDir string // where patches are stored
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forge) GetGoSrc() string {
|
func (f *Forge) GetGoSrc() string {
|
||||||
|
|
Loading…
Reference in New Issue