v0.21.0 cd9706d

cache.go RawHistoryFolder

1package provider
2
3import (
4	"encoding/json"
5	"errors"
6	"os"
7	"path/filepath"
8	"time"
9)
10
11// ModelCache is the on-disk shape for discovered models.
12type ModelCache struct {
13	FetchedAt              time.Time         `json:"fetched_at"`
14	Models                 []Model           `json:"models"`
15	AuthoritativeProviders []string          `json:"authoritative_providers,omitempty"`
16	ProviderScopes         map[string]string `json:"provider_scopes,omitempty"`
17}
18
19// CacheTTL is how long a discovered list is considered fresh.
20const CacheTTL = 6 * time.Hour
21
22// LoadCache reads the model cache from path. Returns an empty ModelCache
23// (no error) if the file does not exist.
24func LoadCache(path string) (ModelCache, error) {
25	var c ModelCache
26	b, err := os.ReadFile(path)
27	if errors.Is(err, os.ErrNotExist) {
28		return c, nil
29	}
30	if err != nil {
31		return c, err
32	}
33	if err := json.Unmarshal(b, &c); err != nil {
34		return c, err
35	}
36	for i := range c.Models {
37		if c.Models[i].Source == "" {
38			c.Models[i].Source = "cache"
39		}
40	}
41	return c, nil
42}
43
44// SaveCache writes the cache atomically.
45func SaveCache(path string, c ModelCache) error {
46	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
47		return err
48	}
49	b, err := json.MarshalIndent(c, "", "  ")
50	if err != nil {
51		return err
52	}
53	tmp := path + ".tmp"
54	if err := os.WriteFile(tmp, b, 0o600); err != nil {
55		return err
56	}
57	return os.Rename(tmp, path)
58}
59
60// IsFresh reports whether the cache was fetched within CacheTTL.
61func (c ModelCache) IsFresh() bool {
62	if c.FetchedAt.IsZero() {
63		return false
64	}
65	return time.Since(c.FetchedAt) < CacheTTL
66}

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 1

Tags 476

↑↓ moveenter openesc close