From 90596c71f085d40da797e5027014c310c9f96e50 Mon Sep 17 00:00:00 2001 From: Amolith Date: Sat, 3 Aug 2024 16:30:11 -0700 Subject: [PATCH] Embed version info, display in CLI and web --- cmd/willow.go | 10 +++++++++- justfile | 4 ++++ ws/static/home.html | 12 ++++++------ ws/ws.go | 14 +++++++++++++- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cmd/willow.go b/cmd/willow.go index bb12fc163f9bdffd5a666a3ab5684b045caedde6..337fa29258c88891a8dc941b192c3965bbcf72d8 100644 --- a/cmd/willow.go +++ b/cmd/willow.go @@ -39,8 +39,10 @@ var ( flagConfig = flag.StringP("config", "c", "config.toml", "Path to config file") flagAddUser = flag.StringP("add", "a", "", "Username of account to add") flagDeleteUser = flag.StringP("deleteuser", "d", "", "Username of account to delete") - flagCheckAuthorised = flag.StringP("validatecredentials", "v", "", "Username of account to check") + flagCheckAuthorised = flag.StringP("validatecredentials", "V", "", "Username of account to check") flagListUsers = flag.BoolP("listusers", "l", false, "List all users") + flagShowVersion = flag.BoolP("version", "v", false, "Print Willow's version") + version = "" config Config req = make(chan struct{}) res = make(chan []project.Project) @@ -50,6 +52,11 @@ var ( func main() { flag.Parse() + if *flagShowVersion { + fmt.Println(version) + os.Exit(0) + } + err := checkConfig() if err != nil { log.Fatalln(err) @@ -101,6 +108,7 @@ func main() { Res: &res, ManualRefresh: &manualRefresh, Mu: &mu, + Version: &version, } mux := http.NewServeMux() diff --git a/justfile b/justfile index a8b7320e8e1ec8f72c24160fcf4609d21edfed52..953900891b9e7662fa6c7183e18b0b7422518f02 100644 --- a/justfile +++ b/justfile @@ -31,6 +31,10 @@ reuse: # Linting licenses and copyright headers reuse lint +build: + # Building Willow + go build -o willow -ldflags "-s -w -X main.version=`git describe --long 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g'`" ./cmd + clean: # Cleaning up rm -rf willow out/ diff --git a/ws/static/home.html b/ws/static/home.html index 65b899635180910a4a0de4ce1a667967c8963348..6086e72d4e8b2bba590ef61e8c16ddb7f1741d85 100644 --- a/ws/static/home.html +++ b/ws/static/home.html @@ -34,13 +34,13 @@
- {{- range . -}} + {{- range .Projects -}} {{- if ne .Running (index .Releases 0).Tag -}}

Outdated projects

{{- break -}} {{- end -}} {{- end -}} - {{- range . -}} + {{- range .Projects -}} {{- if ne .Running (index .Releases 0).Tag -}}

{{ .Name }}   Delete?

@@ -52,13 +52,13 @@ {{- end -}} - {{- range . -}} + {{- range .Projects -}} {{- if eq .Running (index .Releases 0).Tag -}}

Up-to-date projects

{{- break -}} {{- end -}} {{- end -}} - {{- range . -}} + {{- range .Projects -}} {{- if eq .Running (index .Releases 0).Tag -}}

{{ .Name }}   Delete?

@@ -69,7 +69,7 @@

Release notes

- {{- range . -}} + {{- range .Projects -}}

{{ .Name }}: release notes for {{ (index .Releases 0).Tag }}

{{- if eq .Forge "github" "gitea" "forgejo" -}} @@ -88,7 +88,7 @@
diff --git a/ws/ws.go b/ws/ws.go index 7eccc4f4563f9c46715b0e926849380dcb30fb58..6ac2f64f77843367d7f812b3b1cb2ba91cde51ef 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -27,6 +27,7 @@ type Handler struct { ManualRefresh *chan struct{} Res *chan []project.Project Mu *sync.Mutex + Version *string } //go:embed static @@ -40,7 +41,7 @@ func (h Handler) RootHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/login", http.StatusSeeOther) return } - data, err := project.GetProjectsWithReleases(h.DbConn, h.Mu) + projectsWithReleases, err := project.GetProjectsWithReleases(h.DbConn, h.Mu) if err != nil { fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) @@ -50,6 +51,17 @@ func (h Handler) RootHandler(w http.ResponseWriter, r *http.Request) { } return } + + type stuff struct { + Version string + Projects []project.Project + } + + data := stuff{ + Version: *h.Version, + Projects: projectsWithReleases, + } + tmpl := template.Must(template.ParseFS(fs, "static/home.html")) if err := tmpl.Execute(w, data); err != nil { fmt.Println(err)