-
Notifications
You must be signed in to change notification settings - Fork 32
/
plan_repository.go
70 lines (60 loc) · 1.58 KB
/
plan_repository.go
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
package blob
import (
"context"
"fmt"
"io"
"strings"
"github.com/raystack/frontier/billing/plan"
"github.com/raystack/frontier/billing/product"
"gocloud.dev/blob"
"gopkg.in/yaml.v3"
)
type PlanRepository struct {
bucket Bucket
}
func NewPlanRepository(b Bucket) *PlanRepository {
return &PlanRepository{bucket: b}
}
// Get returns the plans from the bucket
func (s *PlanRepository) Get(ctx context.Context) (plan.File, error) {
var definitions []plan.File
// iterate over bucket files, only read .yml & .yaml files
it := s.bucket.List(&blob.ListOptions{})
for {
obj, err := it.Next(ctx)
if err != nil {
if err == io.EOF {
break
}
return plan.File{}, err
}
if obj.IsDir {
continue
}
if !(strings.HasSuffix(obj.Key, ".yaml") || strings.HasSuffix(obj.Key, ".yml")) {
continue
}
fileBytes, err := s.bucket.ReadAll(ctx, obj.Key)
if err != nil {
return plan.File{}, fmt.Errorf("%s: %s", "error in reading bucket object", err.Error())
}
var def plan.File
if err := yaml.Unmarshal(fileBytes, &def); err != nil {
return plan.File{}, fmt.Errorf("get: yaml.Unmarshal: %s: %w", obj.Key, err)
}
definitions = append(definitions, def)
}
var allPlans []plan.Plan
var allProducts []product.Product
var allFeatures []product.Feature
for _, definition := range definitions {
allPlans = append(allPlans, definition.Plans...)
allProducts = append(allProducts, definition.Products...)
allFeatures = append(allFeatures, definition.Features...)
}
return plan.File{
Plans: allPlans,
Products: allProducts,
Features: allFeatures,
}, nil
}