Skip to content

Commit 5999fe2

Browse files
committed
Add docs for pkgs
1 parent 68c2c7e commit 5999fe2

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

v2/pkgs/indexes.go

+7
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ import (
1313
"go.bug.st/downloader"
1414
)
1515

16+
// Indexes is a client that implements github.com/arduino/arduino-create-agent/gen/indexes.Service interface
1617
type Indexes struct {
1718
Log *logrus.Logger
1819
Folder string
1920
}
2021

22+
// Add downloads the index file found at the url contained in the payload, and saves it in the Indexes Folder.
23+
// If called with an already existing index, it overwrites the file.
24+
// It can fail if the payload is not defined, if it contains an invalid url.
2125
func (c *Indexes) Add(ctx context.Context, payload *indexes.IndexPayload) error {
2226
// Parse url
2327
indexURL, err := url.Parse(payload.URL)
@@ -46,6 +50,7 @@ func (c *Indexes) Add(ctx context.Context, payload *indexes.IndexPayload) error
4650
return nil
4751
}
4852

53+
// Get reads the index file from the Indexes Folder, unmarshaling it
4954
func (c *Indexes) Get(ctx context.Context, uri string) (index Index, err error) {
5055
filename := url.PathEscape(uri)
5156
path := filepath.Join(c.Folder, filename)
@@ -62,6 +67,7 @@ func (c *Indexes) Get(ctx context.Context, uri string) (index Index, err error)
6267
return index, nil
6368
}
6469

70+
// List reads from the Indexes Folder and returns the indexes that have been downloaded
6571
func (c *Indexes) List(context.Context) ([]string, error) {
6672
// Read files
6773
files, err := ioutil.ReadDir(c.Folder)
@@ -81,6 +87,7 @@ func (c *Indexes) List(context.Context) ([]string, error) {
8187
return res, nil
8288
}
8389

90+
// Remove deletes the index file from the Indexes Folder
8491
func (c *Indexes) Remove(ctx context.Context, payload *indexes.IndexPayload) error {
8592
filename := url.PathEscape(payload.URL)
8693
return os.RemoveAll(filepath.Join(c.Folder, filename))

v2/pkgs/pkgs.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1+
// Package pkgs implements the functions from
2+
// github.com/arduino-create-agent/gen/indexes
3+
// and github.com/arduino-create-agent/gen/tools.
4+
//
5+
// It allows to manage package indexes from arduino
6+
// cores, and to download tools used for upload.
17
package pkgs
28

9+
// Index is the go representation of a typical
10+
// package-index file, stripped from every non-used field.
311
type Index struct {
412
Packages []struct {
5-
Name string `json:"name"`
6-
Maintainer string `json:"maintainer"`
7-
WebsiteURL string `json:"websiteURL"`
8-
Email string `json:"email,omitempty"`
9-
Help struct {
10-
Online string `json:"online"`
11-
} `json:"help,omitempty"`
13+
Name string `json:"name"`
1214
Tools []Tool `json:"tools"`
1315
} `json:"packages"`
1416
}
1517

18+
// Tool is the go representation of the info about a
19+
//tool contained in a package-index file, stripped from
20+
//every non-used field.
1621
type Tool struct {
1722
Name string `json:"name"`
1823
Version string `json:"version"`
1924
Systems []struct {
20-
Host string `json:"host"`
21-
URL string `json:"url"`
22-
ArchiveFileName string `json:"archiveFileName"`
23-
Checksum string `json:"checksum"`
24-
Size string `json:"size"`
25+
Host string `json:"host"`
26+
URL string `json:"url"`
27+
Checksum string `json:"checksum"`
2528
} `json:"systems"`
2629
}

v2/pkgs/tools.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,29 @@ import (
1717

1818
"github.com/arduino/arduino-create-agent/gen/tools"
1919
"github.com/codeclysm/extract"
20-
"github.com/sirupsen/logrus"
2120
"github.com/xrash/smetrics"
2221
)
2322

23+
// Tools is a client that implements github.com/arduino/arduino-create-agent/gen/tools.Service interface.
24+
// It saves tools in a specified folder with this structure: packager/name/version
25+
// For example:
26+
// folder
27+
// └── arduino
28+
// └── bossac
29+
// ├── 1.6.1-arduino
30+
// │   └── bossac
31+
// └── 1.7.0
32+
// └── bossac
33+
// It requires an Indexes client to list and read package index files: use the Indexes struct
2434
type Tools struct {
25-
Log *logrus.Logger
2635
Indexes interface {
2736
List(context.Context) ([]string, error)
2837
Get(context.Context, string) (Index, error)
2938
}
3039
Folder string
3140
}
3241

42+
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
3343
func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
3444
list, err := c.Indexes.List(ctx)
3545
if err != nil {
@@ -56,6 +66,7 @@ func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err er
5666
return res, nil
5767
}
5868

69+
// Installed crawles the Tools Folder and finds the installed tools.
5970
func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
6071
res := tools.ToolCollection{}
6172

@@ -94,6 +105,8 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
94105
return res, nil
95106
}
96107

108+
// Install crawles the Index folder, downloads the specified tool, extracts the archive in the Tools Folder.
109+
// It checks for the Signature specified in the package index.
97110
func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) error {
98111
list, err := c.Indexes.List(ctx)
99112
if err != nil {
@@ -157,6 +170,7 @@ func (c *Tools) install(ctx context.Context, packager string, tool Tool) error {
157170
return nil
158171
}
159172

173+
// Remove deletes the tool folder from Tools Folder
160174
func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
161175
path := filepath.Join(payload.Packager, payload.Name, payload.Version)
162176

0 commit comments

Comments
 (0)