@@ -50,13 +50,23 @@ import (
50
50
//
51
51
// It requires an Index Resource to search for tools
52
52
type Tools struct {
53
- Index * index.Resource
54
- Folder string
53
+ index * index.Resource
54
+ folder string
55
+ }
56
+
57
+ // New will return a Tool object, allowing the caller to execute operations on it.
58
+ // The New function will accept an index as parameter (used to download the indexes)
59
+ // and a folder used to download the indexes
60
+ func New (index * index.Resource , folder string ) * Tools {
61
+ return & Tools {
62
+ index : index ,
63
+ folder : folder ,
64
+ }
55
65
}
56
66
57
67
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
58
- func (c * Tools ) Available (ctx context.Context ) (res tools.ToolCollection , err error ) {
59
- body , err := c . Index .Read ()
68
+ func (t * Tools ) Available (ctx context.Context ) (res tools.ToolCollection , err error ) {
69
+ body , err := t . index .Read ()
60
70
if err != nil {
61
71
return nil , err
62
72
}
@@ -78,16 +88,16 @@ func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err er
78
88
}
79
89
80
90
// Installed crawles the Tools Folder and finds the installed tools.
81
- func (c * Tools ) Installed (ctx context.Context ) (tools.ToolCollection , error ) {
91
+ func (t * Tools ) Installed (ctx context.Context ) (tools.ToolCollection , error ) {
82
92
res := tools.ToolCollection {}
83
93
84
94
// Find packagers
85
- packagers , err := os .ReadDir (c . Folder )
95
+ packagers , err := os .ReadDir (t . folder )
86
96
if err != nil {
87
97
if ! strings .Contains (err .Error (), "no such file" ) {
88
98
return nil , err
89
99
}
90
- err = os .MkdirAll (c . Folder , 0755 )
100
+ err = os .MkdirAll (t . folder , 0755 )
91
101
if err != nil {
92
102
return nil , err
93
103
}
@@ -99,14 +109,14 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
99
109
}
100
110
101
111
// Find tools
102
- toolss , err := os .ReadDir (filepath .Join (c . Folder , packager .Name ()))
112
+ toolss , err := os .ReadDir (filepath .Join (t . folder , packager .Name ()))
103
113
if err != nil {
104
114
return nil , err
105
115
}
106
116
107
117
for _ , tool := range toolss {
108
118
// Find versions
109
- path := filepath .Join (c . Folder , packager .Name (), tool .Name ())
119
+ path := filepath .Join (t . folder , packager .Name (), tool .Name ())
110
120
versions , err := os .ReadDir (path )
111
121
if err != nil {
112
122
continue // we ignore errors because the folders could be dirty
@@ -127,7 +137,7 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
127
137
128
138
// Install crawles the Index folder, downloads the specified tool, extracts the archive in the Tools Folder.
129
139
// It checks for the Signature specified in the package index.
130
- func (c * Tools ) Install (ctx context.Context , payload * tools.ToolPayload ) (* tools.Operation , error ) {
140
+ func (t * Tools ) Install (ctx context.Context , payload * tools.ToolPayload ) (* tools.Operation , error ) {
131
141
path := filepath .Join (payload .Packager , payload .Name , payload .Version )
132
142
133
143
//if URL is defined and is signed we verify the signature and override the name, payload, version parameters
@@ -136,11 +146,11 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
136
146
if err != nil {
137
147
return nil , err
138
148
}
139
- return c .install (ctx , path , * payload .URL , * payload .Checksum )
149
+ return t .install (ctx , path , * payload .URL , * payload .Checksum )
140
150
}
141
151
142
152
// otherwise we install from the default index
143
- body , err := c . Index .Read ()
153
+ body , err := t . index .Read ()
144
154
if err != nil {
145
155
return nil , err
146
156
}
@@ -159,7 +169,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
159
169
160
170
sys := tool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
161
171
162
- return c .install (ctx , path , sys .URL , sys .Checksum )
172
+ return t .install (ctx , path , sys .URL , sys .Checksum )
163
173
}
164
174
}
165
175
}
@@ -169,7 +179,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
169
179
payload .Packager , payload .Name , payload .Version ))
170
180
}
171
181
172
- func (c * Tools ) install (ctx context.Context , path , url , checksum string ) (* tools.Operation , error ) {
182
+ func (t * Tools ) install (ctx context.Context , path , url , checksum string ) (* tools.Operation , error ) {
173
183
// Download
174
184
res , err := http .Get (url )
175
185
if err != nil {
@@ -182,12 +192,12 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
182
192
reader := io .TeeReader (res .Body , & buffer )
183
193
184
194
// Cleanup
185
- err = os .RemoveAll (filepath .Join (c . Folder , path ))
195
+ err = os .RemoveAll (filepath .Join (t . folder , path ))
186
196
if err != nil {
187
197
return nil , err
188
198
}
189
199
190
- err = extract .Archive (ctx , reader , c . Folder , rename (path ))
200
+ err = extract .Archive (ctx , reader , t . folder , rename (path ))
191
201
if err != nil {
192
202
os .RemoveAll (path )
193
203
return nil , err
@@ -202,7 +212,7 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
202
212
}
203
213
204
214
// Write installed.json for retrocompatibility with v1
205
- err = writeInstalled (c . Folder , path )
215
+ err = writeInstalled (t . folder , path )
206
216
if err != nil {
207
217
return nil , err
208
218
}
@@ -211,9 +221,9 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
211
221
}
212
222
213
223
// Remove deletes the tool folder from Tools Folder
214
- func (c * Tools ) Remove (ctx context.Context , payload * tools.ToolPayload ) (* tools.Operation , error ) {
224
+ func (t * Tools ) Remove (ctx context.Context , payload * tools.ToolPayload ) (* tools.Operation , error ) {
215
225
path := filepath .Join (payload .Packager , payload .Name , payload .Version )
216
- pathToRemove , err := utilities .SafeJoin (c . Folder , path )
226
+ pathToRemove , err := utilities .SafeJoin (t . folder , path )
217
227
if err != nil {
218
228
return nil , err
219
229
}
0 commit comments