summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile9
-rw-r--r--argv.go8
-rw-r--r--doPatchsets.go33
-rw-r--r--exit.go22
-rw-r--r--forgeDir.go76
-rw-r--r--main.go50
6 files changed, 153 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index a40c4b3..127e5aa 100644
--- a/Makefile
+++ b/Makefile
@@ -4,14 +4,7 @@ VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
all: build
- @echo "make build # build"
- @echo "make clean # clean build files"
- @echo "make run # build and run on port 2233"
- @echo "make debian # will build a debian package"
- @echo ""
- @echo "make restart # restart the daemon"
- @echo "make enable # enable daemon on boot"
- @echo "make log # watch the daemon log"
+ ./forged pull
build: goimports
GO111MODULE=off go build \
diff --git a/argv.go b/argv.go
index ab69ec4..573212b 100644
--- a/argv.go
+++ b/argv.go
@@ -12,8 +12,12 @@ import (
var argv args
type args struct {
- Port int `arg:"--port" default:"2520" help:"port to run on"`
- Hostname string `arg:"--hostname" default:"forge.wit.com" help:"hostname to use"`
+ Pull *EmptyCmd `arg:"subcommand:pull" 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"`
+}
+
+type EmptyCmd struct {
}
func (args) Version() string {
diff --git a/doPatchsets.go b/doPatchsets.go
index 90d11c6..7a22c0a 100644
--- a/doPatchsets.go
+++ b/doPatchsets.go
@@ -206,39 +206,6 @@ func lookupRepos(msg []byte) (*gitpb.Repos, error) {
return newpb, nil
}
-/* save the git.pb files
-+ if repo.Namespace == "" {
-+ log.Infof("repo: ERROR. no namespace. %s\n", repo.FullPath)
-+ continue
-+ }
-+
-+ repopath := filepath.Join("/home/forge", repo.Namespace)
-+ if !shell.IsDir(repopath) {
-+ if err := os.MkdirAll(repopath, os.ModePerm); err != nil {
-+ log.Warn("mkdir fail", repo.Namespace, err)
-+ continue
-+ }
-+ }
-+
-+ filename := filepath.Join(repopath, "git.pb")
-+ if !shell.Exists(filename) {
-+ data, err := repo.Marshal()
-+ if err != nil {
-+ log.Info("patchsets.Marshal() to wire failed", err)
-+ continue
-+ }
-+ gitpbf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
-+ defer gitpbf.Close()
-+ if err != nil {
-+ log.Info("filename open error:", filename, err)
-+ continue
-+ }
-+ gitpbf.Write(data)
-+ }
-+
-+ log.Infof("repo path: %s\n", repopath)
-*/
-
func updateRepos(msg []byte) (*gitpb.Repos, error) {
log.Info("proto.Unmarshal() try message len", len(msg))
m := gitpb.NewRepos()
diff --git a/exit.go b/exit.go
new file mode 100644
index 0000000..dce662b
--- /dev/null
+++ b/exit.go
@@ -0,0 +1,22 @@
+// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
+// Use of this source code is governed by the GPL 3.0
+
+package main
+
+import (
+ "os"
+
+ "go.wit.com/log"
+)
+
+func okExit(thing string) {
+ if thing != "" {
+ log.Info("forge exit:", thing, "ok")
+ }
+ os.Exit(0)
+}
+
+func badExit(err error) {
+ log.Info("forged failed: ", err)
+ os.Exit(-1)
+}
diff --git a/forgeDir.go b/forgeDir.go
new file mode 100644
index 0000000..c11554b
--- /dev/null
+++ b/forgeDir.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "go.wit.com/lib/protobuf/gitpb"
+ "go.wit.com/log"
+)
+
+func readGitPB(fullpath string) (*gitpb.Repo, error) {
+ // fmt.Fprintln(w, "repo:", repo.FullPath, repo.Namespace)
+ bytes, err := os.ReadFile(filepath.Join(fullpath, "git.pb"))
+ if err != nil {
+ log.Info("todo: git.pb non-existant:", fullpath)
+ return nil, err
+ }
+ newr := new(gitpb.Repo)
+ if err := newr.Unmarshal(bytes); err != nil {
+ log.Info("todo: unmarshal failed for git.pb:", fullpath)
+ return nil, fmt.Errorf("todo: generate git.pb for " + fullpath)
+ }
+ return newr, nil
+}
+
+// doesn't enter the directory any further when it finds a .git/
+// not stupid like my old version
+func scanForgedDir(srcDir string) ([]string, error) {
+ var all []string
+ var trip bool
+ err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
+ if err != nil {
+ // Handle possible errors, like permission issues
+ fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
+ return err
+ }
+
+ if d.IsDir() {
+ // log.Info("path is dir", path)
+ } else {
+ _, fname := filepath.Split(path)
+ switch fname {
+ case "repos.pb":
+ case "git.pb":
+ case "go.work.last":
+ case "go.work.sum":
+ default:
+ // todo: figure out a way to do padding for init()
+ if trip == false {
+ log.Info("WARNING:")
+ }
+ log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
+ trip = true
+ }
+ return nil
+ }
+
+ gitdir := filepath.Join(path, "git.pb")
+ _, err2 := os.Stat(gitdir)
+ if !os.IsNotExist(err2) {
+ all = append(all, path)
+ return filepath.SkipDir
+ }
+ // todo: check if dir is empty here and delete dir?
+ return nil
+ })
+ //
+ // probably always leave this here forever
+ // this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
+ // because everything is either checked in or deleted. An important thing to know!
+ if trip {
+ log.Info("WARNING: junk in", srcDir)
+ }
+ return all, err
+}
diff --git a/main.go b/main.go
index 7d225c2..fca5e83 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,8 @@ import (
"embed"
"fmt"
"net/http"
+ "os"
+ "path/filepath"
"time"
"go.wit.com/lib/protobuf/forgepb"
@@ -16,16 +18,60 @@ var BUILDTIME string
//go:embed resources/*
var resources embed.FS
-var forge *forgepb.Forge
+
+// var forge *forgepb.Forge
var HOSTNAME string = "forge.wit.com"
var LIBDIR string = "/var/lib/forged/"
+var FORGEDIR string = "/home/forge"
func main() {
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
- forge = forgepb.Init()
+ // forge = forgepb.Init()
+ if argv.Pull != nil {
+ log.Info("do pull here")
+ dirs, err := scanForgedDir(FORGEDIR)
+ if err != nil {
+ badExit(err)
+ }
+ os.Chdir(FORGEDIR)
+ forge := forgepb.InitPB()
+ log.Info("forge len", forge.Repos.Len())
+ forge.PrintHumanTable(forge.Repos)
+
+ for i, dir := range dirs {
+ log.Info("found repo", i, dir)
+ // repo, err := forge.AddNamespaceDir(ns string, fullpath string)
+ repo, err := readGitPB(dir)
+ if err != nil {
+ log.Info(dir, err)
+ continue
+ }
+ log.Info("repo:", repo.Namespace, repo.URL)
+
+ // check to see if 'git clone' has already been run
+ _, err = os.Stat(filepath.Join(dir, "git.clone"))
+ if os.IsNotExist(err) {
+ log.Info("repo needs cloning", dir)
+ if err := forgepb.RunGitClone("git.clone", dir, "https://"+repo.Namespace); err != nil {
+ log.Info("git clone failed", err)
+ if giturl, err := forgepb.FindGoImport("https://" + repo.Namespace); err == nil {
+ log.Info("TRY THIS INSTEAD!!!!", giturl)
+ if err := forgepb.RunGitClone("git.clone", dir, giturl); err != nil {
+ log.Info("git clone still failed", giturl, err)
+ }
+ }
+
+ }
+ } else {
+ log.Info("repo is already cloned", dir)
+ }
+ break
+ }
+ okExit("")
+ }
http.HandleFunc("/", okHandler)
// go https() // use caddy instead