From 8ca0376243e3ced253a8a2045a486e51082057ac Mon Sep 17 00:00:00 2001 From: Amolith Date: Tue, 28 Nov 2023 20:01:26 -0500 Subject: [PATCH] Pre-select current $running when modifying it Implements: https://todo.sr.ht/~amolith/willow/25 --- project/project.go | 8 ++++-- ws/static/select-release.html | 5 ++-- ws/ws.go | 52 ++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/project/project.go b/project/project.go index 888b29f..a80994d 100644 --- a/project/project.go +++ b/project/project.go @@ -196,12 +196,14 @@ func RefreshLoop(dbConn *sql.DB, interval int, manualRefresh, req *chan struct{} // GetProject returns a project from the database func GetProject(dbConn *sql.DB, url string) (Project, error) { - var p Project projectDB, err := db.GetProject(dbConn, url) if err != nil { - return p, err + return Project{}, err } - p = Project{ + if len(projectDB) == 0 { + return Project{}, nil + } + p := Project{ URL: projectDB["url"], Name: projectDB["name"], Forge: projectDB["forge"], diff --git a/ws/static/select-release.html b/ws/static/select-release.html index ef8d75b..7c962a6 100644 --- a/ws/static/select-release.html +++ b/ws/static/select-release.html @@ -24,8 +24,9 @@

Which release of {{ .Name }} are you currently running?

{{- $url := .URL -}} {{- $forge := .Forge -}} + {{- $running := .Running -}} {{- range .Releases -}} - + {{- if ne .URL "" -}}
{{- else -}} @@ -44,7 +45,7 @@ - + {{- if or (eq $forge "github") -}} Some RSS feeds (notably GitHub's) include a limited number of releases. If you don't see your version, please change the forge type to "Other". {{- end -}} diff --git a/ws/ws.go b/ws/ws.go index 3a43292..a4c1486 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -73,35 +73,48 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) { return } - forge := bmStrict.Sanitize(params.Get("forge")) - if forge == "" { + proj, err := project.GetProject(h.DbConn, submittedURL) + if err != nil { w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("No forge provided")) + _, err := w.Write([]byte(fmt.Sprintf("Error getting project: %s", err))) if err != nil { fmt.Println(err) } return } - name := bmStrict.Sanitize(params.Get("name")) - if name == "" { - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("No name provided")) - if err != nil { - fmt.Println(err) + if proj.Running == "" { + forge := bmStrict.Sanitize(params.Get("forge")) + if forge == "" { + w.WriteHeader(http.StatusBadRequest) + _, err := w.Write([]byte("No forge provided")) + if err != nil { + fmt.Println(err) + } + return } - return + + name := bmStrict.Sanitize(params.Get("name")) + if name == "" { + w.WriteHeader(http.StatusBadRequest) + _, err := w.Write([]byte("No name provided")) + if err != nil { + fmt.Println(err) + } + return + } + + proj = project.Project{ + URL: submittedURL, + Name: name, + Forge: forge, + } + + proj.URL = strings.TrimSuffix(proj.URL, ".git") + } - proj := project.Project{ - URL: submittedURL, - Name: name, - Forge: forge, - } - - proj.URL = strings.TrimSuffix(proj.URL, ".git") - - proj, err := project.GetReleases(h.DbConn, proj) + proj, err = project.GetReleases(h.DbConn, proj) if err != nil { w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte(fmt.Sprintf("Error getting releases: %s", err))) @@ -110,6 +123,7 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) { } return } + tmpl := template.Must(template.ParseFS(fs, "static/select-release.html")) if err := tmpl.Execute(w, proj); err != nil { fmt.Println(err)