v2.31.0-dev12 6a38a35

previewstore.go RawHistoryFolder

1package filesystem
2
3import (
4	"context"
5	"encoding/json"
6	"errors"
7	"io/fs"
8	"os"
9	"path/filepath"
10	"sync"
11
12	"github.com/bnema/gordon/internal/domain"
13)
14
15type previewStoreData struct {
16	Previews []domain.PreviewRoute `json:"previews"`
17}
18
19// PreviewStore persists preview routes to a JSON file.
20type PreviewStore struct {
21	path string
22	mu   sync.Mutex
23}
24
25// NewPreviewStore creates a new filesystem-backed preview store.
26func NewPreviewStore(path string) *PreviewStore {
27	return &PreviewStore{path: path}
28}
29
30func (s *PreviewStore) Load(_ context.Context) ([]domain.PreviewRoute, error) {
31	s.mu.Lock()
32	defer s.mu.Unlock()
33
34	data, err := os.ReadFile(s.path)
35	if err != nil {
36		if errors.Is(err, fs.ErrNotExist) {
37			return nil, nil
38		}
39		return nil, err
40	}
41
42	var store previewStoreData
43	if err := json.Unmarshal(data, &store); err != nil {
44		return nil, err
45	}
46	return store.Previews, nil
47}
48
49func (s *PreviewStore) Save(_ context.Context, previews []domain.PreviewRoute) error {
50	s.mu.Lock()
51	defer s.mu.Unlock()
52
53	if len(previews) == 0 {
54		err := os.Remove(s.path)
55		if errors.Is(err, fs.ErrNotExist) {
56			return nil
57		}
58		return err
59	}
60
61	data, err := json.MarshalIndent(previewStoreData{Previews: previews}, "", "  ")
62	if err != nil {
63		return err
64	}
65
66	// Atomic write: temp file → rename
67	dir := filepath.Dir(s.path)
68	if err := os.MkdirAll(dir, 0o750); err != nil {
69		return err
70	}
71	tmp, err := os.CreateTemp(dir, ".previews-*.json.tmp")
72	if err != nil {
73		return err
74	}
75	tmpName := tmp.Name()
76
77	if _, err := tmp.Write(data); err != nil {
78		tmp.Close()
79		os.Remove(tmpName)
80		return err
81	}
82	if err := tmp.Sync(); err != nil {
83		tmp.Close()
84		os.Remove(tmpName)
85		return err
86	}
87	if err := tmp.Close(); err != nil {
88		os.Remove(tmpName)
89		return err
90	}
91	return os.Rename(tmpName, s.path)
92}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close