2
2
package k6catalog
3
3
4
4
import (
5
+ "bytes"
5
6
"context"
7
+ "encoding/json"
6
8
"errors"
7
9
"fmt"
8
10
"io"
@@ -18,10 +20,13 @@ const (
18
20
)
19
21
20
22
var (
21
- ErrCannotSatisfy = errors .New ("cannot satisfy dependency" ) //nolint:revive
22
- ErrInvalidConstrain = errors .New ("invalid constrain" ) //nolint:revive
23
- ErrUnknownDependency = errors .New ("unknown dependency" ) //nolint:revive
24
- ErrDownload = errors .New ("downloading catalog" ) //nolint:revive
23
+ ErrCannotSatisfy = errors .New ("cannot satisfy dependency" ) //nolint:revive
24
+ ErrDownload = errors .New ("downloading catalog" ) //nolint:revive
25
+ ErrInvalidConstrain = errors .New ("invalid constrain" ) //nolint:revive
26
+ ErrInvalidCatalog = fmt .Errorf ("invalid catalog" ) //nolint:revive
27
+ ErrOpening = errors .New ("opening catalog" ) //nolint:revive
28
+ ErrUnknownDependency = errors .New ("unknown dependency" ) //nolint:revive
29
+ ErrDependencyNotFound = fmt .Errorf ("dependency not found in registry" ) //nolint:revive
25
30
)
26
31
27
32
// Dependency defines a Dependency with a version constrain
@@ -45,22 +50,54 @@ type Catalog interface {
45
50
Resolve (ctx context.Context , dep Dependency ) (Module , error )
46
51
}
47
52
53
+ // entry defines a catalog entry
54
+ type entry struct {
55
+ Module string `json:"module,omitempty"`
56
+ Versions []string `json:"versions,omitempty"`
57
+ }
58
+
48
59
type catalog struct {
49
- registry Registry
60
+ dependencies map [ string ] entry
50
61
}
51
62
52
- // NewCatalog creates a catalog from a registry
53
- func NewCatalog (registry Registry ) Catalog {
54
- return catalog {registry : registry }
63
+ // getVersions returns the versions for a given module
64
+ func (c catalog ) getVersions (_ context.Context , mod string ) (entry , error ) {
65
+ e , found := c .dependencies [mod ]
66
+ if ! found {
67
+ return entry {}, fmt .Errorf ("%w : %s" , ErrDependencyNotFound , mod )
68
+ }
69
+
70
+ return e , nil
55
71
}
56
72
57
73
// NewCatalogFromJSON creates a Catalog from a json file
58
- func NewCatalogFromJSON (catalogFile string ) (Catalog , error ) {
59
- registry , err := loadRegistryFromJSON (catalogFile )
74
+ func NewCatalogFromJSON (stream io.Reader ) (Catalog , error ) {
75
+ buff := & bytes.Buffer {}
76
+ _ , err := buff .ReadFrom (stream )
77
+ if err != nil {
78
+ return nil , fmt .Errorf ("%w: %w" , ErrInvalidCatalog , err )
79
+ }
80
+
81
+ dependencies := map [string ]entry {}
82
+ err = json .Unmarshal (buff .Bytes (), & dependencies )
60
83
if err != nil {
61
- return nil , err
84
+ return nil , fmt . Errorf ( "%w: %w" , ErrInvalidCatalog , err )
62
85
}
63
- return catalog {registry : registry }, nil
86
+
87
+ return catalog {
88
+ dependencies : dependencies ,
89
+ }, nil
90
+ }
91
+
92
+ // NewCatalogFromFile creates a Catalog from a json file
93
+ func NewCatalogFromFile (catalogFile string ) (Catalog , error ) {
94
+ json , err := os .ReadFile (catalogFile ) //nolint:forbidigo,gosec
95
+ if err != nil {
96
+ return nil , fmt .Errorf ("%w: %w" , ErrOpening , err )
97
+ }
98
+
99
+ buff := bytes .NewBuffer (json )
100
+ return NewCatalogFromJSON (buff )
64
101
}
65
102
66
103
// NewCatalogFromURL creates a Catalog from a URL
@@ -80,24 +117,7 @@ func NewCatalogFromURL(ctx context.Context, catalogURL string) (Catalog, error)
80
117
return nil , fmt .Errorf ("%w %s" , ErrDownload , resp .Status )
81
118
}
82
119
83
- catalogFile , err := os .CreateTemp ("" , "catalog*.json" ) //nolint:forbidigo
84
- if err != nil {
85
- return nil , fmt .Errorf ("%w %w" , ErrDownload , err )
86
- }
87
-
88
- _ , err = io .Copy (catalogFile , resp .Body )
89
- if err != nil {
90
- _ = catalogFile .Close ()
91
- _ = os .Remove (catalogFile .Name ()) //nolint:forbidigo
92
- return nil , fmt .Errorf ("%w %w" , ErrDownload , err )
93
- }
94
-
95
- err = catalogFile .Close ()
96
- if err != nil {
97
- return nil , fmt .Errorf ("%w %w" , ErrDownload , err )
98
- }
99
-
100
- catalog , err := NewCatalogFromJSON (catalogFile .Name ())
120
+ catalog , err := NewCatalogFromJSON (resp .Body )
101
121
if err != nil {
102
122
return nil , fmt .Errorf ("%w %w" , ErrDownload , err )
103
123
}
@@ -107,11 +127,11 @@ func NewCatalogFromURL(ctx context.Context, catalogURL string) (Catalog, error)
107
127
108
128
// DefaultCatalog creates a Catalog from the default json file 'catalog.json'
109
129
func DefaultCatalog () (Catalog , error ) {
110
- return NewCatalogFromJSON (defaultCatalogFile )
130
+ return NewCatalogFromFile (defaultCatalogFile )
111
131
}
112
132
113
133
func (c catalog ) Resolve (ctx context.Context , dep Dependency ) (Module , error ) {
114
- entry , err := c .registry . GetVersions (ctx , dep .Name )
134
+ entry , err := c .getVersions (ctx , dep .Name )
115
135
if err != nil {
116
136
return Module {}, err
117
137
}
0 commit comments