get, list, and send patch sets()

This commit is contained in:
Jeff Carr 2024-12-14 14:08:16 -06:00
parent 019d2961e5
commit 3300be8371
2 changed files with 106 additions and 47 deletions

59
http.go
View File

@ -7,10 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -26,19 +23,17 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
log.Info("ioutil.ReadAll() error =", err) log.Info("ioutil.ReadAll() error =", err)
return return
} }
log.Info("ioutil.ReadAll() msg =", len(msg)) // fmt.Fprintln(w, "ioutil.ReadAll() msg =", len(msg))
fmt.Fprintln(w, "ioutil.ReadAll() msg =", len(msg))
// dumpClient(accessf, clientf, r) // dumpClient(accessf, clientf, r)
var route string var route string
// tmp = r.URL.String() // tmp = r.URL.String()
route = cleanURL(r.URL.Path) route = cleanURL(r.URL.Path)
parts := strings.Split(route, "?") parts := strings.Split(route, "?")
log.Info("client sent url =", route) log.Info("client sent url =", route, parts)
log.Info("parts are:", parts)
requrl := parts[0] requrl := parts[0]
url, repourl := findkey(requrl) url, repourl := findkey(requrl)
log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl) log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl, "msg =", len(msg))
if repourl != "" { if repourl != "" {
repoHTML(w, url, repourl) repoHTML(w, url, repourl)
return return
@ -83,48 +78,18 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if route == "/machine" { if route == "/patchset" {
var m *zoopb.Machine savePatchset(w, msg)
m = new(zoopb.Machine)
if err := m.Unmarshal(msg); err != nil {
log.Info("proto.Unmarshal() failed on wire message len", len(msg))
log.Info("error =", err)
return
}
log.Info("GOT patchset:", len(msg))
fmt.Fprintln(w, "GOT patchset:", len(msg))
return return
} }
if route == "/patchset" { if route == "/patchsetlist" {
log.Info("proto.Unmarshal() try message len", len(msg)) listPatchsets(w)
var m *forgepb.Patchs return
m = new(forgepb.Patchs) }
if err := m.Unmarshal(msg); err != nil { if route == "/patchsetget" {
log.Info("proto.Unmarshal() failed on wire message len", len(msg)) filename := r.URL.Query().Get("filename")
log.Info("error =", err) getPatchset(w, filename)
return
}
log.Info("GOT patchset:", len(msg))
fmt.Fprintln(w, "GOT patchset:", len(msg))
all := m.SortByFilename()
for all.Scan() {
repo := all.Next()
log.Info("filename:", repo.Filename)
fmt.Fprintln(w, "filename:", repo.Filename)
}
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/", 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
}
fmt.Fprint(regfile, msg)
regfile.Close()
return return
} }

94
patchsets.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
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.Patchs
m = new(forgepb.Patchs)
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 listPatchsets(w http.ResponseWriter) {
dirname := filepath.Join(LIBDIR, "patchset/")
// Open the directory
entries, err := os.ReadDir(dirname)
if err != nil {
fmt.Printf("Error reading directory: %v\n", err)
fmt.Fprintf(w, "Error reading directory: %v\n", err)
return
}
// Iterate through the directory entries
for _, entry := range entries {
// Check if the entry is a file and matches the *.pb pattern
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
fmt.Fprintln(w, entry.Name())
fmt.Println(entry.Name())
}
}
}
func savePatchset(w http.ResponseWriter, msg []byte) {
log.Info("proto.Unmarshal() try message len", len(msg))
var m *forgepb.Patchs
m = new(forgepb.Patchs)
if err := m.Unmarshal(msg); err != nil {
log.Info("proto.Unmarshal() failed on wire message len", len(msg))
log.Info("error =", err)
return
}
log.Info("GOT patchset:", len(msg))
fmt.Fprintln(w, "GOT patchset:", len(msg))
all := m.SortByFilename()
for all.Scan() {
repo := all.Next()
log.Info("filename:", repo.Filename)
fmt.Fprintln(w, "filename:", repo.Filename)
}
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/", 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
}
regfile.Write(msg)
regfile.Close()
}