2024-11-04 03:23:28 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.wit.com/lib/gui/repostatus"
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// remove '?' part and trailing '/'
|
|
|
|
func cleanURL(url string) string {
|
|
|
|
url = "/" + strings.Trim(url, "/")
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var route string
|
|
|
|
route = cleanURL(r.URL.Path)
|
2024-11-07 08:25:11 -06:00
|
|
|
log.HttpMode(w)
|
|
|
|
defer log.HttpMode(nil)
|
2024-11-04 03:23:28 -06:00
|
|
|
|
|
|
|
if route == "/help" {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info("uptime/ uptime uptime fun!")
|
|
|
|
log.Info("list/ list modified repos")
|
|
|
|
log.Info("list?readonly=true shows every repo")
|
|
|
|
log.Info("gitpull/ run a git pull in each repo")
|
|
|
|
|
|
|
|
log.Info("")
|
|
|
|
log.Info("Examples:")
|
|
|
|
log.Info("")
|
|
|
|
log.Info("not done yet:")
|
|
|
|
|
|
|
|
log.Info("changes/ show latest changes")
|
|
|
|
log.Info("repo?repo=go.wit.com/apps/autotypist show this repo")
|
|
|
|
log.Info("clone?repo=go.wit.com/apps/virtigo go-clone a new repo")
|
|
|
|
log.Info("build?repo=go.wit.com/apps/autotypist build the autotypist")
|
|
|
|
log.Info("debian?repo=go.wit.com/apps/autotypist make a debian package of the autotypist")
|
2024-11-04 03:23:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/uptime" {
|
|
|
|
s := "uptime uptime uptime fun!"
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(s)
|
2024-11-04 03:23:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:44:32 -06:00
|
|
|
if route == "/release" {
|
|
|
|
simpleRelease(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-04 03:23:28 -06:00
|
|
|
if route == "/gitpull" {
|
|
|
|
repoName := r.URL.Query().Get("repo")
|
|
|
|
if repoName != "" {
|
|
|
|
// git pull (or go-clone of it doesn't exist)
|
2024-11-22 21:04:56 -06:00
|
|
|
repo := me.repos.View.FindByName(repoName)
|
2024-11-04 03:23:28 -06:00
|
|
|
if repo == nil {
|
|
|
|
cmd := []string{"go-clone", repoName}
|
|
|
|
header := repo.StandardHeader()
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + strings.Join(cmd, " "))
|
2024-11-04 03:23:28 -06:00
|
|
|
shell.RunHttpOut(cmd, w, r)
|
|
|
|
} else {
|
|
|
|
cmd := []string{"git", "pull", "-v"}
|
|
|
|
header := repo.StandardHeader()
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + strings.Join(cmd, " "))
|
2024-11-04 03:23:28 -06:00
|
|
|
shell.RunPathHttpOut(repo.Status.FullPath(), cmd, w, r)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// git pull every repo. Ignore Dirty repos
|
|
|
|
loop := me.repos.View.ReposAll()
|
|
|
|
for loop.Scan() {
|
|
|
|
repo := loop.Repo()
|
|
|
|
cmd := []string{"git", "pull", "-v"}
|
|
|
|
header := repo.StandardHeader()
|
|
|
|
if repo.CheckDirty() {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "skip dirty repo")
|
2024-11-04 03:23:28 -06:00
|
|
|
continue
|
|
|
|
}
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + strings.Join(cmd, " "))
|
2024-11-04 03:23:28 -06:00
|
|
|
shell.RunPathHttpOut(repo.Status.FullPath(), cmd, w, r)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/list" {
|
|
|
|
readonly := r.URL.Query().Get("readonly")
|
2024-11-04 06:16:52 -06:00
|
|
|
onlydirty := r.URL.Query().Get("onlydirty")
|
2024-11-04 03:23:28 -06:00
|
|
|
perfect := r.URL.Query().Get("perfect")
|
|
|
|
var count int
|
|
|
|
|
|
|
|
loop := me.repos.View.ReposSortByName()
|
|
|
|
for loop.Scan() {
|
|
|
|
repo := loop.Repo()
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
header := repo.StandardHeader()
|
2024-11-04 06:16:52 -06:00
|
|
|
if onlydirty == "true" {
|
|
|
|
if repo.CheckDirty() {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "")
|
2024-11-04 06:16:52 -06:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-11-04 03:23:28 -06:00
|
|
|
|
|
|
|
if repo.ReadOnly() {
|
|
|
|
if readonly == "true" {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "readonly")
|
2024-11-04 03:23:28 -06:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if repo.State() == "PERFECT" {
|
|
|
|
if perfect == "false" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if repo.State() != "merge to main" {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "")
|
2024-11-04 03:23:28 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if repo.CheckDirty() {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "")
|
2024-11-04 03:23:28 -06:00
|
|
|
continue
|
|
|
|
}
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(header + "")
|
2024-11-04 03:23:28 -06:00
|
|
|
}
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(fmt.Sprintf("EVERYTHING WORKED repo count = %d", count))
|
2024-11-04 03:23:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if route == "/goweblist" {
|
|
|
|
readonly := r.URL.Query().Get("readonly")
|
|
|
|
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 {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(fmt.Sprintf("tag date error", repo.Name()))
|
2024-11-04 03:23:28 -06:00
|
|
|
}
|
|
|
|
// if lastTag == "" {
|
|
|
|
// lastTag = tag.Name()
|
|
|
|
// }
|
|
|
|
if repo.ReadOnly() {
|
|
|
|
if readonly == "true" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dur := time.Since(gitAge)
|
2024-11-07 08:25:11 -06:00
|
|
|
// log.Info(fmt.Sprintf("%-60s %s %s %s", repo.Name(), lastTag, shell.FormatDuration(dur), lastTag, tag.Name()))
|
|
|
|
log.Info(fmt.Sprintf("%s %d %s", repo.Name(), gitAge.Unix(), lastTag))
|
2024-11-04 03:23:28 -06:00
|
|
|
/*
|
|
|
|
for _, tag := range repo.Tags.ListAll() {
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(fmt.Sprintf("%-60s %s", "", tag.Name()))
|
2024-11-04 03:23:28 -06:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-15 20:06:17 -06:00
|
|
|
// turn off timestamps for STDOUT (systemd adds them)
|
|
|
|
if route == "/daemon" {
|
|
|
|
log.Info("daemon mode ???")
|
|
|
|
log.DaemonMode(false)
|
|
|
|
log.Info("daemon mode false")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-04 03:23:28 -06:00
|
|
|
if route == "/listGitDirectories" {
|
|
|
|
for i, path := range repostatus.ListGitDirectories() {
|
|
|
|
// log.Info("addRepo()", i, path)
|
|
|
|
path = strings.TrimPrefix(path, me.goSrcPwd.String())
|
|
|
|
path = strings.Trim(path, "/")
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(fmt.Sprintf("paths:", i, path))
|
2024-11-04 03:23:28 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/repo" {
|
|
|
|
reponame := r.URL.Query().Get("repo")
|
|
|
|
if reponame == "" {
|
|
|
|
s := "reponame is blank " + cleanURL(r.URL.Path)
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(s)
|
2024-11-04 03:23:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s := "reponame is " + reponame
|
2024-11-07 08:25:11 -06:00
|
|
|
log.Info(s)
|
2024-11-04 03:23:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/quit" {
|
|
|
|
log.Warn("writing out config file and exiting virtigo")
|
|
|
|
fmt.Fprintln(w, "writing out config file and exiting virtigo")
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/favicon.ico" {
|
|
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/goReference.svg" {
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
writeFile(w, "goReference.svg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("BAD URL =", route)
|
|
|
|
fmt.Fprintln(w, "BAD URL =", route)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write a file out to the http socket
|
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
|
|
fullname := "resources/" + filename
|
|
|
|
pfile, err := resources.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
// w.Write(pfile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var repohtml string
|
|
|
|
repohtml = string(pfile)
|
|
|
|
if filename == "goReference.svg" {
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, repohtml)
|
|
|
|
log.Println("writeFile() found internal file:", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts and sits waiting for HTTP requests
|
|
|
|
func startHTTP() {
|
|
|
|
http.HandleFunc("/", okHandler)
|
|
|
|
|
2024-11-05 07:48:22 -06:00
|
|
|
p := fmt.Sprintf(":%d", myargv.Port)
|
2024-11-04 03:23:28 -06:00
|
|
|
log.Println("Running on port", p)
|
|
|
|
|
|
|
|
err := http.ListenAndServe(p, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error starting server:", err)
|
|
|
|
}
|
|
|
|
}
|