lots of Init() cleanups
This commit is contained in:
parent
888442708c
commit
98cc06ab8f
11
config.go
11
config.go
|
@ -19,7 +19,7 @@ import (
|
||||||
func (f *Forge) ConfigSave() error {
|
func (f *Forge) ConfigSave() error {
|
||||||
var err error
|
var err error
|
||||||
// backup the current config files
|
// backup the current config files
|
||||||
if e := backupConfig(); e != nil {
|
if e := f.backupConfig(); e != nil {
|
||||||
log.Info("forge.BackupConfig() error", e)
|
log.Info("forge.BackupConfig() error", e)
|
||||||
err = e
|
err = e
|
||||||
// continue here? notsure. could be bad either way
|
// continue here? notsure. could be bad either way
|
||||||
|
@ -45,7 +45,7 @@ func (f *Forge) ConfigSave() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to ~/.config/forge/ unless ENV{FORGE_CONFIG} is set
|
// write to ~/.config/forge/
|
||||||
func (f *ForgeConfigs) ConfigSave() error {
|
func (f *ForgeConfigs) ConfigSave() error {
|
||||||
data, err := f.Marshal()
|
data, err := f.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -64,12 +64,7 @@ func (f *ForgeConfigs) ConfigSave() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the ~/.config/forge/ files
|
// load the ~/.config/forge/ files
|
||||||
func (c *ForgeConfigs) ConfigLoad() error {
|
func (c *ForgeConfigs) ConfigLoad(fullpath string) error {
|
||||||
if os.Getenv("FORGE_CONFIG") == "" {
|
|
||||||
homeDir, _ := os.UserHomeDir()
|
|
||||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
|
||||||
os.Setenv("FORGE_CONFIG", fullpath)
|
|
||||||
}
|
|
||||||
// var data []byte
|
// var data []byte
|
||||||
// var err error
|
// var err error
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
|
|
@ -12,10 +12,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func backupConfig() error {
|
func (f *Forge) backupConfig() error {
|
||||||
// make a new dir to backup the files
|
// make a new dir to backup the files
|
||||||
srcDir := filepath.Join(os.Getenv("FORGE_CONFIG"))
|
srcDir := filepath.Join(f.configDir)
|
||||||
destDir := filepath.Join(os.Getenv("FORGE_CONFIG"), "backup")
|
destDir := filepath.Join(f.configDir, "backup")
|
||||||
return backupFiles(srcDir, destDir)
|
return backupFiles(srcDir, destDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -330,7 +330,7 @@ func (psets *Patchsets) PrintTable() {
|
||||||
log.DaemonMode(true)
|
log.DaemonMode(true)
|
||||||
|
|
||||||
// print the header
|
// print the header
|
||||||
args := []string{"commit hash", "what", "age", "cId", "partId", "", "", "", "", ""}
|
args := []string{"commit hash", "", "", "name", "Repo Namespace", "", "", "", "", ""}
|
||||||
sizes := []int{36, 3, 3, 40, 80, 2, 2, 2, 2, 2}
|
sizes := []int{36, 3, 3, 40, 80, 2, 2, 2, 2, 2}
|
||||||
log.Info(cobol.StandardTableSize10(sizes, args))
|
log.Info(cobol.StandardTableSize10(sizes, args))
|
||||||
|
|
||||||
|
|
158
init.go
158
init.go
|
@ -5,9 +5,9 @@ package forgepb
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.wit.com/lib/fhelp"
|
||||||
"go.wit.com/lib/gui/shell"
|
"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"
|
||||||
|
@ -84,87 +84,26 @@ func Init() *Forge {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetermineGoPath() *Forge {
|
|
||||||
f := new(Forge)
|
|
||||||
|
|
||||||
// TODO: rethink this but it works for now
|
|
||||||
gosrc := os.Getenv("FORGE_GOSRC")
|
|
||||||
if gosrc == "" {
|
|
||||||
goSrcDir, err := f.findGoSrc()
|
|
||||||
if err != nil {
|
|
||||||
log.Log(WARN, "forge init() findGoSrc()", err)
|
|
||||||
}
|
|
||||||
os.Setenv("FORGE_GOSRC", goSrcDir)
|
|
||||||
}
|
|
||||||
f.goSrc = os.Getenv("FORGE_GOSRC")
|
|
||||||
|
|
||||||
// also rethink this, but maybe this is the right thing to do
|
|
||||||
if os.Getenv("FORGE_CONFIG") == "" {
|
|
||||||
homeDir, _ := os.UserHomeDir()
|
|
||||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
|
||||||
os.Setenv("FORGE_CONFIG", fullpath)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.configDir = os.Getenv("FORGE_CONFIG")
|
|
||||||
|
|
||||||
// check again for go.work // user could have a go.work file in ~/go/src
|
|
||||||
if f.goWorkExists() {
|
|
||||||
f.goWork = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// print out the settings that will be used
|
|
||||||
log.Log(INFO, "forgepb.Init() FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
|
|
||||||
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forge) InitPB() {
|
func (f *Forge) InitPB() {
|
||||||
|
f.setenv()
|
||||||
|
|
||||||
// load the ~/.config/forge/ config
|
// load the ~/.config/forge/ config
|
||||||
f.Config = new(ForgeConfigs)
|
f.Config = new(ForgeConfigs)
|
||||||
if err := f.Config.ConfigLoad(); err != nil {
|
if err := f.Config.ConfigLoad(f.configDir); err != nil {
|
||||||
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
|
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.IsGoWork() {
|
|
||||||
log.Log(INFO, "forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = true)")
|
|
||||||
} else {
|
|
||||||
log.Log(INFO, "forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = false)")
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Repos = gitpb.NewRepos()
|
f.Repos = gitpb.NewRepos()
|
||||||
f.Repos.ConfigLoad()
|
f.Repos.ConfigLoad()
|
||||||
if f.Repos.HasFullScan {
|
if f.Repos.HasFullScan {
|
||||||
f.hasFullScan = true
|
f.hasFullScan = true
|
||||||
}
|
}
|
||||||
|
|
||||||
f.forgeURL = "https://forge.wit.com/"
|
|
||||||
|
|
||||||
if os.Getenv("FORGE_URL") != "" {
|
|
||||||
f.forgeURL = os.Getenv("FORGE_URL")
|
|
||||||
log.Info("got forge url", f.forgeURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: play with these / determine good values based on user's machine
|
// todo: play with these / determine good values based on user's machine
|
||||||
f.rillX = 10
|
f.rillX = 10
|
||||||
f.rillY = 20
|
f.rillY = 20
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the URL for forge otherwise fallback to ENV or to forge.wit.com
|
|
||||||
func (f *Forge) SetForgeURL(url string) {
|
|
||||||
if url == "" {
|
|
||||||
url = os.Getenv("FORGE_URL")
|
|
||||||
}
|
|
||||||
if url == "" {
|
|
||||||
url = "https://forge.wit.com/"
|
|
||||||
}
|
|
||||||
f.forgeURL = url
|
|
||||||
log.Info("Forge URL has been set to", f.forgeURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forge) GetForgeURL() string {
|
|
||||||
return f.forgeURL
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forge) InitMachine() {
|
func (f *Forge) InitMachine() {
|
||||||
if f.Config.Username == "" {
|
if f.Config.Username == "" {
|
||||||
usr, _ := user.Current()
|
usr, _ := user.Current()
|
||||||
|
@ -176,7 +115,8 @@ func (f *Forge) InitMachine() {
|
||||||
|
|
||||||
// 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 := DetermineGoPath()
|
f := new(Forge)
|
||||||
|
f.setenv()
|
||||||
f.InitPB()
|
f.InitPB()
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
@ -197,55 +137,65 @@ func (f *Forge) Exit() {
|
||||||
|
|
||||||
func RawInitPB() *Forge {
|
func RawInitPB() *Forge {
|
||||||
f := new(Forge)
|
f := new(Forge)
|
||||||
|
f.RawInitPB()
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
homeDir, _ := os.UserHomeDir()
|
func (f *Forge) RawInitPB() {
|
||||||
// todo: check or exit if err?
|
f.setenv()
|
||||||
|
|
||||||
// TODO: rethink this but it works for now
|
|
||||||
if os.Getenv("FORGE_GOSRC") == "" {
|
|
||||||
fullpath := filepath.Join(homeDir, ".cache/forge")
|
|
||||||
err := os.MkdirAll(fullpath, os.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
log.Log(WARN, "mkdir failed", fullpath, err)
|
|
||||||
}
|
|
||||||
os.Setenv("FORGE_GOSRC", fullpath)
|
|
||||||
}
|
|
||||||
f.goSrc = os.Getenv("FORGE_GOSRC")
|
|
||||||
|
|
||||||
// also rethink this, but maybe this is the right thing to do
|
|
||||||
if os.Getenv("FORGE_CONFIG") == "" {
|
|
||||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
|
||||||
os.MkdirAll(fullpath, os.ModePerm)
|
|
||||||
os.Setenv("FORGE_CONFIG", fullpath)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.configDir = os.Getenv("FORGE_CONFIG")
|
|
||||||
|
|
||||||
// load the ~/.config/forge/ config
|
// load the ~/.config/forge/ config
|
||||||
f.Config = new(ForgeConfigs)
|
f.Config = new(ForgeConfigs)
|
||||||
if err := f.Config.ConfigLoad(); err != nil {
|
if err := f.Config.ConfigLoad(f.configDir); err != nil {
|
||||||
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
|
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Repos = gitpb.NewRepos()
|
f.Repos = gitpb.NewRepos()
|
||||||
f.Repos.ConfigLoad()
|
f.Repos.ConfigLoad()
|
||||||
|
|
||||||
f.forgeURL = "https://forge.wit.com/"
|
|
||||||
|
|
||||||
if os.Getenv("FORGE_URL") != "" {
|
|
||||||
f.forgeURL = os.Getenv("FORGE_URL")
|
|
||||||
log.Info("got forge url", f.forgeURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
// where patches are stored
|
|
||||||
f.patchDir = f.goSrc
|
|
||||||
if os.Getenv("FORGE_PATCHDIR") != "" {
|
|
||||||
f.patchDir = os.Getenv("FORGE_PATCHDIR")
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: play with these / determine good values based on user's machine
|
// todo: play with these / determine good values based on user's machine
|
||||||
f.rillX = 10
|
f.rillX = 10
|
||||||
f.rillY = 20
|
f.rillY = 20
|
||||||
|
}
|
||||||
return f
|
|
||||||
|
// 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) setenv() {
|
||||||
|
if err := fhelp.ConfigureENV(); err != nil {
|
||||||
|
log.Info("forge ConfigureENV() failed", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
f.configDir = os.Getenv("FORGE_CONFIG")
|
||||||
|
f.goSrc = os.Getenv("FORGE_GOSRC")
|
||||||
|
f.repoPB = os.Getenv("FORGE_REPOPB")
|
||||||
|
f.forgeURL = os.Getenv("FORGE_URL")
|
||||||
|
f.patchDir = os.Getenv("FORGE_PATCHDIR")
|
||||||
|
if os.Getenv("FORGE_GOWORK") == "true" {
|
||||||
|
f.goWork = true
|
||||||
|
}
|
||||||
|
log.Printf("FORGE_CONFIG = %s\n", f.configDir)
|
||||||
|
log.Printf("FORGE_GOSRC = %s\n", f.goSrc)
|
||||||
|
log.Printf("FORGE_GOWORK = %v\n", f.goWork)
|
||||||
|
log.Printf("FORGE_REPOPB = %s\n", f.repoPB)
|
||||||
|
log.Printf("FORGE_URL = %s\n", f.forgeURL)
|
||||||
|
log.Printf("FORGE_PATCHDIR = %s\n", f.patchDir)
|
||||||
|
log.Printf("HOSTNAME = %s\n", os.Getenv("HOSTNAME"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Forge) GetForgeURL() string {
|
||||||
|
return f.forgeURL
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the URL for forge otherwise fallback to ENV or to forge.wit.com
|
||||||
|
func (f *Forge) SetForgeURL(url string) {
|
||||||
|
if url == "" {
|
||||||
|
url = os.Getenv("FORGE_URL")
|
||||||
|
}
|
||||||
|
if url == "" {
|
||||||
|
url = "https://forge.wit.com/"
|
||||||
|
}
|
||||||
|
f.forgeURL = url
|
||||||
|
os.Setenv("FORGE_URL", f.forgeURL)
|
||||||
|
log.Info("Forge URL has been set to", f.forgeURL)
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,15 @@ func (f *Forge) loadUpstreamPatchsets(psets *Patchsets) {
|
||||||
log.Info("new patchset", pset.Name, pset.Uuid)
|
log.Info("new patchset", pset.Name, pset.Uuid)
|
||||||
pset.State = "new"
|
pset.State = "new"
|
||||||
foundnew = true
|
foundnew = true
|
||||||
|
if pset == nil {
|
||||||
|
log.Warn("OH NO! pset == nil")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if f.Patchsets == nil {
|
||||||
|
log.Warn("OH NO! f.Patchsets == nil")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
f.Patchsets.Append(pset)
|
||||||
} else {
|
} else {
|
||||||
log.Info("patchset already on disk", found.Name, found.State)
|
log.Info("patchset already on disk", found.Name, found.State)
|
||||||
pset.State = found.State
|
pset.State = found.State
|
||||||
|
@ -53,7 +62,6 @@ func (f *Forge) loadUpstreamPatchsets(psets *Patchsets) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if foundnew {
|
if foundnew {
|
||||||
log.Info("should save these here")
|
|
||||||
f.SavePatchsets()
|
f.SavePatchsets()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (f *Forge) LoadPatchsets() error {
|
||||||
log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
|
log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("LoadPatchsets() worked ok %d\n", f.Patchsets.Len())
|
log.Infof("LoadPatchsets() found %d patches.\n", f.Patchsets.Len())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ func (f *Forge) SavePatchsets() error {
|
||||||
log.Infof("SavePatchset() proto.Marshal() error %v\n", err)
|
log.Infof("SavePatchset() proto.Marshal() error %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("SavePatchset() worked (%d) bytes\n", len(data))
|
log.Infof("SavePatchset() worked (%d) bytes on %d patches\n", len(data), f.Patchsets.Len())
|
||||||
regfile.Write(data)
|
regfile.Write(data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ message Patchset { // `autogenpb:mars
|
||||||
string hostname = 14; //
|
string hostname = 14; //
|
||||||
}
|
}
|
||||||
|
|
||||||
message Patchsets { // `autogenpb:marshal` `autogenpb:gui`
|
message Patchsets { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
|
||||||
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
|
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
|
||||||
string version = 2; // `autogenpb:version:v0.0.45`
|
string version = 2; // `autogenpb:version:v0.0.45`
|
||||||
repeated Patchset Patchsets = 3;
|
repeated Patchset Patchsets = 3;
|
||||||
|
|
|
@ -13,6 +13,7 @@ type Forge struct {
|
||||||
initOnce sync.Once
|
initOnce 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
|
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
|
||||||
|
|
Loading…
Reference in New Issue