summaryrefslogtreecommitdiff
path: root/http.go
blob: d6633b0c1bbecc175401e25cb59e004bf6eb2455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main

import (
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/protobuf/gitpb"
	"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)
	log.HttpMode(w)
	defer log.HttpMode(nil)

	// 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")

	switch route {
	case "/help":
		log.Info("list/                                       list modified repos")
		log.Info("list?readonly=true                          shows every repo")
		log.Info("")
		log.Info("doRelease                                   runs doRelease()")
		log.Info("findNext                                    runs findNext()")
		log.Info("showNext                                    shows the repo for doRelease()")
		log.Info("setTargetVersion                            set the target version for findNext()")
		log.Info("setAllTargetVersions?version=v0.12.4        set ever repo to target version")
		log.Info("setCurrentRepo?repo=go.wit.com/gui          runs setCurrentRepo(repo)")
		log.Info("")
		log.Info("setAllBranchesToMaster                      git checkout master on every repo")
		log.Info("")
		log.Info("setVersion?repo=go.wit.com/gui?target=0.2   attempts to set the target version to 0.2")
		log.Info("")
	case "/doRelease":
		buttonDisable()
		if err := doRelease(); err == nil {
			buttonEnable()
			log.Info("doRelease() worked")
		} else {
			log.Info("doRelease() failed")
		}
	case "/findNext":
		me.Disable()
		defer me.Enable()
		if findNext() {
			log.Info("findNext() found a repo")
		} else {
			log.Info("findNext() did not find a repo. You might be finished?")
		}
		log.Info("repo: " + me.release.repo.String())
		log.Info("name: " + me.release.version.String())
		log.Info("notes: " + me.release.notes.String())
		log.Info("status: " + me.release.status.String())

		if me.current == nil {
			log.Info("findNext() == nil")
			return
		}

		// log.Info(me.current.StandardHeader())
		log.Info(me.forge.StandardReleaseHeader(me.current, "todoing"))
	case "/rescanAll":
		me.repos.View.ScanRepositoriesOld()
	case "/setCurrentRepo":
		log.Info("repo: " + repoName)
		log.Info("version: " + version)
		log.Info("comment: " + comment)

		repo := me.forge.FindByGoPath(repoName)
		if repo == nil {
			log.Info("FindRepoByName() returned nil")
			return
		}

		setCurrentRepo(repo, "HTTP", "doRelease() ?")
		return
	case "/fixNext":
		check := me.forge.FindByGoPath(me.current.GetGoPath())
		if check == nil {
			log.Info("boo, you didn't git clone", me.current.GetGoPath())
			return
		}
		// destroy and recreate the go.sum
		fixGodepsOLD(check)
		findOk = true
		return
	case "/showNext":
		check := me.forge.FindByGoPath(me.current.GetGoPath())
		if check == nil {
			log.Info("boo, current is missing", me.current.GetGoPath())
			return
		}
		testGoRepo(check)
		me.forge.HumanPrintRepo(check)
		return
	case "/list":
		me.forge.PrintHumanTable(me.found)
		return
	case "/releaseList":
		me.forge.PrintHumanTable(me.found)
		return
	default:
		log.Info("BAD URL = " + route)
	}
}

func testGoRepo(check *gitpb.Repo) {
	data, _ := os.ReadFile(filepath.Join(check.FullPath, "go.mod"))
	log.Info(string(data))

	if err := me.forge.FinalGoDepsCheckOk(check, true); err == nil {
		log.Info("forge.FinalGoDepsCheck(check) worked!")
	} else {
		log.Info("forge.FinalGoDepsCheck(check) failed. boo.")
	}

}

// 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)
	}
}