From 61ea43c88aee6e676e8f93b43f5ce833b842eccd Mon Sep 17 00:00:00 2001
From: Michael Lynch
Date: Fri, 2 Feb 2024 22:18:43 -0500
Subject: [PATCH 01/10] Print version information in app
---
.gitignore | 1 -
build/build.go | 21 +++++++++++++++++++++
dev-scripts/build-backend | 4 +++-
flake.nix | 11 ++++++++++-
handlers/templates/pages/disk-usage.html | 7 +++++++
handlers/views.go | 6 ++++++
modd.conf | 2 +-
7 files changed, 48 insertions(+), 4 deletions(-)
create mode 100644 build/build.go
diff --git a/.gitignore b/.gitignore
index c902e3d6..e1a9c027 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,6 @@
# Compiled binaries
bin/
-build/
dist/
# Persistent data directory
diff --git a/build/build.go b/build/build.go
new file mode 100644
index 00000000..a128e8d6
--- /dev/null
+++ b/build/build.go
@@ -0,0 +1,21 @@
+package build
+
+import (
+ "log"
+ "strconv"
+ "time"
+)
+
+// These values are set by ldflags at build time.
+// https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications
+var unixTime string
+var Version string
+
+func Time() time.Time {
+ t, err := strconv.ParseInt(unixTime, 10, 64)
+ if err != nil {
+ log.Printf("no build time specified through ldflags")
+ return time.Time{}
+ }
+ return time.Unix(t, 0)
+}
diff --git a/dev-scripts/build-backend b/dev-scripts/build-backend
index 73d16a5f..a5a4a23c 100755
--- a/dev-scripts/build-backend
+++ b/dev-scripts/build-backend
@@ -60,8 +60,10 @@ readonly BUILD_TAGS_JOINED
# cgo is required for mattn/go-sqlite3.
export CGO_ENABLED=1
+PS_VERSION="$(git describe --tags)"
+
go build \
-tags "${BUILD_TAGS_JOINED}" \
- -ldflags '-w -extldflags "-static"' \
+ -ldflags "-w -extldflags '-static' -X 'github.com/mtlynch/picoshare/v2/build.Version=${PS_VERSION}' -X 'github.com/mtlynch/picoshare/v2/build.unixTime=$(date +%s)'" \
-o "${BINARY}" \
cmd/picoshare/main.go
diff --git a/flake.nix b/flake.nix
index a245e977..656a4641 100644
--- a/flake.nix
+++ b/flake.nix
@@ -28,8 +28,14 @@
{
devShells.default = go_dep.mkShell.override { stdenv = go_dep.pkgsStatic.stdenv; } {
packages = [
- go_dep.gopls
go_dep.gotools
+ go_dep.gopls
+ go_dep.go-outline
+ go_dep.gocode
+ go_dep.gopkgs
+ go_dep.gocode-gomod
+ go_dep.godef
+ go_dep.golint
go_dep.go_1_21
nodejs_dep.nodejs_20
shellcheck_dep.shellcheck
@@ -37,6 +43,9 @@
];
shellHook = ''
+ GOROOT="$(dirname $(dirname $(which go)))/share/go"
+ export GOROOT
+
echo "shellcheck" "$(shellcheck --version | grep '^version:')"
sqlfluff --version
echo "node" "$(node --version)"
diff --git a/handlers/templates/pages/disk-usage.html b/handlers/templates/pages/disk-usage.html
index 2cf498d5..ebb4e183 100644
--- a/handlers/templates/pages/disk-usage.html
+++ b/handlers/templates/pages/disk-usage.html
@@ -24,6 +24,8 @@
{{ define "content" }}
Disk Usage
+
+
+
+ Build info
+
+ Build time: {{ .BuildTime }}
+ Version: {{ .Version }}
{{ end }}
{{ template "base.html" }}
diff --git a/handlers/views.go b/handlers/views.go
index 3b4a957f..19e0b388 100644
--- a/handlers/views.go
+++ b/handlers/views.go
@@ -14,6 +14,7 @@ import (
"github.com/gorilla/mux"
"github.com/mileusna/useragent"
+ "github.com/mtlynch/picoshare/v2/build"
"github.com/mtlynch/picoshare/v2/handlers/parse"
"github.com/mtlynch/picoshare/v2/picoshare"
"github.com/mtlynch/picoshare/v2/store"
@@ -558,14 +559,19 @@ func (s Server) diskUsageGet() http.HandlerFunc {
http.Error(w, fmt.Sprintf("failed to check available space: %v", err), http.StatusInternalServerError)
return
}
+
if err := renderTemplate(w, "disk-usage.html", struct {
commonProps
UsedBytes uint64
TotalBytes uint64
+ BuildTime time.Time
+ Version string
}{
commonProps: makeCommonProps("PicoShare - Disk Usage", r.Context()),
UsedBytes: space.TotalBytes - space.AvailableBytes,
TotalBytes: space.TotalBytes,
+ BuildTime: build.Time(),
+ Version: build.Version,
}, template.FuncMap{
"formatFileSize": humanReadableFileSize,
"percentage": func(part, total uint64) string {
diff --git a/modd.conf b/modd.conf
index 65c68d18..ff7f8fa1 100644
--- a/modd.conf
+++ b/modd.conf
@@ -1,3 +1,3 @@
**/*.go !**/*_test.go **/*.html **/*.js **/*.css {
- daemon: go build --tags 'dev' -o ./bin/picoshare-dev cmd/picoshare/main.go && PS_SHARED_SECRET=somepassword ./bin/picoshare-dev -db data/store.db
+ daemon: dev-scripts/build-backend dev && PS_SHARED_SECRET=somepassword ./bin/picoshare-dev -db data/store.db
}
From 04fcdef20e4a9d1b09cbd0675c419395bd1d8bc4 Mon Sep 17 00:00:00 2001
From: Michael Lynch
Date: Fri, 2 Feb 2024 22:22:36 -0500
Subject: [PATCH 02/10] work in progress
---
handlers/templates/pages/disk-usage.html | 2 --
1 file changed, 2 deletions(-)
diff --git a/handlers/templates/pages/disk-usage.html b/handlers/templates/pages/disk-usage.html
index ebb4e183..b62676db 100644
--- a/handlers/templates/pages/disk-usage.html
+++ b/handlers/templates/pages/disk-usage.html
@@ -24,8 +24,6 @@
{{ define "content" }}
Disk Usage
-
-