forged init

This commit is contained in:
Jeff Carr 2025-07-09 19:46:16 -05:00
parent 3d3a07c194
commit 0d9fe58e57
5 changed files with 84 additions and 4 deletions

View File

@ -5,8 +5,8 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
all: build
# ./forged pull
FORGE_GOSRC=/home/forge ./forged list
# ./forged list
./forged init
build: goimports
GO111MODULE=off go build \

View File

@ -14,6 +14,7 @@ var argv args
type args struct {
Pull *EmptyCmd `arg:"subcommand:pull" help:"list the repos"`
List *EmptyCmd `arg:"subcommand:list" help:"list the repos"`
Init *EmptyCmd `arg:"subcommand:init" help:"list the repos"`
Port int `arg:"--port" default:"2520" help:"port to run on"`
Hostname string `arg:"--hostname" default:"forge.wit.com" help:"hostname to use"`
}

View File

@ -3,6 +3,7 @@ package main
import (
"os"
"path/filepath"
"strings"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
@ -74,11 +75,73 @@ func tryGitClone(repo *gitpb.Repo, dir string) error {
return err
}
func doList() {
func initForged() *forgepb.Forge {
log.Info("do pull here")
dirs, err := scanForgedDir(FORGEDIR)
if err != nil {
badExit(err)
return nil
}
os.Chdir(FORGEDIR)
forge := forgepb.InitPB()
log.Printf("forged has %d repos\n", forge.Repos.Len())
// forge.PrintHumanTable(forge.Repos)
for _, dir := range dirs {
oldr, err := readGitPB(dir)
if err != nil {
log.Info("readGitPB() failed", dir, err)
continue
}
fullpath := filepath.Join(dir, "git.clone")
if check := forge.Repos.FindByFullPath(fullpath); check != nil {
// log.Info(oldr.Namespace, fullpath, "already added")
continue
}
// check to see if 'git clone' has already been run
_, err = os.Stat(fullpath)
if os.IsNotExist(err) {
log.Info("repo needs cloning:", oldr.Namespace, oldr.FullPath, dir)
} else {
log.Info("repo is already cloned", dir, oldr.Namespace)
}
}
return forge
}
func verifyForged(f *forgepb.Forge) {
var changed bool = false
for repo := range f.Repos.IterAll() {
if strings.HasPrefix(repo.GetFullPath(), os.Getenv("FORGE_GOSRC")) {
// log.Info(os.Getenv("FORGE_GOSRC"), repo.GetFullPath())
} else {
log.Info("not here", os.Getenv("FORGE_GOSRC"), repo.GetFullPath())
f.Repos.Delete(repo)
changed = true
}
}
if changed {
f.Repos.ConfigSave()
}
}
func doInit() *forgepb.Forge {
forge := initForged()
verifyForged(forge)
return forge
}
func doList() any {
log.Info("do pull here")
dirs, err := scanForgedDir(FORGEDIR)
if err != nil {
badExit(err)
return err
}
os.Chdir(FORGEDIR)
forge := forgepb.InitPB()
@ -133,4 +196,5 @@ func doList() {
forge.PrintHumanTable(forge.Repos)
forge.Repos.ConfigSave()
okExit("")
return nil
}

View File

@ -9,6 +9,10 @@ import (
"go.wit.com/log"
)
func exit(thing any) {
os.Exit(0)
}
func okExit(thing string) {
if thing != "" {
log.Info("forge exit:", thing, "ok")

15
main.go
View File

@ -4,6 +4,7 @@ import (
"embed"
"fmt"
"net/http"
"os"
"time"
"go.wit.com/log"
@ -26,11 +27,21 @@ func main() {
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
// the default forged dir is /home/forge
if os.Getenv("FORGE_GOSRC") == "" {
os.Setenv("FORGE_GOSRC", "/home/forge")
}
if argv.List != nil {
doList()
if argv.Init != nil {
doInit()
okExit("")
}
if argv.List != nil {
exit(doList())
okExit("")
}
// forge = forgepb.Init()
if argv.Pull != nil {
doPull()