-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathindexes.go
105 lines (91 loc) · 2.59 KB
/
indexes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package pkgs
import (
"context"
"encoding/json"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/arduino/arduino-create-agent/gen/indexes"
"github.com/sirupsen/logrus"
"go.bug.st/downloader"
)
// Indexes is a client that implements github.com/arduino/arduino-create-agent/gen/indexes.Service interface
type Indexes struct {
Log *logrus.Logger
Folder string
}
// Add downloads the index file found at the url contained in the payload, and saves it in the Indexes Folder.
// If called with an already existing index, it overwrites the file.
// It can fail if the payload is not defined, if it contains an invalid url.
func (c *Indexes) Add(ctx context.Context, payload *indexes.IndexPayload) (*indexes.Operation, error) {
// Parse url
indexURL, err := url.Parse(payload.URL)
if err != nil {
return nil, indexes.MakeInvalidURL(err)
}
// Download tmp file
filename := url.PathEscape(payload.URL)
path := filepath.Join(c.Folder, filename+".tmp")
d, err := downloader.Download(path, indexURL.String())
if err != nil {
return nil, err
}
err = d.Run()
if err != nil {
return nil, err
}
// Move tmp file
err = os.Rename(path, filepath.Join(c.Folder, filename))
if err != nil {
return nil, err
}
return &indexes.Operation{Status: "ok"}, nil
}
// Get reads the index file from the Indexes Folder, unmarshaling it
func (c *Indexes) Get(ctx context.Context, uri string) (index Index, err error) {
filename := url.PathEscape(uri)
path := filepath.Join(c.Folder, filename)
data, err := ioutil.ReadFile(path)
if err != nil {
return index, err
}
err = json.Unmarshal(data, &index)
if err != nil {
return index, err
}
return index, nil
}
// List reads from the Indexes Folder and returns the indexes that have been downloaded
func (c *Indexes) List(context.Context) ([]string, error) {
// Create folder if it doesn't exist
_ = os.MkdirAll(c.Folder, 0755)
// Read files
files, err := ioutil.ReadDir(c.Folder)
if err != nil {
return nil, err
}
res := []string{}
for _, file := range files {
// Select only files that begin with http
if !strings.HasPrefix(file.Name(), "http") {
continue
}
path, err := url.PathUnescape(file.Name())
if err != nil {
c.Log.Warn(err)
}
res = append(res, path)
}
return res, nil
}
// Remove deletes the index file from the Indexes Folder
func (c *Indexes) Remove(ctx context.Context, payload *indexes.IndexPayload) (*indexes.Operation, error) {
filename := url.PathEscape(payload.URL)
err := os.RemoveAll(filepath.Join(c.Folder, filename))
if err != nil {
return nil, err
}
return &indexes.Operation{Status: "ok"}, nil
}