Compare commits

...

41 Commits

Author SHA1 Message Date
Jeff Carr 460338c4eb add /sets/ 2025-09-23 14:50:23 -05:00
Jeff Carr cd545c7c9f add doClean() 2025-09-23 14:50:22 -05:00
Jeff Carr b09b86009e some fun stuff 2025-09-23 14:50:21 -05:00
Jeff Carr 1f22b771c3 something good about this 2025-09-23 14:50:19 -05:00
Jeff Carr ada923ea05 changes for new .proto files 2025-09-23 10:17:06 -05:00
Jeff Carr 2b6107fa51 rm GOSRC 2025-09-11 23:10:26 -05:00
Jeff Carr 7a76b5acd1 common forge.Init() 2025-09-11 04:46:33 -05:00
Jeff Carr 6df342bc10 new config() 2025-09-11 03:36:26 -05:00
Jeff Carr 209ee29773 minor fixes 2025-09-10 22:45:01 -05:00
Jeff Carr 43d653a8c0 add config file support 2025-09-10 22:35:20 -05:00
Jeff Carr 6722b019ec convert to httppb 2025-09-09 18:01:41 -05:00
Jeff Carr 82b232f47a use httppb.Log() 2025-09-09 17:14:55 -05:00
Jeff Carr 979fd2f978 rm old code 2025-09-09 12:38:07 -05:00
Jeff Carr 24e137c7ec new GUI changes 2025-09-09 05:08:03 -05:00
Jeff Carr 63be841469 new updates 2025-09-09 03:16:29 -05:00
Jeff Carr 4827be1d2a cleaner argv autocomplete 2025-09-09 02:33:08 -05:00
Jeff Carr f1c1ca950c stub in a gui. doesn't work 2025-09-08 23:14:03 -05:00
Jeff Carr 7ea7393d6c much better and easier to read code 2025-09-08 22:44:16 -05:00
Jeff Carr b9ec316d35 log at the end 2025-09-08 22:44:15 -05:00
Jeff Carr c1de4f1f5a more standardation 2025-09-08 22:44:13 -05:00
Jeff Carr 34d9943e73 committed before checking build ;( 2025-09-08 16:58:19 -05:00
Jeff Carr be62079b78 rm old code 2025-09-08 16:18:59 -05:00
Jeff Carr 4c5ded025f use standard bash stuff 2025-09-08 15:36:32 -05:00
Jeff Carr 192fd065dd moved this to httppb 2025-09-08 12:53:25 -05:00
Jeff Carr 3a1ba210ba rm old code 2025-09-08 12:47:29 -05:00
Jeff Carr f23109dce8 fix the build 2025-09-08 12:42:24 -05:00
Jeff Carr e1411dd1e9 work on repos/ 2025-09-08 08:40:32 -05:00
Jeff Carr 607886cc6a rm old code 2025-09-08 04:44:51 -05:00
Jeff Carr 5e71dd3ecc add "/applied" and "/merged" routes 2025-09-07 22:48:58 -05:00
Jeff Carr 873cb39932 patches being sent again when requested 2025-09-07 21:41:29 -05:00
Jeff Carr a099bbd3e6 add support for old version of the tool using new code 2025-09-07 21:41:26 -05:00
Jeff Carr c2e0e8e80b start using a standard http PB 2025-09-07 21:41:18 -05:00
Jeff Carr eae2c3be2b keep working towards common http PB 2025-09-06 20:07:59 -05:00
Jeff Carr 4e5a34b772 convert http req directly to PB 2025-09-06 18:48:53 -05:00
Jeff Carr f8b2ff5ace more on patch handling 2025-09-06 17:12:46 -05:00
Jeff Carr 0d1f53afee all kindsa crazy. this needs massive cleanup 2025-09-06 16:04:51 -05:00
Jeff Carr d795dbeb61 set route in PB 2025-09-05 14:17:28 -05:00
Jeff Carr b2af891b20 builds. puts http headers in protobuf 2025-09-05 07:24:24 -05:00
Jeff Carr 1442d4f6c5 maybe this'll work 2025-09-05 01:19:39 -05:00
Jeff Carr 77eed08e7a good stuff 2025-09-05 01:05:21 -05:00
Jeff Carr 3db711a6c0 don't die if patches failed to open 2025-09-03 04:03:23 -05:00
13 changed files with 552 additions and 489 deletions

View File

@ -1,17 +1,18 @@
.PHONY: build .PHONY: build
VERSION = $(shell git describe --tags) VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M) # BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
BUILDTIME = $(shell date +%s)
all: install all: build
forged merge FORGE_VERBOSE=true ./forged clean
forged # FORGE_VERBOSE=true ./forged list
build: goimports build: goimports
GO111MODULE=off go build \ GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
verbose: build-verbose: goimports
GO111MODULE=off go build -v -x \ GO111MODULE=off go build -v -x \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
@ -34,11 +35,15 @@ start:
systemctl start forged.service systemctl start forged.service
stop: stop:
systemctl stop forged.service -systemctl stop forged.service
run: install run: build stop
systemctl stop forged.service ./forged --daemon
forged --daemon # setcap 'cap_net_bind_service=+ep' forged # allow the binary to open ports below 1024
#
run-clean: build
-rm /var/lib/forged/all-patches.pb
./forged --daemon
# setcap 'cap_net_bind_service=+ep' forged # allow the binary to open ports below 1024 # setcap 'cap_net_bind_service=+ep' forged # allow the binary to open ports below 1024
prod: build prod: build
@ -68,3 +73,6 @@ squash-the-last-3-commits-together:
git rebase -i HEAD~3 git rebase -i HEAD~3
git fsck git fsck
git prune git prune
list:
./forged list

61
argv.go
View File

@ -6,7 +6,14 @@ package main
*/ */
import ( import (
"go.wit.com/dev/alexflint/arg" "fmt"
"os"
"strconv"
"strings"
"time"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/gui/shell"
) )
var argv args var argv args
@ -14,24 +21,66 @@ var argv args
type args struct { type args struct {
Pull *EmptyCmd `arg:"subcommand:pull" help:"'git pull' on the repos"` Pull *EmptyCmd `arg:"subcommand:pull" help:"'git pull' on the repos"`
List *EmptyCmd `arg:"subcommand:list" help:"list the repos"` List *EmptyCmd `arg:"subcommand:list" help:"list the repos"`
Clean *EmptyCmd `arg:"subcommand:clean" help:"clean the repos"`
Gui *EmptyCmd `arg:"subcommand:gui" help:"show gui"`
Merge *EmptyCmd `arg:"subcommand:merge" help:"merge in new patchsets"` Merge *EmptyCmd `arg:"subcommand:merge" help:"merge in new patchsets"`
Init *EmptyCmd `arg:"subcommand:init" help:"init the repo list"` Init *EmptyCmd `arg:"subcommand:init" help:"init the repo list"`
Repos *EmptyCmd `arg:"subcommand:repos" help:"show the repos"`
Port int `arg:"--port" default:"2520" help:"port to run on"` Port int `arg:"--port" default:"2520" help:"port to run on"`
Hostname string `arg:"--hostname" default:"forge.wit.com" help:"hostname to use"` Hostname string `arg:"--hostname" help:"hostname to use"`
Daemon bool `arg:"--daemon" help:"run as a daemon"` Daemon bool `arg:"--daemon" help:"run as a daemon"`
Force bool `arg:"--force" help:"try to strong arm things"` Force bool `arg:"--force" help:"try to strong arm things"`
Verbose bool `arg:"--verbose" help:"show more output"` Verbose bool `arg:"--verbose" help:"show more output"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"`
} }
type EmptyCmd struct { type EmptyCmd struct {
} }
func (args) Appname() string {
return ARGNAME
}
func (args) Version() string { func (args) Version() string {
parts := strings.Split(BUILDTIME, ".")
if len(parts) == 1 {
// The input epoch seconds
// epochSeconds := int64(1758646486)
num, err := strconv.Atoi(BUILDTIME)
epochSeconds := int64(num)
if err == nil {
// 1. Convert the epoch seconds to a time.Time object.
// time.Unix() creates the time in the UTC timezone by default.
t := time.Unix(epochSeconds, 0)
// 2. Convert the UTC time to the computer's local timezone.
localTime := t.Local()
// 3. Print the result. The default format is clear and includes the timezone.
// fmt.Println("Default format:", localTime)
// For a more human-friendly format, use the Format() method.
// Go uses a special reference time for formatting: Mon Jan 2 15:04:05 2006 MST
// You lay out your desired format using these specific numbers.
// formattedString := localTime.Format("Monday, January 2, 2006 at 3:04:05 PM (MST)")
// fmt.Println(" Custom format:", formattedString)
// now := time.Now()
// dur := time.Since(localTime)
BUILDTIME = fmt.Sprintf("%s age(%v)", localTime.String(), shell.FormatDuration(time.Since(localTime)))
}
}
return ARGNAME + " " + VERSION + " Built on " + BUILDTIME return ARGNAME + " " + VERSION + " Built on " + BUILDTIME
} }
func init() { func (a args) DoAutoComplete(pb *prep.Auto) {
arg.MustParse(&argv) switch pb.Cmd {
case "list":
pb.Autocomplete2("--missing")
case "clean":
pb.Autocomplete2("")
default:
pb.Autocomplete2("list clean")
}
os.Exit(0)
} }

View File

@ -1,102 +0,0 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"os"
"path/filepath"
"go.wit.com/lib/gui/shell"
)
/*
handles shell autocomplete
*/
// used for shell auto completion
func (args) doBashAuto() {
argv.doBashHelp()
switch argv.BashAuto[0] {
case "merge":
fmt.Println("--force")
default:
if argv.BashAuto[0] == ARGNAME {
// list the subcommands here
fmt.Println("list merge")
}
}
os.Exit(0)
}
// prints help to STDERR // TODO: move everything below this to go-args
func (args) doBashHelp() {
if len(argv.BashAuto) < 2 {
fmt.Fprintf(os.Stderr, "something went wrong with the GO args autocomplete in %s\n", ARGNAME)
return
}
if argv.BashAuto[1] != "''" {
// if this is not blank, then the user has typed something
return
}
if argv.BashAuto[0] != ARGNAME {
// if this is not the name of the command, the user already started doing something
return
}
if argv.BashAuto[0] == ARGNAME {
me.pp.WriteHelp(os.Stderr)
return
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "something went wrong with the GO args package")
fmt.Fprintln(os.Stderr, "")
}
func (args) doBash() {
if homeDir, err := os.UserHomeDir(); err == nil {
filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", ARGNAME)
if !shell.Exists(filename) {
if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
f.Write([]byte(makeBashCompletionText(ARGNAME)))
f.Close()
}
}
}
fmt.Println(makeBashCompletionText(ARGNAME))
}
func makeBashCompletionText(argname string) string {
var out string
out += fmt.Sprintf("# add this in your bashrc:\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf("# todo: add this to go-arg as a 'hidden' go-arg option --bash\n")
out += fmt.Sprintf("#\n")
out += fmt.Sprintf("# Put the below in the file: ~/.local/share/bash-completion/completions/%s\n", argname)
out += fmt.Sprintf("#\n")
out += fmt.Sprintf("# todo: make this output work/parse with:\n")
out += fmt.Sprintf("# complete -C " + argname + " --bash go\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf("_" + argname + "_complete()\n")
out += fmt.Sprintf("{\n")
out += fmt.Sprintf(" # sets local to this func vars\n")
out += fmt.Sprintf(" local cur prev all\n")
out += fmt.Sprintf(" cur=${COMP_WORDS[COMP_CWORD]}\n")
out += fmt.Sprintf(" prev=${COMP_WORDS[COMP_CWORD-1]}\n")
out += fmt.Sprintf(" all=${COMP_WORDS[@]}\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf(" # this is where we generate the go-arg output\n")
out += fmt.Sprintf(" GOARGS=$(" + argname + " --auto-complete $prev \\'$cur\\' $all)\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf(" # this compares the command line input from the user\n")
out += fmt.Sprintf(" # to whatever strings we output\n")
out += fmt.Sprintf(" COMPREPLY=( $(compgen -W \"$GOARGS\" -- $cur) ) # THIS WORKS\n")
out += fmt.Sprintf(" return 0\n")
out += fmt.Sprintf("}\n")
out += fmt.Sprintf("complete -F _%s_complete %s\n", argname, argname)
out += fmt.Sprintf("\n")
out += fmt.Sprintf("# copy and paste the above into your bash shell should work\n")
return out
}

50
config.go Normal file
View File

@ -0,0 +1,50 @@
package main
// functions to import and export the protobuf
// data to and from config files
import (
"errors"
"os"
"path/filepath"
"go.wit.com/lib/config"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
func configSave() error {
return me.configs.ConfigSave()
}
func configInit() *forgepb.ForgeConfigs {
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
// the default forged dir is /home/forge
if os.Getenv("FORGE_REPODIR") == "" {
os.Setenv("FORGE_REPODIR", "/home/forge")
}
if os.Getenv("FORGE_PATCHDIR") == "" {
os.Setenv("FORGE_PATCHDIR", "/var/lib/forged")
}
configs := new(forgepb.ForgeConfigs)
err := config.ConfigLoad(configs, ARGNAME, "forge")
if errors.Is(err, os.ErrNotExist) {
configs.ReposDir = os.Getenv("FORGE_REPODIR")
configs.ReposPB = filepath.Join(configs.ReposDir, "repos.pb")
configs.PatchDir = os.Getenv("FORGE_PATCHDIR")
if err := configSave(); err != nil {
badExit(err)
}
log.Info("made a default config file here", configs.Filename)
okExit("")
}
if err != nil {
badExit(err)
}
return configs
}

51
doGui.go Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// An app to submit patches for the 30 GO GUI repos
import (
"time"
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
func debug() {
defer func() {
if r := recover(); r != nil {
gui.Crash(r, "forge debug()")
}
}()
time.Sleep(2 * time.Second)
for {
now := time.Now()
doList()
log.Printf("finished a scan here in (%s)\n", shell.FormatDuration(time.Since(now)))
time.Sleep(90 * time.Second)
}
}
func doGui() {
mainWindow := gadgets.NewGenericWindow("forged: forge.wit.com", "Current Settings")
mainWindow.Custom = func() {
log.Warn("MAIN WINDOW CLOSE")
now := time.Now()
log.Printf("rill repos.Reload() took (%s)\n", shell.FormatDuration(time.Since(now)))
okExit("")
}
drawWindow(mainWindow)
}
func drawWindow(win *gadgets.GenericWindow) {
grid := win.Group.RawGrid()
grid.NewButton("stats", func() {
doList()
})
}

View File

@ -6,26 +6,27 @@ import (
) )
func doList() error { func doList() error {
log.Info("do list here") log.Infof("do list here. Patchsets.Len()=%d\n", me.forge.Patchsets.Len())
for pset := range me.forge.Patchsets.IterAll() {
if err := me.forge.LoadPatchsets(); err != nil { pset.PrintTable()
badExit(err) }
} return nil
// first show the general patchset protobuf
for pb := range me.forge.Patchsets.IterAll() {
if pb.Name == "forge auto commit" {
pb.ShowPatchsets()
}
} }
func doClean() error {
log.Infof("clean Patchsets.Len()=%d\n", me.forge.Patchsets.Len())
// show all the patchsets with Names // show all the patchsets with Names
for pset := range me.forge.Patchsets.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
if pset.Name == "forge auto commit" { for patch := range pset.Patches.IterAll() {
continue if patch.PatchId == "" {
log.Info("Delete", patch.CommitHash, patch.PatchId, patch.Namespace)
pset.Patches.Delete(patch)
} else {
log.Info("\t", patch.CommitHash, patch.PatchId, patch.Namespace)
} }
pset.ShowPatchsets()
} }
}
me.forge.SavePatchsets()
return nil return nil
} }
@ -45,3 +46,20 @@ func findPatch(newpatch *forgepb.Patch) bool {
return false return false
} }
// returns true if the patch already exists in the protobuf
func expirePatch(newpatch *forgepb.Patch) bool {
// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)
for pset := range me.forge.Patchsets.IterAll() {
for _, patch := range pset.Patches.Patches {
if patch.CommitHash == newpatch.CommitHash {
patch.NewHash = newpatch.NewHash
// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)
return true
}
}
}
return false
}

View File

@ -21,7 +21,7 @@ func doMerge() error {
return nil return nil
} }
func findAutoPatchset() *forgepb.Patchset { func findAutoPatchset() *forgepb.Set {
for pset := range me.forge.Patchsets.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
if pset.Name == "forge auto commit" { if pset.Name == "forge auto commit" {
return pset return pset
@ -29,14 +29,14 @@ func findAutoPatchset() *forgepb.Patchset {
} }
} }
var fauto *forgepb.Patchset var fauto *forgepb.Set
log.Warn("findAutoPatchset() had to create 'forge auto commit'") log.Warn("findAutoPatchset() had to create 'forge auto commit'")
if fauto == nil { if fauto == nil {
fauto = new(forgepb.Patchset) fauto = new(forgepb.Set)
fauto.Name = "forge auto commit" fauto.Name = "forge auto commit"
fauto.Patches = forgepb.NewPatches() fauto.Patches = forgepb.NewPatches()
fauto.Uuid = uuid.New().String() fauto.Uuid = uuid.New().String()
me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, fauto) me.forge.Patchsets.Append(fauto)
} }
return fauto return fauto
} }
@ -63,7 +63,7 @@ func addRandomPatch(patch *forgepb.Patch) error {
/* /*
// adds a patchset or just the patches // adds a patchset or just the patches
func addPatchset(filename string, pb *forgepb.Patchset) { func addPatchset(filename string, pb *forgepb.Set) {
// if the name of the patchset is "forge auto commit" // if the name of the patchset is "forge auto commit"
// then just add all the patches // then just add all the patches
if pb.Name == "forge auto commit" { if pb.Name == "forge auto commit" {
@ -96,8 +96,8 @@ func addPatchset(filename string, pb *forgepb.Patchset) {
} }
// Clone() this protobuf into me.forge.Patchsets // Clone() this protobuf into me.forge.Patchsets
var newpb *forgepb.Patchset var newpb *forgepb.Set
newpb = proto.Clone(pb).(*forgepb.Patchset) newpb = proto.Clone(pb).(*forgepb.Set)
if newpb != nil { if newpb != nil {
me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, newpb) me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, newpb)
} }
@ -122,8 +122,8 @@ func mergePatchsets() {
fmt.Println(entry.Name(), err) fmt.Println(entry.Name(), err)
continue continue
} }
var p *forgepb.Patchset var p *forgepb.Set
p = new(forgepb.Patchset) p = new(forgepb.Set)
err = p.Unmarshal(bytes) err = p.Unmarshal(bytes)
if err != nil { if err != nil {
fmt.Println(entry.Name(), err) fmt.Println(entry.Name(), err)

View File

@ -1,190 +0,0 @@
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
)
func getPatchset(w http.ResponseWriter, pbname string) {
if pbname == "" {
fmt.Fprintf(w, "filename was empty")
return
}
msg := fmt.Sprintf("filename = %s\n", pbname)
log.Info(msg)
filename := filepath.Join(LIBDIR, "patchset/", pbname)
data, err := os.ReadFile(filename)
if err != nil {
msg := fmt.Sprintf("Error reading file %s: %v\n", filename, err)
fmt.Printf(msg)
fmt.Fprintf(w, msg)
return
}
var m *forgepb.Patchset
m = new(forgepb.Patchset)
if err := m.Unmarshal(data); err != nil {
msg := fmt.Sprintf("proto.Unmarshal() failed on %s len=%d\n", filename, len(data))
fmt.Printf(msg)
fmt.Fprintf(w, msg)
msg = fmt.Sprintf("proto.Unmarshal() error %v\n", err)
fmt.Printf(msg)
fmt.Fprintf(w, msg)
return
}
log.Info("going to w.Write(data) with len", len(data))
w.Write(data)
}
func savePatchset(w http.ResponseWriter, msg []byte) error {
// log.Info("proto.Unmarshal() try message len", len(msg))
var m *forgepb.Patchset
m = new(forgepb.Patchset)
if err := m.Unmarshal(msg); err != nil {
log.Info("proto.Unmarshal() failed on wire message len", len(msg))
log.Info("error =", err)
return err
}
log.Info("GOT patchset:", len(msg))
// fmt.Fprintln(w, "GOT patchset:", len(msg))
now := time.Now()
// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
filename := filepath.Join(LIBDIR, "patchset.archive/", timestamp+".submitted.pb")
regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Info("filename open error:", filename, err)
// fmt.Fprintln(w, "filename open error:", filename, err)
return err
}
regfile.Write(msg)
regfile.Close()
me.forge.AddPatchset(m)
response := forgepb.NewPatches() // this sets the proper handshake protobuf UUID
all := m.Patches.SortByFilename()
for all.Scan() {
p := all.Next()
log.Info("filename:", p.Filename)
var newp *forgepb.Patch
newp = p
newp = proto.Clone(p).(*forgepb.Patch)
newp.Data = nil // don't send back all the data
// fmt.Fprintln(w, "filename:", repo.Filename)
response.Append(newp)
}
log.Info("sent", m.Patches.Len(), "filenames. now going to attempt to marshal and send Patches protobuf")
data, err := response.Marshal()
if err != nil {
log.Infof("savePatchset() proto.Marshal() error %v\n", err)
return err
}
log.Infof("savePatchset() proto.Unmarshal() try to send len(msg)=%d back to the client forge\n", len(data))
w.Write(data)
return nil
}
func lookupRepos(msg []byte) (*gitpb.Repos, error) {
log.Info("proto.Unmarshal() try message len", len(msg))
m := gitpb.NewRepos()
if err := m.Unmarshal(msg); err != nil {
log.Info("gitpb.Repos.Unmarshal() failed. len(msg) =", len(msg), err)
return m, err
}
newpb := gitpb.NewRepos()
log.Info("GOT repos:", len(msg))
for repo := range m.IterAll() {
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
if repo.Namespace == "" {
log.Info("todo: namespace empty for", repo.FullPath)
continue
// return nil, fmt.Errorf("todo: namespace empty for " + repo.FullPath)
}
// fmt.Fprintln(w, "repo:", repo.FullPath, repo.Namespace)
bytes, err := os.ReadFile(filepath.Join("/home/forge", repo.Namespace, "git.pb"))
if err != nil {
log.Info("todo: git.pb non-existant:", repo.FullPath)
// return nil, err
continue
}
newr := new(gitpb.Repo)
if err := newr.Unmarshal(bytes); err != nil {
log.Info("todo: unmarshal failed for git.pb:", repo.FullPath)
// return nil, fmt.Errorf("todo: generate git.pb for " + repo.Namespace)
continue
}
// log.Infof("repo unmarshal worked:%s,%s\n", repo.Namespace, repo.FullPath)
newpb.Append(newr)
}
// print out found git.pb files
for repo := range newpb.IterAll() {
log.Infof("found repo:%-14s,%s\n", repo.GetMasterVersion(), repo.Namespace)
}
return newpb, nil
}
func updateRepos(msg []byte) (*gitpb.Repos, error) {
log.Info("proto.Unmarshal() try message len", len(msg))
m := gitpb.NewRepos()
if err := m.Unmarshal(msg); err != nil {
log.Info("gitpb.Repos.Unmarshal() failed. len(msg) =", len(msg), err)
return m, err
}
newpb := gitpb.NewRepos()
log.Info("GOT repos:", len(msg))
for repo := range m.IterAll() {
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
if repo.Namespace == "" {
log.Info("todo: namespace empty for", repo.FullPath)
continue
// return nil, fmt.Errorf("todo: namespace empty for " + repo.FullPath)
}
// save the git.pb files
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")
log.Infof("update repo file %s\n", repopath)
// 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)
}
/*
// print out found git.pb files
for repo := range newpb.IterAll() {
log.Infof("updated repo:%-14s,%s\n", repo.GetMasterVersion(), repo.Namespace)
}
*/
return newpb, nil
}

66
handlePatches.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"net/http"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/log"
)
func addNewPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
newPatchesPB := new(forgepb.Patches)
for newpatch := range pb.IterAll() {
me.forge.AddNewPatch(newpatch)
newPatchesPB.Append(newpatch)
}
reqPB.Logf("Recived %d patches from client", newPatchesPB.Len())
return newPatchesPB
}
func handleMergedPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
newPatchesPB := new(forgepb.Patches)
for newpatch := range pb.IterAll() {
if expirePatch(newpatch) {
newPatchesPB.Append(newpatch)
}
}
return newPatchesPB
}
func sendPendingPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
allPatchesPB := new(forgepb.Patches)
for pset := range me.forge.Patchsets.IterAll() {
for newpatch := range pset.Patches.IterAll() {
allPatchesPB.Append(newpatch)
}
}
return allPatchesPB
}
func sendPendingPatchsets(pb *forgepb.Sets, reqPB *httppb.HttpRequest) *forgepb.Sets {
allPatchsetsPB := new(forgepb.Sets)
for pset := range me.forge.Patchsets.IterAll() {
allPatchsetsPB.Append(pset)
}
return allPatchsetsPB
}
/*
func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) {
pb := forgepb.NewPatches()
err := pb.Unmarshal(reqPB.ServerData)
return pb, err
}
*/
func makePatchsetsPB(reqPB *httppb.HttpRequest) (*forgepb.Sets, error) {
pb := forgepb.NewSets()
err := pb.Unmarshal(reqPB.ServerData)
return pb, err
}
func sendPatchesError(w http.ResponseWriter, r *forgepb.Patches, err error) error {
log.Info("send error back to user", err)
return nil
}

61
handleRepos.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"strings"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/log"
)
func handleRepos(pb *gitpb.Repos, route string) error {
log.Info("GOT PATCHES ROUTE", route, "with # patches =", pb.Len())
if strings.HasPrefix(route, "/patches/old") {
} else if strings.HasPrefix(route, "/patches/new") {
log.Info("add new patches")
} else {
log.Info("unknown route", route)
}
return nil
}
// returns a repo PB with just the master and devel branch versions
// is just this information so it keeps things small when it's sent over the wire
func getCurrentRepoVersions(namespace string) *gitpb.Repo {
newrepo := new(gitpb.Repo)
newrepo.Namespace = namespace
found := me.forge.Repos.FindByNamespace(namespace)
if found == nil {
// todo: clone repo here
return newrepo
}
newrepo.MasterHash = found.MasterHash
newrepo.DevelHash = found.DevelHash
newrepo.URL = found.URL
return newrepo
}
func pullRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
versionsPB := gitpb.NewRepos()
for repo := range pb.IterAll() {
found := getCurrentRepoVersions(repo.Namespace)
versionsPB.Append(found)
}
return versionsPB
}
func addRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
newReposPB := gitpb.NewRepos()
for repo := range pb.IterAll() {
if found := me.forge.Repos.FindByNamespace(repo.Namespace); found != nil {
// already know about this namespace
continue
}
newrepo := new(gitpb.Repo)
newrepo.Namespace = repo.Namespace
newrepo.URL = repo.URL
newReposPB.Append(newrepo)
}
return newReposPB
}

236
http.go
View File

@ -2,33 +2,31 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/log" "go.wit.com/log"
) )
// remove '?' part and trailing '/' func logReqPB(pb *httppb.HttpRequest) {
func cleanURL(url string) string { log.Info("LOG: httppb.HttpRequest START:")
url = "/" + strings.Trim(url, "/") for i, line := range pb.Logs {
return url log.Infof("%d, URL:%s LINE: %s\n", int(i), pb.URL, line)
}
log.Info("LOG: httppb.HttpRequest END")
} }
func okHandler(w http.ResponseWriter, r *http.Request) { func okHandler(w http.ResponseWriter, r *http.Request) {
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte reqPB, err := httppb.ReqToPB(r)
reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
if err != nil { if err != nil {
log.Info("ioutil.ReadAll() error =", err) reqPB.Logf("httppb err %v", err)
return
} }
var route string route := reqPB.Route
route = cleanURL(r.URL.Path)
parts := strings.Split(route, "?")
// log.Info("client sent url =", route, parts)
requrl := parts[0]
if route == "/" { if route == "/" {
w.Header().Set("Content-Type", "text") w.Header().Set("Content-Type", "text")
@ -37,92 +35,160 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if strings.HasPrefix(route, "/repos/") {
pb := gitpb.NewRepos()
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
reqPB.Logf("Repos Unmarshal() len=%d", pb.Len())
} else {
reqPB.Logf("Repos Unmarshal() err=%v", err)
}
result := gitpb.NewRepos()
switch route {
case "/repos/check":
result = addRequest(pb, reqPB)
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d", result.Len(), pb.Len())
case "/repos/pull":
result = pullRequest(pb, reqPB)
case "/repos/add":
result = addRequest(pb, reqPB)
default:
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d", result.Len(), pb.Len())
log.Info("repos", route, "unknown")
}
if err := result.SendReply(w, reqPB); err != nil {
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
}
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
if strings.HasPrefix(route, "/set/") {
pb := new(forgepb.Set)
if err := pb.Unmarshal(reqPB.ClientData); err != nil {
reqPB.Logf("Patches Unmarshal() len(data)=%d err=%v", len(reqPB.ClientData), err)
logReqPB(reqPB)
return
}
reqPB.Logf("Patches Unmarshal() len=%d", pb.Patches.Len())
result := new(forgepb.Set)
switch route {
case "/set/new":
me.forge.Patchsets.Append(pb)
reqPB.Logf("addNewPatches() pb.Patches.Len()=%d Patchsets.Len()=%d", pb.Patches.Len(), me.forge.Patchsets.Len())
me.forge.SavePatchsets()
result.Uuid = pb.Uuid
default:
}
if err := result.SendReply(w, reqPB); err != nil {
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
}
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
if strings.HasPrefix(route, "/sets/") {
pb := forgepb.NewSets()
if err := pb.Unmarshal(reqPB.ClientData); err != nil {
reqPB.Logf("Patches Unmarshal() len(data)=%d err=%v", len(reqPB.ClientData), err)
logReqPB(reqPB)
return
}
reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
switch route {
case "/sets/get":
if err := me.forge.Patchsets.SendReply(w, reqPB); err != nil {
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
} else {
reqPB.Logf("SendReply() worked")
}
default:
}
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
if strings.HasPrefix(route, "/patches/") {
pb := forgepb.NewPatches()
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
} else {
reqPB.Logf("Patches Unmarshal() err=%v", err)
}
result := forgepb.NewPatches()
switch route {
case "/patches/new":
result = addNewPatches(pb, reqPB)
reqPB.Logf("addNewPatches() pb.Len()=%d result.Len()=%d", pb.Len(), result.Len())
case "/patches/applied":
reqPB.Log("not really anything needs to be done on applied patches?")
case "/patches/merged":
reqPB.Log("a maintainer has merged these patches")
result = handleMergedPatches(pb, reqPB)
// result = handleAppliedPatches(pb, reqPB)
case "/patches/get":
result = sendPendingPatches(pb, reqPB)
default:
result = addNewPatches(pb, reqPB)
}
if err := result.SendReply(w, reqPB); err != nil {
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
}
me.forge.SavePatchsets()
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
if strings.HasPrefix(route, "/patchsets/") {
pb, err := makePatchsetsPB(reqPB)
if err != nil {
reqPB.Logf("%v", err)
}
result := forgepb.NewSets()
switch route {
case "/patches/get":
result = sendPendingPatchsets(pb, reqPB)
default:
result = sendPendingPatchsets(pb, reqPB)
}
if err := result.SendReply(w, reqPB); err != nil {
log.Info("Oh well, Send to client failed. err =", err)
}
log.Info("Send to client seems to have worked. log:", reqPB.Log)
me.forge.SavePatchsets()
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
logReqPB(reqPB)
// used for uptime monitor checking // used for uptime monitor checking
if route == "/uptime" { if route == "/uptime" {
writeFile(w, "uptime.html") httppb.WriteFile(w, resources, "uptime.html")
return
}
log.Warn("forged REQUEST URL =", requrl, "msg =", len(msg))
if route == "/patchset" {
if err := savePatchset(w, msg); err != nil {
log.Warn("forged /patchset error", err)
return
}
if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err)
return
}
return
}
if route == "/lookup" {
log.Info("doing lookup len(msg) =", len(msg))
found, err := lookupRepos(msg)
if err != nil {
return
}
/*
for repo := range repos.IterAll() {
repo.Namespace += "good"
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
}
*/
found.SendPB(w)
return
}
if route == "/update" {
log.Info("doing update len(msg) =", len(msg))
found, err := updateRepos(msg)
if err != nil {
return
}
found.SendPB(w)
return
}
if route == "/GetPatchsets" {
data, err := me.forge.Patchsets.Marshal()
if err != nil {
log.Info("patchsets.Marshal() to wire failed", err)
return
}
start := time.Now()
log.Info("going to w.Write(data) with len", len(data))
w.Write(data)
age := shell.FormatDuration(time.Since(start))
log.Printf("Done with xfer in (%s). happy hacking!\n", age)
return
}
if route == "/patchsetget" {
filename := r.URL.Query().Get("filename")
getPatchset(w, filename)
return return
} }
if route == "/goReference.svg" { if route == "/goReference.svg" {
w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Content-Type", "image/svg+xml")
writeFile(w, "goReference.svg") httppb.WriteFile(w, resources, "goReference.svg")
return return
} }
if route == "/skeleton.v2.css" { if route == "/skeleton.v2.css" {
writeFile(w, "skeleton.v2.css") httppb.WriteFile(w, resources, "skeleton.v2.css")
return return
} }
if route == "/favicon.ico" { if route == "/favicon.ico" {
writeFile(w, "ipv6.png") httppb.WriteFile(w, resources, "ipv6.png")
return return
} }
log.Warn("BAD URL =", requrl) log.Warn("BAD URL =", reqPB.URL, "from", reqPB.IP, "author", reqPB.Hostname)
badurl(w, r.URL.String()) badurl(w, r.URL.String())
} }
/*
func writeFile(w http.ResponseWriter, filename string) { func writeFile(w http.ResponseWriter, filename string) {
// fmt.Fprintln(w, "GOT TEST?") // fmt.Fprintln(w, "GOT TEST?")
fullname := "resources/" + filename fullname := "resources/" + filename
@ -136,8 +202,8 @@ func writeFile(w http.ResponseWriter, filename string) {
var repohtml string var repohtml string
repohtml = string(pfile) repohtml = string(pfile)
fmt.Fprintln(w, repohtml) fmt.Fprintln(w, repohtml)
// log.Println("writeFile() found internal file:", filename)
} }
*/
func badurl(w http.ResponseWriter, badurl string) { func badurl(w http.ResponseWriter, badurl string) {
fmt.Fprintln(w, "<!DOCTYPE html>") fmt.Fprintln(w, "<!DOCTYPE html>")
@ -146,7 +212,7 @@ func badurl(w http.ResponseWriter, badurl string) {
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">") fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">")
fmt.Fprintln(w, " </head>") fmt.Fprintln(w, " </head>")
fmt.Fprintln(w, " <body>") fmt.Fprintln(w, " <body>")
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>") fmt.Fprintln(w, " IPv4 MAY NOT WORK<br>")
fmt.Fprintln(w, " FORGE REQUIRES IPv6.<br>") fmt.Fprintln(w, " FORGE REQUIRES IPv6.<br>")
fmt.Fprintln(w, " <br>") fmt.Fprintln(w, " <br>")
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>") fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>")

119
main.go
View File

@ -4,11 +4,10 @@ import (
"embed" "embed"
"fmt" "fmt"
"net/http" "net/http"
"os"
"time" "time"
"go.wit.com/dev/alexflint/arg" "go.wit.com/lib/fhelp"
"go.wit.com/gui" "go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -22,47 +21,37 @@ var ARGNAME string = "forged"
var resources embed.FS var resources embed.FS
var HOSTNAME string = "forge.wit.com" var HOSTNAME string = "forge.wit.com"
var LIBDIR string = "/var/lib/forged/" var LIBDIR string = "/var/lib/forged/" // need to deprecate this
var FORGEDIR string = "/home/forge" // var FORGEDIR string = "/home/forge" // deprecated?
func main() { func main() {
me = new(mainType) me = new(mainType)
gui.InitArg() me.myGui = prep.Gui() // prepares the GUI package for go-args
me.pp = arg.MustParse(&argv) me.auto = prep.Bash3(&argv) // this line should be: prep.Bash(&argv)
if argv.Bash { me.forge = forgepb.InitByAppname(ARGNAME)
argv.doBash()
os.Exit(0)
}
if len(argv.BashAuto) != 0 {
argv.doBashAuto()
os.Exit(0)
}
if argv.Hostname != "" { if err := me.forge.InitPatchsets(); err != nil {
HOSTNAME = argv.Hostname log.Info("patches failed to open", err)
}
// the default forged dir is /home/forge
if os.Getenv("FORGE_GOSRC") == "" {
os.Setenv("FORGE_GOSRC", "/home/forge")
}
if os.Getenv("FORGE_PATCHDIR") == "" {
os.Setenv("FORGE_PATCHDIR", "/var/lib/forged")
}
me.forge = forgepb.RawInitPB()
if err := me.forge.LoadPatchsets(); err != nil {
badExit(err) badExit(err)
} }
if argv.List != nil { if argv.List != nil {
log.Printf("forge.Init() %s len()=%d\n", me.forge.Config.Filename, me.forge.Repos.Len())
fhelp.DumpENV("finit:")
me.forge.Config.DumpENV()
doList() doList()
okExit("") okExit("")
} }
if argv.Clean != nil {
log.Printf("forge.Init() %s len()=%d\n", me.forge.Config.Filename, me.forge.Repos.Len())
fhelp.DumpENV("finit:")
me.forge.Config.DumpENV()
doClean()
okExit("")
}
if argv.Merge != nil { if argv.Merge != nil {
if err := doMerge(); err != nil { if err := doMerge(); err != nil {
badExit(err) badExit(err)
@ -76,44 +65,38 @@ func main() {
} }
if argv.Daemon == true { if argv.Daemon == true {
log.Info("Running in --daemon mode") if argv.Gui != nil {
http.HandleFunc("/", okHandler) me.myGui.Start() // loads the GUI toolkit
// go https() // use caddy instead doGui() // start making our forge GUI
debug() // sits here forever
}
mux := http.NewServeMux()
okHandlerFunc := http.HandlerFunc(okHandler)
// Set a limit of 50 kilobytes for requests to this handler.
// Adjust this value to your needs.
const maxUploadSize = 1025 * 1024 // 1 MB
mux.Handle("/", http.MaxBytesHandler(okHandlerFunc, maxUploadSize))
p := fmt.Sprintf(":%d", argv.Port) p := fmt.Sprintf(":%d", argv.Port)
log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME) log.Printf("Server starting on port %s...\n", p)
log.Println("Running on port", "http://localhost"+p) log.Printf("Test with: curl -d 'hello world' http://localhost:%s/\n", p)
log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
err := http.ListenAndServe(p, nil) server := &http.Server{
if err != nil { Addr: p,
log.Println("Error starting server:", err) Handler: mux,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
} }
} else {
log.Printf("Server starting on port %s with a 1 MB request body limit...\n", p)
if err := server.ListenAndServe(); err != nil {
log.Fatal("Could not start server: %s\n", err)
}
okExit("")
}
log.Info("--daemon was not set. Just list the patches.") log.Info("--daemon was not set. Just list the patches.")
doList() // doList()
}
}
func formatDuration(d time.Duration) string {
seconds := int(d.Seconds()) % 60
minutes := int(d.Minutes()) % 60
hours := int(d.Hours()) % 24
days := int(d.Hours()) / 24
result := ""
if days > 0 {
result += fmt.Sprintf("%dd ", days)
return result
}
if hours > 0 {
result += fmt.Sprintf("%dh ", hours)
return result
}
if minutes > 0 {
result += fmt.Sprintf("%dm ", minutes)
return result
}
if seconds > 0 {
result += fmt.Sprintf("%ds", seconds)
}
return result
} }

View File

@ -5,6 +5,7 @@ package main
import ( import (
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
) )
@ -14,5 +15,7 @@ var me *mainType
type mainType struct { type mainType struct {
pp *arg.Parser // for parsing the command line args. Yay to alexf lint! pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
forge *forgepb.Forge // for holding the forge protobuf files forge *forgepb.Forge // for holding the forge protobuf files
// myGui *gui.Node // the gui toolkit handle myGui *prep.GuiPrep // the gui toolkit handle
auto *prep.Auto // more experiments for bash handling
configs *forgepb.ForgeConfigs // for holding the forge protobuf files
} }