Embed version info, display in CLI and web

Amolith created

Change summary

cmd/willow.go       | 10 +++++++++-
justfile            |  4 ++++
ws/static/home.html | 12 ++++++------
ws/ws.go            | 14 +++++++++++++-
4 files changed, 32 insertions(+), 8 deletions(-)

Detailed changes

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()

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/

ws/static/home.html 🔗

@@ -34,13 +34,13 @@
             <div class="wrapper two_column">
             <div class="projects">
                 <!-- Range through projects that aren't yet up-to-date -->
-                {{- range . -}}
+                {{- range .Projects -}}
                 {{- if ne .Running (index .Releases 0).Tag -}}
                 <h2>Outdated projects</h2>
                 {{- break -}}
                 {{- end -}}
                 {{- end -}}
-                {{- range . -}}
+                {{- range .Projects -}}
                 {{- if ne .Running (index .Releases 0).Tag -}}
                 <div id="{{ .ID }}" class="project card">
                     <h3><a href="{{ .URL }}">{{ .Name }}</a>&nbsp;&nbsp;&nbsp;<span class="delete"><a href="/new?action=delete&id={{ .ID }}">Delete?</a></span></h3>
@@ -52,13 +52,13 @@
                 {{- end -}}
 
                 <!-- Range through projects that _are_ up-to-date -->
-                {{- range . -}}
+                {{- range .Projects -}}
                 {{- if eq .Running (index .Releases 0).Tag -}}
                 <h2>Up-to-date projects</h2>
                 {{- break -}}
                 {{- end -}}
                 {{- end -}}
-                {{- range . -}}
+                {{- range .Projects -}}
                 {{- if eq .Running (index .Releases 0).Tag -}}
                 <div class="project card">
                     <h3><a href="{{ .URL }}">{{ .Name }}</a>&nbsp;&nbsp;&nbsp;<span class="delete"><a href="/new?action=delete&id={{ .ID }}">Delete?</a></span></h3>
@@ -69,7 +69,7 @@
             </div>
             <div class="release_notes">
                 <h2>Release notes</h2>
-                {{- range . -}}
+                {{- range .Projects -}}
                 <div id="{{ (index .Releases 0).ID }}" class="release_note card">
                     <h3>{{ .Name }}: release notes for <a href="{{ (index .Releases 0).URL }}">{{ (index .Releases 0).Tag }}</a></h3>
                     {{- if eq .Forge "github" "gitea" "forgejo" -}}
@@ -88,7 +88,7 @@
         </main>
         <footer>
             <div class="wrapper">
-                <p>Willow &mdash; <a href="https://getwillow.org">Source code</a> &mdash; <a href="https://todo.sr.ht/~amolith/willow">Issue queue</a></p>
+              <p>Willow {{ .Version }} &mdash; <a href="https://getwillow.org">Source code</a> &mdash; <a href="https://todo.sr.ht/~amolith/willow">Issue queue</a></p>
             </div>
         </footer>
         </div>

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)