forked from openmeterio/openmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
80 lines (64 loc) · 1.46 KB
/
Copy pathutils.go
File metadata and controls
80 lines (64 loc) · 1.46 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
package main
import (
"context"
"fmt"
"github.com/sourcegraph/conc/pool"
"github.com/openmeterio/openmeter/.dagger/internal/dagger"
)
type syncable[T any] interface {
Sync(ctx context.Context) (T, error)
}
func sync[T any](ctx context.Context, s syncable[T]) error {
_, err := s.Sync(ctx)
if err != nil {
return err
}
return nil
}
func syncFunc[T any](s syncable[T]) func(context.Context) error {
return func(ctx context.Context) error {
return sync(ctx, s)
}
}
func wrapSyncable[T any](s syncable[T]) func(context.Context) error {
return func(ctx context.Context) error {
_, err := s.Sync(ctx)
return err
}
}
func wrapSyncables[T syncable[T]](ss []T) func(context.Context) error {
return func(ctx context.Context) error {
for _, s := range ss {
_, err := s.Sync(ctx)
if err != nil {
return err
}
}
return nil
}
}
type pipeline struct {
pool *pool.ContextPool
}
func newPipeline(ctx context.Context) *pipeline {
return &pipeline{
pool: pool.New().WithErrors().WithContext(ctx),
}
}
func (p *pipeline) addJob(job func(context.Context) error) {
p.pool.Go(job)
}
func (p *pipeline) addJobs(jobs ...func(context.Context) error) {
for _, job := range jobs {
p.pool.Go(job)
}
}
func (p *pipeline) wait() error {
return p.pool.Wait()
}
func addSyncableStep[T any](p *pipeline, s syncable[T]) {
p.pool.Go(syncFunc(s))
}
func cacheVolume(name string) *dagger.CacheVolume {
return dag.CacheVolume(fmt.Sprintf("openmeter-%s", name))
}