gowebd/patchsets.go

111 lines
3.0 KiB
Go

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" {
bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
if err != nil {
fmt.Fprintln(w, entry.Name(), err)
fmt.Println(entry.Name(), err)
continue
}
var p *forgepb.Patchs
p = new(forgepb.Patchs)
err = p.Unmarshal(bytes)
if err != nil {
fmt.Fprintln(w, entry.Name(), err)
fmt.Println(entry.Name(), err)
continue
}
author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
fmt.Fprintln(w, entry.Name(), p.Name, p.Comment, author)
fmt.Println(entry.Name(), p.Name, p.Comment, author)
}
}
}
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()
}