Skip to content

Commit 620acfe

Browse files
committed
make pkgs.Tools struct properties unexported and use a "constructor/getters"
1 parent 2cd8248 commit 620acfe

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

v2/http.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ func Server(directory string, index *index.Resource) http.Handler {
4040
logAdapter := LogAdapter{Logger: logger}
4141

4242
// Mount tools
43-
toolsSvc := pkgs.Tools{
44-
Folder: directory,
45-
Index: index,
46-
}
47-
toolsEndpoints := toolssvc.NewEndpoints(&toolsSvc)
43+
toolsSvc := pkgs.New(index, directory)
44+
toolsEndpoints := toolssvc.NewEndpoints(toolsSvc)
4845
toolsServer := toolssvr.New(toolsEndpoints, mux, CustomRequestDecoder, goahttp.ResponseEncoder, errorHandler(logger), nil)
4946
toolssvr.Mount(mux, toolsServer)
5047

v2/pkgs/tools.go

+39-19
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,33 @@ import (
5050
//
5151
// It requires an Index Resource to search for tools
5252
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+
}
65+
}
66+
67+
// GetIndex will return the index
68+
func (t *Tools) GetIndex() *index.Resource {
69+
return t.index
70+
}
71+
72+
// GetFolder will return the folder
73+
func (t *Tools) GetFolder() string {
74+
return t.folder
5575
}
5676

5777
// 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()
78+
func (t *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
79+
body, err := t.GetIndex().Read()
6080
if err != nil {
6181
return nil, err
6282
}
@@ -78,16 +98,16 @@ func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err er
7898
}
7999

80100
// Installed crawles the Tools Folder and finds the installed tools.
81-
func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
101+
func (t *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
82102
res := tools.ToolCollection{}
83103

84104
// Find packagers
85-
packagers, err := os.ReadDir(c.Folder)
105+
packagers, err := os.ReadDir(t.GetFolder())
86106
if err != nil {
87107
if !strings.Contains(err.Error(), "no such file") {
88108
return nil, err
89109
}
90-
err = os.MkdirAll(c.Folder, 0755)
110+
err = os.MkdirAll(t.GetFolder(), 0755)
91111
if err != nil {
92112
return nil, err
93113
}
@@ -99,14 +119,14 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
99119
}
100120

101121
// Find tools
102-
toolss, err := os.ReadDir(filepath.Join(c.Folder, packager.Name()))
122+
toolss, err := os.ReadDir(filepath.Join(t.GetFolder(), packager.Name()))
103123
if err != nil {
104124
return nil, err
105125
}
106126

107127
for _, tool := range toolss {
108128
// Find versions
109-
path := filepath.Join(c.Folder, packager.Name(), tool.Name())
129+
path := filepath.Join(t.GetFolder(), packager.Name(), tool.Name())
110130
versions, err := os.ReadDir(path)
111131
if err != nil {
112132
continue // we ignore errors because the folders could be dirty
@@ -127,7 +147,7 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
127147

128148
// Install crawles the Index folder, downloads the specified tool, extracts the archive in the Tools Folder.
129149
// It checks for the Signature specified in the package index.
130-
func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
150+
func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
131151
path := filepath.Join(payload.Packager, payload.Name, payload.Version)
132152

133153
//if URL is defined and is signed we verify the signature and override the name, payload, version parameters
@@ -136,11 +156,11 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
136156
if err != nil {
137157
return nil, err
138158
}
139-
return c.install(ctx, path, *payload.URL, *payload.Checksum)
159+
return t.install(ctx, path, *payload.URL, *payload.Checksum)
140160
}
141161

142162
// otherwise we install from the default index
143-
body, err := c.Index.Read()
163+
body, err := t.GetIndex().Read()
144164
if err != nil {
145165
return nil, err
146166
}
@@ -159,7 +179,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
159179

160180
sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
161181

162-
return c.install(ctx, path, sys.URL, sys.Checksum)
182+
return t.install(ctx, path, sys.URL, sys.Checksum)
163183
}
164184
}
165185
}
@@ -169,7 +189,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
169189
payload.Packager, payload.Name, payload.Version))
170190
}
171191

172-
func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools.Operation, error) {
192+
func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools.Operation, error) {
173193
// Download
174194
res, err := http.Get(url)
175195
if err != nil {
@@ -182,12 +202,12 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
182202
reader := io.TeeReader(res.Body, &buffer)
183203

184204
// Cleanup
185-
err = os.RemoveAll(filepath.Join(c.Folder, path))
205+
err = os.RemoveAll(filepath.Join(t.GetFolder(), path))
186206
if err != nil {
187207
return nil, err
188208
}
189209

190-
err = extract.Archive(ctx, reader, c.Folder, rename(path))
210+
err = extract.Archive(ctx, reader, t.GetFolder(), rename(path))
191211
if err != nil {
192212
os.RemoveAll(path)
193213
return nil, err
@@ -202,7 +222,7 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
202222
}
203223

204224
// Write installed.json for retrocompatibility with v1
205-
err = writeInstalled(c.Folder, path)
225+
err = writeInstalled(t.GetFolder(), path)
206226
if err != nil {
207227
return nil, err
208228
}
@@ -211,9 +231,9 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
211231
}
212232

213233
// Remove deletes the tool folder from Tools Folder
214-
func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
234+
func (t *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
215235
path := filepath.Join(payload.Packager, payload.Name, payload.Version)
216-
pathToRemove, err := utilities.SafeJoin(c.Folder, path)
236+
pathToRemove, err := utilities.SafeJoin(t.GetFolder(), path)
217237
if err != nil {
218238
return nil, err
219239
}

v2/pkgs/tools_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ func TestTools(t *testing.T) {
4343
// Instantiate Index
4444
Index := index.Init(indexURL, config.GetDataDir())
4545

46-
service := pkgs.Tools{
47-
Folder: tmp,
48-
Index: Index,
49-
}
46+
service := pkgs.New(Index, tmp)
5047

5148
ctx := context.Background()
5249

0 commit comments

Comments
 (0)