gunk-dev/the-valley

registry.go

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