gunk-dev/the-valley

registry.go

source60ad80d5a5a5main's published tip history
5.9 KiB
1package project
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9)
10
11// Registry holds the project registry: a default projects directory
12// and a map of project name to local path.
13type Registry struct {
14 ProjectsDir string `json:"projects_dir"`
15 Projects map[string]string `json:"projects"`
16 Descriptions map[string]string `json:"descriptions,omitempty"`
17}
18
19// registryPath returns the path to ~/.klaus/projects.json.
20func registryPath() (string, error) {
21 home, err := os.UserHomeDir()
22 if err != nil {
23 return "", fmt.Errorf("resolving home dir: %w", err)
24 }
25 return filepath.Join(home, ".klaus", "projects.json"), nil
26}
27
28// ExpandHome expands a leading ~ in a path to the user's home directory.
29func ExpandHome(path string) (string, error) {
30 if path == "~" || strings.HasPrefix(path, "~/") {
31 home, err := os.UserHomeDir()
32 if err != nil {
33 return "", fmt.Errorf("resolving home dir: %w", err)
34 }
35 return filepath.Join(home, path[1:]), nil
36 }
37 return path, nil
38}
39
40// contractHome replaces the user's home directory prefix with ~ for storage.
41func contractHome(path string) string {
42 home, err := os.UserHomeDir()
43 if err != nil {
44 return path
45 }
46 if path == home {
47 return "~"
48 }
49 if strings.HasPrefix(path, home+string(filepath.Separator)) {
50 return "~" + path[len(home):]
51 }
52 return path
53}
54
55// Load reads the registry from ~/.klaus/projects.json.
56// Returns an empty registry with defaults if the file doesn't exist.
57func Load() (*Registry, error) {
58 p, err := registryPath()
59 if err != nil {
60 return nil, err
61 }
62 return loadFrom(p)
63}
64
65// loadFrom reads a registry from the given path. Exported for testing via LoadFrom.
66func loadFrom(path string) (*Registry, error) {
67 data, err := os.ReadFile(path)
68 if err != nil {
69 if os.IsNotExist(err) {
70 return defaultRegistry()
71 }
72 return nil, fmt.Errorf("reading projects file: %w", err)
73 }
74
75 var reg Registry
76 if err := json.Unmarshal(data, &reg); err != nil {
77 return nil, fmt.Errorf("parsing projects file: %w", err)
78 }
79 if reg.Projects == nil {
80 reg.Projects = make(map[string]string)
81 }
82 if reg.Descriptions == nil {
83 reg.Descriptions = make(map[string]string)
84 }
85 return &reg, nil
86}
87
88// defaultRegistry returns a new registry with sensible defaults.
89func defaultRegistry() (*Registry, error) {
90 home, err := os.UserHomeDir()
91 if err != nil {
92 return nil, fmt.Errorf("resolving home dir: %w", err)
93 }
94 return &Registry{
95 ProjectsDir: filepath.Join(home, "src"),
96 Projects: make(map[string]string),
97 Descriptions: make(map[string]string),
98 }, nil
99}
100
101// Save writes the registry to ~/.klaus/projects.json.
102func (r *Registry) Save() error {
103 p, err := registryPath()
104 if err != nil {
105 return err
106 }
107 return r.saveTo(p)
108}
109
110// saveTo writes the registry to the given path.
111func (r *Registry) saveTo(path string) error {
112 dir := filepath.Dir(path)
113 if err := os.MkdirAll(dir, 0o755); err != nil {
114 return fmt.Errorf("creating config dir: %w", err)
115 }
116
117 // Store paths with ~ for readability
118 out := Registry{
119 ProjectsDir: contractHome(r.ProjectsDir),
120 Projects: make(map[string]string, len(r.Projects)),
121 }
122 for name, p := range r.Projects {
123 out.Projects[name] = contractHome(p)
124 }
125 out.Descriptions = r.Descriptions
126
127 data, err := json.MarshalIndent(out, "", " ")
128 if err != nil {
129 return fmt.Errorf("marshaling projects file: %w", err)
130 }
131 data = append(data, '\n')
132 return os.WriteFile(path, data, 0o644)
133}
134
135// Add registers a project with the given name and local path.
136// The path must be absolute. Returns an error if the name is already registered.
137func (r *Registry) Add(name, localPath string) error {
138 if _, exists := r.Projects[name]; exists {
139 return fmt.Errorf("project %q is already registered", name)
140 }
141 r.Projects[name] = localPath
142 return nil
143}
144
145// Remove unregisters a project by name. Returns an error if the name is not found.
146func (r *Registry) Remove(name string) error {
147 if _, exists := r.Projects[name]; !exists {
148 return fmt.Errorf("project %q is not registered", name)
149 }
150 delete(r.Projects, name)
151 delete(r.Descriptions, name)
152 return nil
153}
154
155// Describe sets or clears a freeform description for a registered project.
156// An empty description deletes any stored description for the name.
157// Returns an error if the project is not registered.
158func (r *Registry) Describe(name, description string) error {
159 if _, exists := r.Projects[name]; !exists {
160 return fmt.Errorf("project %q is not registered", name)
161 }
162 description = strings.Join(strings.Fields(description), " ")
163 if description == "" {
164 delete(r.Descriptions, name)
165 return nil
166 }
167 if r.Descriptions == nil {
168 r.Descriptions = make(map[string]string)
169 }
170 r.Descriptions[name] = description
171 return nil
172}
173
174// Description returns the stored description for a project, or "" if none.
175func (r *Registry) Description(name string) string {
176 return r.Descriptions[name]
177}
178
179// Get returns the local path for a project, expanding ~ if present.
180func (r *Registry) Get(name string) (string, bool) {
181 p, ok := r.Projects[name]
182 if !ok {
183 return "", false
184 }
185 expanded, err := ExpandHome(p)
186 if err != nil {
187 return p, true
188 }
189 return expanded, true
190}
191
192// List returns all registered projects as a map of name to expanded local path.
193func (r *Registry) List() map[string]string {
194 result := make(map[string]string, len(r.Projects))
195 for name, p := range r.Projects {
196 expanded, err := ExpandHome(p)
197 if err != nil {
198 expanded = p
199 }
200 result[name] = expanded
201 }
202 return result
203}
204
205// SetProjectsDir sets the default projects directory.
206func (r *Registry) SetProjectsDir(dir string) {
207 r.ProjectsDir = dir
208}
209
210// ExpandedProjectsDir returns the projects directory with ~ expanded.
211func (r *Registry) ExpandedProjectsDir() (string, error) {
212 return ExpandHome(r.ProjectsDir)
213}
214
215// LoadFrom reads a registry from the given path (for testing).
216func LoadFrom(path string) (*Registry, error) {
217 return loadFrom(path)
218}
219
220// SaveTo writes the registry to the given path (for testing).
221func (r *Registry) SaveTo(path string) error {
222 return r.saveTo(path)
223}