Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ Everything happens in `main()` in `main.go`, in a straight-line pipeline:

### Temp file / Snap sandboxing (`getTempDir`, `isSnap`)

Output location resolution order: `MDVIEW_DIR` env var → (if running as a Snap, per `SNAP_USER_COMMON` env var) `~/mdview-temp` → OS default temp dir. `MDVIEW_DIR` and `~/mdview-temp` are created with `MkdirAll`. Before writing a temp file, `removeStaleTempFiles` deletes generated `mdview<32 hex>.html` files older than 24 hours from that directory (the browser reads the new file after mdview exits, so mdview cannot delete it right away). This exists because Snap-sandboxed browsers (e.g. Snap Firefox) can't read from `/tmp`, only from the user's home directory — `check()` writes errors to stderr, names the failing path, and prints a Snap-specific hint for permission and not-found errors for this reason.
Output location resolution order: `MDVIEW_DIR` env var → (if running as a Snap, per `SNAP_USER_COMMON` env var) `~/mdview-temp` (the home directory comes from `SNAP_REAL_HOME`, because snapd remaps `HOME` to the snap's private data directory) → OS default temp dir. `MDVIEW_DIR` and `~/mdview-temp` are created with `MkdirAll`. Before writing a temp file, `removeStaleTempFiles` deletes generated `mdview<32 hex>.html` files older than 24 hours from that directory (the browser reads the new file after mdview exits, so mdview cannot delete it right away). This exists because Snap-sandboxed browsers (e.g. Snap Firefox) can't read from `/tmp`, only from the user's home directory — `check()` writes errors to stderr, names the failing path, and prints a Snap-specific hint for permission and not-found errors for this reason.

The snap (`snap/snapcraft.yaml`, `base: core24`, `platforms:` instead of `architectures:`) plugs `home` and
`desktop`. `desktop` (auto-connected) lets `xdg-open` launch the default browser; it replaced the transitional
`unity7` plug.

### Vendored `mermaid.min.js`

Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ func getTempDir() string {
}

if isSnap() {
var tmpdir = filepath.Join(os.Getenv("HOME"), "mdview-temp")
// snapd points HOME at the snap's private data directory, so use the real home directory
home := os.Getenv("SNAP_REAL_HOME")
if home == "" {
home = os.Getenv("HOME")
}
var tmpdir = filepath.Join(home, "mdview-temp")
check(os.MkdirAll(tmpdir, 0700))
return tmpdir
}
Expand Down
13 changes: 13 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ func TestGetTempDir(t *testing.T) {
home := t.TempDir()
t.Setenv("MDVIEW_DIR", "")
t.Setenv("SNAP_USER_COMMON", "/snap/common")
t.Setenv("HOME", filepath.Join(home, "snap", "mdview", "1"))
t.Setenv("SNAP_REAL_HOME", home)
want := filepath.Join(home, "mdview-temp")
if got := getTempDir(); got != want {
t.Fatalf("getTempDir = %q, want %q", got, want)
}
})

t.Run("snap without SNAP_REAL_HOME falls back to HOME", func(t *testing.T) {
home := t.TempDir()
t.Setenv("MDVIEW_DIR", "")
t.Setenv("SNAP_USER_COMMON", "/snap/common")
t.Setenv("SNAP_REAL_HOME", "")
t.Setenv("HOME", home)
want := filepath.Join(home, "mdview-temp")
if got := getTempDir(); got != want {
Expand Down
17 changes: 6 additions & 11 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ description: |
Type `mdview --help` for options. https://github.com/mapitman/mdview
grade: stable
confinement: strict
base: core22
base: core24
apps:
mdview:
command: bin/mdview
plugs: [home,unity7]
environment:
HOME: /home/$USER
plugs: [home, desktop]
adopt-info: mdview
parts:
mdview:
Expand All @@ -29,10 +27,7 @@ parts:
# - libblkid-dev
build-snaps:
- go
architectures:
- build-on: [amd64]
build-for: [amd64]
- build-on: [arm64]
build-for: [arm64]
- build-on: [armhf]
build-for: [armhf]
platforms:
amd64:
arm64:
armhf:
Loading