guireleaser/http.go

251 lines
6.6 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
"os"
"strings"
"go.wit.com/lib/gui/repolist"
"go.wit.com/log"
)
// remove '?' part and trailing '/'
func cleanURL(url string) string {
url = "/" + strings.Trim(url, "/")
return url
}
// send stuff to the socket and stdout
func msg(w http.ResponseWriter, s string) {
log.Info(s)
fmt.Fprintln(w, s)
}
func okHandler(w http.ResponseWriter, r *http.Request) {
var route string
route = cleanURL(r.URL.Path)
// common http args that can be set
repoName := r.URL.Query().Get("repo")
version := r.URL.Query().Get("version")
comment := r.URL.Query().Get("comment")
readonly := r.URL.Query().Get("readonly")
onlydirty := r.URL.Query().Get("onlydirty")
perfect := r.URL.Query().Get("perfect")
whitelist := r.URL.Query().Get("whitelist")
switch route {
case "/help":
msg(w, "list/ list modified repos")
msg(w, "list?readonly=true shows every repo")
msg(w, "")
msg(w, "doRelease runs doRelease()")
msg(w, "findNext runs findNext()")
msg(w, "showNext shows the repo for doRelease()")
msg(w, "setTargetVersion set the target version for findNext()")
msg(w, "setAllTargetVersions?version=v0.12.4 set ever repo to target version")
msg(w, "setCurrentRepo?repo=go.wit.com/gui runs setCurrentRepo(repo)")
msg(w, "")
msg(w, "setAllBranchesToMaster git checkout master on every repo")
msg(w, "")
msg(w, "setVersion?repo=go.wit.com/gui?target=0.2 attempts to set the target version to 0.2")
msg(w, "")
case "/tempWin":
tempWin := makeTempView()
loop := me.repos.View.UnmergedRepos()
for loop.Scan() {
repo := loop.Repo()
tempWin.View.ShowRepo(repo)
msg(w, "UnmergedRepo: "+repo.Name())
}
case "/doRelease":
buttonDisable()
if doRelease() {
buttonEnable()
msg(w, "doRelease() worked")
} else {
msg(w, "doRelease() failed")
}
case "/findNext":
me.Disable()
defer me.Enable()
if findNext() {
msg(w, "findNext() found a repo")
} else {
msg(w, "findNext() did not find a repo. You might be finished?")
}
msg(w, "repo: "+me.release.repo.String())
msg(w, "name: "+me.release.version.String())
msg(w, "notes: "+me.release.notes.String())
msg(w, "status: "+me.release.status.String())
msg(w, me.current.StandardHeader())
msg(w, me.current.StandardReleaseHeader())
case "/rescanAll":
me.repos.View.ScanRepositories()
case "/setCurrentRepo":
msg(w, "repo: "+repoName)
msg(w, "version: "+version)
msg(w, "comment: "+comment)
repo := me.repos.View.FindRepoByName(repoName)
if repo == nil {
msg(w, "FindRepoByName() returned nil")
return
}
setCurrentRepo(repo, "HTTP", "doRelease() ?")
case "/IncrementRevisonVersion":
msg(w, "repo: "+repoName)
msg(w, "version: "+version)
me.current.Status.IncrementRevisionVersion(comment)
case "/IncrementMinorVersion":
msg(w, "repo: "+repoName)
msg(w, "version: "+version)
me.current.Status.IncrementMinorVersion("trying minor")
case "/setAllBranchesToMaster":
msg(w, "set all branches to master")
me.Disable()
defer me.Enable()
if setAllBranchesToMaster() {
// if it succeeds, disable this button
me.setBranchesToMasterB.Disable()
}
return
case "/setTargetVersion":
msg(w, "repo: "+repoName)
msg(w, "version: "+version)
if me.current == nil {
msg(w, "me.current == nil")
return
}
me.current.Status.SetTargetVersion(version)
case "/setAllTargetVersions":
msg(w, "version: "+version)
loop := me.repos.View.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
master := repo.Status.GetMasterVersion()
lastTag := repo.Status.LastTag()
if master == lastTag {
repo.Status.SetTargetVersion(master)
} else {
repo.Status.IncrementRevisionVersion("Nov 2024 test")
}
}
case "/whitelist":
repo := me.repos.View.FindRepoByName(repoName)
if repo == nil {
msg(w, "FindRepoByName() returned nil")
return
}
msg(w, "whitelisted "+repo.Name())
if whitelist == "false" {
repo.Status.Whitelist = false
} else {
repo.Status.Whitelist = true
}
case "/showNext":
msg(w, "gui repo: "+me.release.repo.String())
msg(w, "gui name: "+me.release.version.String())
msg(w, "gui notes: "+me.release.notes.String())
msg(w, "gui status: "+me.release.status.String())
msg(w, "")
if checkValidGoSum(me.current) {
msg(w, "checkValidGoSum() == true")
} else {
msg(w, "checkValidGoSum() == false SHOULD NOT RELEASE THIS")
}
if me.current.Status.IsReleased() {
msg(w, "IsReleased() == true SHOULD NOT RELEASE THIS")
} else {
msg(w, "IsReleased() == false")
}
if me.current.Status.CheckDirty() {
msg(w, "CheckDirty() == true. SHOULD NOT RELEASE THIS")
} else {
msg(w, "CheckDirty() == false")
}
if me.current.Status.IsPrimitive() {
msg(w, "IsPrimitive() == true")
} else {
msg(w, "IsPrimitive() == false")
}
if me.current.Status.IsProtobuf() {
msg(w, "IsProtobuf() == true")
} else {
msg(w, "IsProtobuf() == false")
}
if me.current.Status.Whitelist {
msg(w, "Whitelist == true SHOULD NOT RELEASE THIS")
} else {
msg(w, "Whitelist == false")
}
msg(w, "")
msg(w, repolist.ReportHeader())
msg(w, me.current.StandardHeader())
msg(w, "")
msg(w, repolist.ReleaseReportHeader())
msg(w, me.current.StandardReleaseHeader())
case "/list":
me.repos.View.PrintReport(w, readonly, onlydirty, perfect)
case "/releaseList":
me.repos.View.PrintReleaseReport(w, readonly, perfect)
case "/quit":
msg(w, "Got URL /quit")
os.Exit(0)
case "/goweblist":
loop := me.repos.View.ReposAll()
for loop.Scan() {
repo := loop.Repo()
lastTag := repo.LastTag()
tag := repo.Status.NewestTag()
gitAge, err := tag.GetDate()
if err != nil {
msg(w, fmt.Sprintf("tag date error", repo.Name()))
}
// if lastTag == "" {
// lastTag = tag.Name()
// }
if repo.ReadOnly() {
if readonly == "true" {
continue
}
}
// dur := time.Since(gitAge)
// msg(w, fmt.Sprintf("%-60s %s %s %s", repo.Name(), lastTag, shell.FormatDuration(dur), lastTag, tag.Name()))
msg(w, fmt.Sprintf("%s %d %s", repo.Name(), gitAge.Unix(), lastTag))
/*
for _, tag := range repo.Tags.ListAll() {
msg(w, fmt.Sprintf("%-60s %s", "", tag.Name()))
}
*/
}
default:
msg(w, "BAD URL = "+route)
}
}
// starts and sits waiting for HTTP requests
func startHTTP() {
http.HandleFunc("/", okHandler)
p := fmt.Sprintf(":%d", myargs.Port)
log.Println("Running on port", p)
err := http.ListenAndServe(p, nil)
if err != nil {
log.Println("Error starting server:", err)
}
}