use Repos.ConfigLoad() & ConfigSave()

This commit is contained in:
Jeff Carr 2024-12-01 10:44:29 -06:00
parent 4e47dea41e
commit d132639049
4 changed files with 93 additions and 2 deletions

82
build.go Normal file
View File

@ -0,0 +1,82 @@
package forgepb
// for golang repos, this is an attempt to build the package
// there might be some 'standard' ways to implement a build
// that make sense.
// Additions to 'go build' that are attempted here:
//
// * detect packages that are plugins
// * autogen packages that have .proto protobuf files
// * define some 'standard' ldflags
//
import (
"errors"
"fmt"
"os"
"time"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
// always assume all sources have been downloaded
// todo: detect when in ~/go/src vs go.work mode
os.Setenv("GO111MODULE", "off")
// get the version
version := repo.GetCurrentBranchVersion()
loop := repo.Tags.SortByRefname()
for loop.Scan() {
t := loop.Next()
log.Info("Build() tag:", t.Refname)
}
if repo.GoDeps == nil {
repo.RedoGoMod()
}
if repo.GoDeps.Len() == 0 {
log.Info("redo go.mod", repo.GetGoPath())
repo.RedoGoMod()
f.Repos.ConfigSave()
}
loop1 := repo.GoDeps.SortByGoPath()
for loop1.Scan() {
t := loop1.Next()
log.Info("Build() dep:", t.GetGoPath(), t.GetVersion())
}
log.Info("Build() dep len:", repo.GoDeps.Len())
os.Exit(-1)
if repo.CheckDirty() {
version = version + "-dirty"
}
cmd := []string{"go", "build", "-v"}
// set standard ldflag options
now := time.Now()
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
log.Info("datestamp =", datestamp)
// add some standard golang flags
ldflags := "-X main.VERSION=" + version + " "
ldflags += "-X main.BUILDTIME=" + datestamp + " "
ldflags += "-X main.GUIVERSION=" + version + "" // todo: git this from the filesystem
cmd = append(cmd, "-ldflags", ldflags)
// add any flags from the command line
// this might not actually work
// todo: test this
for _, flag := range userFlags {
cmd = append(cmd, "-ldflags", "-X "+flag)
}
if r := repo.Run(cmd); r.Error == nil {
log.Warn("go build worked")
return nil
} else {
return errors.New("go build failed: " + fmt.Sprint(r.Error))
}
}

View File

@ -13,6 +13,8 @@ import (
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set // write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
func (f *Forge) ConfigSave() error { func (f *Forge) ConfigSave() error {
// f.Config.Lock()
// defer f.Config.UnLock()
if os.Getenv("FORGE_HOME") == "" { if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge") fullpath := filepath.Join(homeDir, ".config/forge")
@ -27,7 +29,7 @@ func (f *Forge) ConfigSave() error {
log.Info("proto.Marshal() failed len", len(data), err) log.Info("proto.Marshal() failed len", len(data), err)
return err return err
} }
log.Info("proto.Marshal() worked len", len(data)) log.Info("forgepb.ConfigSave() proto.Marshal() worked len", len(data))
configWrite("forge.pb", data) configWrite("forge.pb", data)
s := f.Config.FormatTEXT() s := f.Config.FormatTEXT()
@ -35,6 +37,10 @@ func (f *Forge) ConfigSave() error {
s = f.Config.FormatJSON() s = f.Config.FormatJSON()
configWrite("forge.json", []byte(s)) configWrite("forge.json", []byte(s))
if f.Repos != nil {
f.Repos.ConfigSave()
}
return nil return nil
} }

View File

@ -37,6 +37,7 @@ func Init() *Forge {
} }
f.Repos = new(gitpb.Repos) f.Repos = new(gitpb.Repos)
f.Repos.ConfigLoad()
f.Machine = new(zoopb.Machine) f.Machine = new(zoopb.Machine)
if err := f.Machine.ConfigLoad(); err != nil { if err := f.Machine.ConfigLoad(); err != nil {

View File

@ -40,6 +40,8 @@ func (f *Forge) NewGoPath(gopath string) (*gitpb.Repo, error) {
} else { } else {
newr.SetUserBranchName(uname + "FIXME") newr.SetUserBranchName(uname + "FIXME")
} }
f.Repos.ConfigSave()
panic("forgepb got here")
return newr, err // return newr, err
} }