246 lines
5.7 KiB
Go
246 lines
5.7 KiB
Go
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
|
|
}
|
|
|
|
// 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)
|
|
|
|
if route == "/help" {
|
|
msg(w, "uptime/ uptime uptime fun!")
|
|
msg(w, "list/ list modified repos")
|
|
msg(w, "list?readonly=true shows every repo")
|
|
msg(w, "gitpull/ run a git pull in each repo")
|
|
|
|
msg(w, "")
|
|
msg(w, "Examples:")
|
|
msg(w, "")
|
|
msg(w, "not done yet:")
|
|
|
|
msg(w, "changes/ show latest changes")
|
|
msg(w, "repo?repo=go.wit.com/apps/autotypist show this repo")
|
|
msg(w, "clone?repo=go.wit.com/apps/virtigo go-clone a new repo")
|
|
msg(w, "build?repo=go.wit.com/apps/autotypist build the autotypist")
|
|
msg(w, "debian?repo=go.wit.com/apps/autotypist make a debian package of the autotypist")
|
|
return
|
|
}
|
|
|
|
if route == "/uptime" {
|
|
s := "uptime uptime uptime fun!"
|
|
msg(w, s)
|
|
return
|
|
}
|
|
|
|
if route == "/release" {
|
|
simpleRelease(w, r)
|
|
return
|
|
}
|
|
|
|
if route == "/gitpull" {
|
|
repoName := r.URL.Query().Get("repo")
|
|
if repoName != "" {
|
|
// git pull (or go-clone of it doesn't exist)
|
|
repo := me.repos.View.FindRepoByName(repoName)
|
|
if repo == nil {
|
|
cmd := []string{"go-clone", repoName}
|
|
header := repo.StandardHeader()
|
|
msg(w, header+strings.Join(cmd, " "))
|
|
shell.RunHttpOut(cmd, w, r)
|
|
} else {
|
|
cmd := []string{"git", "pull", "-v"}
|
|
header := repo.StandardHeader()
|
|
msg(w, header+strings.Join(cmd, " "))
|
|
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() {
|
|
msg(w, header+"skip dirty repo")
|
|
continue
|
|
}
|
|
msg(w, header+strings.Join(cmd, " "))
|
|
shell.RunPathHttpOut(repo.Status.FullPath(), cmd, w, r)
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/list" {
|
|
readonly := r.URL.Query().Get("readonly")
|
|
onlydirty := r.URL.Query().Get("onlydirty")
|
|
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()
|
|
if onlydirty == "true" {
|
|
if repo.CheckDirty() {
|
|
msg(w, header+"")
|
|
}
|
|
continue
|
|
}
|
|
|
|
if repo.ReadOnly() {
|
|
if readonly == "true" {
|
|
msg(w, header+"readonly")
|
|
}
|
|
continue
|
|
}
|
|
if repo.State() == "PERFECT" {
|
|
if perfect == "false" {
|
|
continue
|
|
}
|
|
}
|
|
if repo.State() != "merge to main" {
|
|
msg(w, header+"")
|
|
continue
|
|
}
|
|
if repo.CheckDirty() {
|
|
msg(w, header+"")
|
|
continue
|
|
}
|
|
msg(w, header+"")
|
|
}
|
|
msg(w, fmt.Sprintf("EVERYTHING WORKED repo count = %d", count))
|
|
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 {
|
|
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()))
|
|
}
|
|
*/
|
|
|
|
}
|
|
return
|
|
}
|
|
|
|
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, "/")
|
|
msg(w, fmt.Sprintf("paths:", i, path))
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/repo" {
|
|
reponame := r.URL.Query().Get("repo")
|
|
if reponame == "" {
|
|
s := "reponame is blank " + cleanURL(r.URL.Path)
|
|
msg(w, s)
|
|
return
|
|
}
|
|
s := "reponame is " + reponame
|
|
msg(w, s)
|
|
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)
|
|
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
log.Println("Running on port", p)
|
|
|
|
err := http.ListenAndServe(p, nil)
|
|
if err != nil {
|
|
log.Println("Error starting server:", err)
|
|
}
|
|
}
|