added Init() and a Forge struct

This commit is contained in:
Jeff Carr 2024-11-28 00:02:27 -06:00
parent 54a93cd5eb
commit 0401b949c6
6 changed files with 84 additions and 41 deletions

View File

@ -12,7 +12,7 @@ import (
)
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
func (m *ForgeConfigs) ConfigSave() error {
func (f *Forge) ConfigSave() error {
if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
@ -22,7 +22,7 @@ func (m *ForgeConfigs) ConfigSave() error {
if err := backupConfig(); err != nil {
return err
}
data, err := m.Marshal()
data, err := f.Config.Marshal()
if err != nil {
log.Info("proto.Marshal() failed len", len(data), err)
return err
@ -30,10 +30,10 @@ func (m *ForgeConfigs) ConfigSave() error {
log.Info("proto.Marshal() worked len", len(data))
configWrite("forge.pb", data)
s := m.FormatTEXT()
s := f.Config.FormatTEXT()
configWrite("forge.text", []byte(s))
s = m.FormatJSON()
s = f.Config.FormatJSON()
configWrite("forge.json", []byte(s))
return nil
}

View File

@ -11,37 +11,34 @@ import (
var VERSION string
func main() {
var repos forgepb.ForgeConfigs
if err := repos.ConfigLoad(); err != nil {
log.Warn("forgepb.ConfigLoad() failed", err)
os.Exit(-1)
}
var f forgepb.Forge
f.Init()
if argv.List {
repos.PrintTable()
loop := repos.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Next()
log.Info("repo:", r.GoPath)
}
f.PrintTable()
loop := f.SortByPath() // get the list of forge configs
for loop.Scan() {
r := loop.Next()
log.Info("repo:", r.GoPath)
}
os.Exit(0)
}
// try to delete, then save config and exit
if argv.Delete {
if oldr := repos.DeleteByPath(argv.GoPath); oldr == nil {
if oldr := f.Config.DeleteByPath(argv.GoPath); oldr == nil {
log.Info("deleted", argv.GoPath, "did not exist. did nothing")
os.Exit(0)
}
log.Info("deleted", argv.GoPath, "ok")
repos.ConfigSave()
f.ConfigSave()
os.Exit(0)
}
// try to update, then save config and exit
if argv.Update {
/*
if repos.UpdateGoPath(argv.Name, argv.GoPath) {
if f.UpdateGoPath(argv.Name, argv.GoPath) {
// save updated config file
repos.ConfigSave()
}
@ -62,18 +59,18 @@ func main() {
Interesting: argv.Interesting,
}
if repos.Append(&new1) {
if f.Config.Append(&new1) {
log.Info("added", new1.GoPath, "ok")
} else {
log.Info("added", new1.GoPath, "failed")
os.Exit(-1)
}
repos.ConfigSave()
f.ConfigSave()
os.Exit(0)
}
// testMemoryCorruption(repos)
repos.ConfigSave()
// testMemoryCorruption(f)
f.ConfigSave()
}
/*

View File

@ -2,7 +2,6 @@ package forgepb
import (
"fmt"
"os"
"go.wit.com/log"
)
@ -18,16 +17,16 @@ func standardHeader() string {
return fmt.Sprintf("%-4s %-40s %s", "", "Path", "flags")
}
func (all *ForgeConfigs) standardHeader(r *ForgeConfig) string {
func (f *Forge) standardHeader(r *ForgeConfig) string {
var flags string
var readonly string
if all.IsPrivate(r.GoPath) {
if f.IsPrivate(r.GoPath) {
flags += "(private) "
}
if all.IsFavorite(r.GoPath) {
if f.IsFavorite(r.GoPath) {
flags += "(favorite) "
}
if all.IsReadOnly(r.GoPath) {
if f.IsReadOnly(r.GoPath) {
readonly = ""
} else {
readonly = "r/w"
@ -36,15 +35,15 @@ func (all *ForgeConfigs) standardHeader(r *ForgeConfig) string {
}
// print a human readable table to STDOUT
func (all *ForgeConfigs) PrintTable() {
if all == nil {
log.Info("WTF")
os.Exit(0)
func (f *Forge) ConfigPrintTable() {
if f == nil {
log.Info("WTF forge == nil")
panic("WTF forge == nil")
}
log.Info(standardHeader())
loop := all.SortByPath()
loop := f.Config.SortByPath()
for loop.Scan() {
r := loop.Next()
log.Info(all.standardHeader(r))
log.Info(f.standardHeader(r))
}
}

41
init.go Normal file
View File

@ -0,0 +1,41 @@
package forgepb
import (
"os"
"path/filepath"
"go.wit.com/log"
)
// set FORGE_GOSRC env if not already set
func init() {
gosrc := os.Getenv("FORGE_GOSRC")
if gosrc != "" {
// already set. ignore init()
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Warn("forge init() could not find UserHomeDir()", err)
panic("forge could not find UserHomeDir")
}
fullpath := filepath.Join(homeDir, "go/src")
os.Setenv("FORGE_GOSRC", fullpath)
}
func (f *Forge) Init() {
if f == nil {
f = new(Forge)
}
if f.Config == nil {
f.Config = new(ForgeConfigs)
}
// load the ~/.config/forge/ config
if err := f.Config.ConfigLoad(); err != nil {
log.Warn("forgepb.ConfigLoad() failed", err)
os.Exit(-1)
}
}
func (f *Forge) SortByPath() *ForgeConfigIterator {
return f.Config.SortByPath()
}

View File

@ -28,10 +28,10 @@ func (all *ForgeConfigs) UpdateGoPath(name string, gopath string) bool {
// returns true if gopath is readonly()
// will attempt to match IsWritable("foo") against anything ending in "foo"
func (all *ForgeConfigs) IsReadOnly(gopath string) bool {
func (f *Forge) IsReadOnly(gopath string) bool {
var match *ForgeConfig
loop := all.SortByPath() // get the list of repos
loop := f.Config.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Next()
if r.GoPath == gopath {
@ -88,11 +88,11 @@ func (all *ForgeConfigs) IsReadOnly(gopath string) bool {
// this let's you check a git tag version against a package .deb version
// allows gopath's to not need to match the .deb name
// this is important in lots of cases! It is normal and happens often enough.
func (all *ForgeConfigs) DebName(gopath string) string {
func (f *Forge) DebName(gopath string) string {
// get "zookeeper" from "go.wit.com/apps/zookeeper"
normalBase := filepath.Base(gopath)
loop := all.SortByPath()
loop := f.Config.SortByPath()
for loop.Scan() {
r := loop.Next()
if r.GoPath == gopath {
@ -115,13 +115,13 @@ func (all *ForgeConfigs) DebName(gopath string) string {
//
// IsPrivate("go.foo.com/jcarr/foo") returns true if private
// IsPrivate("foo") also returns true if "go.bar.com/jcarr/foo" is private
func (all *ForgeConfigs) IsPrivate(thing string) bool {
func (f *Forge) IsPrivate(thing string) bool {
var match *ForgeConfig
// sort by path means the simple 'match' logic
// here works in the sense the last directory match
// is the one that is used
loop := all.SortByPath() // get the list of repos
loop := f.Config.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Next()
if r.GoPath == thing {
@ -159,10 +159,10 @@ func (all *ForgeConfigs) IsPrivate(thing string) bool {
// file that lets you set things as favorites
// so you can just go-clone a bunch of common things
// on a new box or after you reset/delete your ~/go/src dir
func (all *ForgeConfigs) IsFavorite(thing string) bool {
func (f *Forge) IsFavorite(thing string) bool {
var match *ForgeConfig
loop := all.SortByPath() // get the list of repos
loop := f.Config.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Next()
if r.GoPath == thing {

6
structs.go Normal file
View File

@ -0,0 +1,6 @@
package forgepb
// maybe an interface someday?
type Forge struct {
Config *ForgeConfigs
}