@@ -50,13 +50,23 @@ import (
5050//
5151// It requires an Index Resource to search for tools
5252type 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+ }
5565}
5666
5767// 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 ()
6070 if err != nil {
6171 return nil , err
6272 }
@@ -78,16 +88,16 @@ func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err er
7888}
7989
8090// 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 ) {
8292 res := tools.ToolCollection {}
8393
8494 // Find packagers
85- packagers , err := os .ReadDir (c . Folder )
95+ packagers , err := os .ReadDir (t . folder )
8696 if err != nil {
8797 if ! strings .Contains (err .Error (), "no such file" ) {
8898 return nil , err
8999 }
90- err = os .MkdirAll (c . Folder , 0755 )
100+ err = os .MkdirAll (t . folder , 0755 )
91101 if err != nil {
92102 return nil , err
93103 }
@@ -99,14 +109,14 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
99109 }
100110
101111 // Find tools
102- toolss , err := os .ReadDir (filepath .Join (c . Folder , packager .Name ()))
112+ toolss , err := os .ReadDir (filepath .Join (t . folder , packager .Name ()))
103113 if err != nil {
104114 return nil , err
105115 }
106116
107117 for _ , tool := range toolss {
108118 // Find versions
109- path := filepath .Join (c . Folder , packager .Name (), tool .Name ())
119+ path := filepath .Join (t . folder , packager .Name (), tool .Name ())
110120 versions , err := os .ReadDir (path )
111121 if err != nil {
112122 continue // we ignore errors because the folders could be dirty
@@ -127,7 +137,7 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
127137
128138// Install crawles the Index folder, downloads the specified tool, extracts the archive in the Tools Folder.
129139// 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 ) {
131141 path := filepath .Join (payload .Packager , payload .Name , payload .Version )
132142
133143 //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
136146 if err != nil {
137147 return nil , err
138148 }
139- return c .install (ctx , path , * payload .URL , * payload .Checksum )
149+ return t .install (ctx , path , * payload .URL , * payload .Checksum )
140150 }
141151
142152 // otherwise we install from the default index
143- body , err := c . Index .Read ()
153+ body , err := t . index .Read ()
144154 if err != nil {
145155 return nil , err
146156 }
@@ -159,7 +169,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
159169
160170 sys := tool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
161171
162- return c .install (ctx , path , sys .URL , sys .Checksum )
172+ return t .install (ctx , path , sys .URL , sys .Checksum )
163173 }
164174 }
165175 }
@@ -169,7 +179,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
169179 payload .Packager , payload .Name , payload .Version ))
170180}
171181
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 ) {
173183 // Download
174184 res , err := http .Get (url )
175185 if err != nil {
@@ -182,12 +192,12 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
182192 reader := io .TeeReader (res .Body , & buffer )
183193
184194 // Cleanup
185- err = os .RemoveAll (filepath .Join (c . Folder , path ))
195+ err = os .RemoveAll (filepath .Join (t . folder , path ))
186196 if err != nil {
187197 return nil , err
188198 }
189199
190- err = extract .Archive (ctx , reader , c . Folder , rename (path ))
200+ err = extract .Archive (ctx , reader , t . folder , rename (path ))
191201 if err != nil {
192202 os .RemoveAll (path )
193203 return nil , err
@@ -202,7 +212,7 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
202212 }
203213
204214 // Write installed.json for retrocompatibility with v1
205- err = writeInstalled (c . Folder , path )
215+ err = writeInstalled (t . folder , path )
206216 if err != nil {
207217 return nil , err
208218 }
@@ -211,9 +221,9 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
211221}
212222
213223// 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 ) {
215225 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 )
217227 if err != nil {
218228 return nil , err
219229 }
0 commit comments