forgeConfig updates

This commit is contained in:
Jeff Carr 2024-11-20 12:11:13 -06:00
parent e10696e119
commit e14bc69169
5 changed files with 120 additions and 2 deletions

View File

@ -40,6 +40,11 @@ func (m *Repos) ConfigSave() error {
}
func (m *Repos) ConfigLoad() error {
if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_HOME", fullpath)
}
var data []byte
var err error
if m == nil {
@ -50,7 +55,6 @@ func (m *Repos) ConfigLoad() error {
// something went wrong loading the file
return err
}
if data != nil {
// this means the forge.pb file exists and was read
if len(data) == 0 {
@ -61,6 +65,7 @@ func (m *Repos) ConfigLoad() error {
log.Warn("broken forge.pb config file")
return err
}
log.Info("config load found", len(m.Repos), "repos")
return nil
}
@ -80,6 +85,27 @@ func (m *Repos) ConfigLoad() error {
log.Warn("broken forge.text config file")
return err
}
log.Info("config load found", len(m.Repos), "repos")
return nil
}
// forge.text doesn't exist. try forge.json
// this lets the user hand edit the config
if data, err = loadFile("forge.json"); err != nil {
// something went wrong loading the file
return err
}
if data != nil {
// this means the forge.text file exists and was read
if len(data) == 0 {
// todo: error out if the file is empty?
}
if err = m.UnmarshalJSON(data); err != nil {
log.Warn("broken forge.json config file")
return err
}
log.Info("config load found", len(m.Repos), "repos")
return nil
}

View File

@ -1,8 +1,18 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
build:
GO111MODULE=off go build
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
./forgeConfig
FORGE_HOME=/tmp/forge ./forgeConfig
list:
./forgeConfig --list
add:
./forgeConfig --add --name 'foo' --gopath 'go.wit.com/apps/foo'
goimports:
goimports -w *.go

43
forgeConfig/argv.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"os"
"github.com/alexflint/go-arg"
)
var argv args
type args struct {
ConfigDir string `arg:"env:FORGE_HOME" help:"defaults to ~/.config/forge/"`
List bool `arg:"--list" default:"false" help:"list repos in your config"`
Add bool `arg:"--add" default:"false" help:"add a new repo"`
Name string `arg:"--name" help:"name of the repo"`
GoPath string `arg:"--gopath" help:"gopath of the repo"`
}
func (a args) Description() string {
return `
forgeConfig -- add entries to your config files
This is just example protobuf code to test forgepb is working
but it could be used to automagically create a config file too.
If you need to change your config file, just edit the forge.text or forge.json
files then remove the forge.pb and ConfigLoad() will attempt to load those files instead
`
}
func (args) Version() string {
return "virtigo " + VERSION
}
func init() {
var pp *arg.Parser
pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
}

View File

@ -8,6 +8,9 @@ import (
"go.wit.com/log"
)
// sent via ldflags
var VERSION string
func main() {
var repos *forgepb.Repos
repos = new(forgepb.Repos)
@ -15,6 +18,30 @@ func main() {
log.Warn("forgepb.ConfigLoad() failed", err)
os.Exit(-1)
}
if argv.List {
log.Info(forgepb.RepoHeader())
loop := repos.SortByName() // get the list of droplets
for loop.Scan() {
r := loop.Repo()
log.Info("repo:", r.Name, r.Gopath)
}
os.Exit(0)
}
if argv.Add {
log.Info("going to add a new repo", argv.Name, argv.GoPath)
new1 := new(forgepb.Repo)
new1.Name = argv.Name
new1.Gopath = argv.GoPath
if repos.Append(new1) {
log.Info("added", new1.Name, "ok")
} else {
log.Info("added", new1.Name, "failed")
os.Exit(-1)
}
repos.ConfigSave()
os.Exit(0)
}
testAddRepos(repos)
repos.ConfigSave()
}

12
human.go Normal file
View File

@ -0,0 +1,12 @@
package forgepb
// mostly just functions related to making STDOUT
// more readable by us humans
// also function shortcuts the do fixed limited formatting (it's like COBOL)
// so reporting tables of the status of what droplets and hypervisors
// are in text columns and rows that can be easily read in a terminal
func RepoHeader() string {
return "Name Path"
}