-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
219 lines (183 loc) · 5.18 KB
/
service.go
File metadata and controls
219 lines (183 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package git
import (
"context"
"iter"
"path/filepath"
"slices"
"strings"
"sync"
"dappco.re/go/core"
)
// Queries for git service
// QueryStatus requests git status for paths.
type QueryStatus struct {
Paths []string
Names map[string]string
}
// QueryDirtyRepos requests repos with uncommitted changes.
type QueryDirtyRepos struct{}
// QueryAheadRepos requests repos with unpushed commits.
type QueryAheadRepos struct{}
// Tasks for git service
// TaskPush requests git push for a path.
type TaskPush struct {
Path string
Name string
}
// TaskPull requests git pull for a path.
type TaskPull struct {
Path string
Name string
}
// TaskPushMultiple requests git push for multiple paths.
type TaskPushMultiple struct {
Paths []string
Names map[string]string
}
// ServiceOptions for configuring the git service.
type ServiceOptions struct {
WorkDir string
}
// Compile-time interface checks.
var _ core.Startable = (*Service)(nil)
// Service provides git operations as a Core service.
type Service struct {
*core.ServiceRuntime[ServiceOptions]
opts ServiceOptions
mu sync.RWMutex
lastStatus []RepoStatus
}
// NewService creates a git service factory.
func NewService(opts ServiceOptions) func(*core.Core) (any, error) {
return func(c *core.Core) (any, error) {
return &Service{
ServiceRuntime: core.NewServiceRuntime(c, opts),
opts: opts,
}, nil
}
}
// OnStartup registers query and task handlers.
func (s *Service) OnStartup(ctx context.Context) error {
s.Core().RegisterQuery(s.handleQuery)
s.Core().RegisterTask(s.handleTask)
return nil
}
func (s *Service) handleQuery(c *core.Core, q core.Query) core.Result {
ctx := context.Background() // TODO: core should pass context to handlers
switch m := q.(type) {
case QueryStatus:
// Validate all paths before execution
for _, path := range m.Paths {
if err := s.validatePath(path); err != nil {
return c.LogError(err, "git.handleQuery", "path validation failed")
}
}
statuses := Status(ctx, StatusOptions(m))
s.mu.Lock()
s.lastStatus = statuses
s.mu.Unlock()
return core.Result{Value: statuses, OK: true}
case QueryDirtyRepos:
return core.Result{Value: s.DirtyRepos(), OK: true}
case QueryAheadRepos:
return core.Result{Value: s.AheadRepos(), OK: true}
}
return core.Result{}
}
func (s *Service) handleTask(c *core.Core, t core.Task) core.Result {
ctx := context.Background() // TODO: core should pass context to handlers
switch m := t.(type) {
case TaskPush:
if err := s.validatePath(m.Path); err != nil {
return c.LogError(err, "git.handleTask", "path validation failed")
}
if err := Push(ctx, m.Path); err != nil {
return c.LogError(err, "git.handleTask", "push failed")
}
return core.Result{OK: true}
case TaskPull:
if err := s.validatePath(m.Path); err != nil {
return c.LogError(err, "git.handleTask", "path validation failed")
}
if err := Pull(ctx, m.Path); err != nil {
return c.LogError(err, "git.handleTask", "pull failed")
}
return core.Result{OK: true}
case TaskPushMultiple:
for _, path := range m.Paths {
if err := s.validatePath(path); err != nil {
return c.LogError(err, "git.handleTask", "path validation failed")
}
}
results, err := PushMultiple(ctx, m.Paths, m.Names)
if err != nil {
// Log for observability; partial results are still returned.
_ = c.LogError(err, "git.handleTask", "push multiple had failures")
}
return core.Result{Value: results, OK: true}
}
return core.Result{}
}
func (s *Service) validatePath(path string) error {
if !filepath.IsAbs(path) {
return core.E("git.validatePath", "path must be absolute: "+path, nil)
}
workDir := s.opts.WorkDir
if workDir != "" {
rel, err := filepath.Rel(workDir, path)
if err != nil || strings.HasPrefix(rel, "..") {
return core.E("git.validatePath", "path "+path+" is outside of allowed WorkDir "+workDir, nil)
}
}
return nil
}
// Status returns last status result.
func (s *Service) Status() []RepoStatus {
s.mu.RLock()
defer s.mu.RUnlock()
return slices.Clone(s.lastStatus)
}
// All returns an iterator over all last known statuses.
func (s *Service) All() iter.Seq[RepoStatus] {
s.mu.RLock()
defer s.mu.RUnlock()
return slices.Values(slices.Clone(s.lastStatus))
}
// Dirty returns an iterator over repos with uncommitted changes.
func (s *Service) Dirty() iter.Seq[RepoStatus] {
s.mu.RLock()
defer s.mu.RUnlock()
lastStatus := slices.Clone(s.lastStatus)
return func(yield func(RepoStatus) bool) {
for _, st := range lastStatus {
if st.Error == nil && st.IsDirty() {
if !yield(st) {
return
}
}
}
}
}
// Ahead returns an iterator over repos with unpushed commits.
func (s *Service) Ahead() iter.Seq[RepoStatus] {
s.mu.RLock()
defer s.mu.RUnlock()
lastStatus := slices.Clone(s.lastStatus)
return func(yield func(RepoStatus) bool) {
for _, st := range lastStatus {
if st.Error == nil && st.HasUnpushed() {
if !yield(st) {
return
}
}
}
}
}
// DirtyRepos returns repos with uncommitted changes.
func (s *Service) DirtyRepos() []RepoStatus {
return slices.Collect(s.Dirty())
}
// AheadRepos returns repos with unpushed commits.
func (s *Service) AheadRepos() []RepoStatus {
return slices.Collect(s.Ahead())
}