-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathtools.go
133 lines (119 loc) · 3.83 KB
/
tools.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 Arduino SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tools
import (
"encoding/json"
"path/filepath"
"strings"
"sync"
"github.com/arduino/arduino-create-agent/index"
"github.com/arduino/go-paths-helper"
"github.com/xrash/smetrics"
)
// Tools handle the tools necessary for an upload on a board.
// It provides a means to download a tool from the arduino servers.
//
// - *directory* contains the location where the tools are downloaded.
// - *indexURL* contains the url where the tools description is contained.
// - *logger* is a StdLogger used for reporting debug and info messages
// - *installed* contains a map[string]string of the tools installed and their exact location
//
// Usage:
// You have to call the New() function passing it the required parameters:
//
// index = index.Init("https://downloads.arduino.cc/packages/package_index.json", dataDir)
// tools := tools.New(dataDir, index, logger)
// Tools will represent the installed tools
type Tools struct {
directory *paths.Path
index *index.Resource
logger func(msg string)
installed map[string]string
mutex sync.RWMutex
}
// New will return a Tool object, allowing the caller to execute operations on it.
// The New functions accept the directory to use to host the tools,
// an index (used to download the tools),
// and a logger to log the operations
func New(directory *paths.Path, index *index.Resource, logger func(msg string)) *Tools {
t := &Tools{
directory: directory,
index: index,
logger: logger,
installed: map[string]string{},
mutex: sync.RWMutex{},
}
_ = t.readMap()
return t
}
func (t *Tools) setMapValue(key, value string) {
t.mutex.Lock()
t.installed[key] = value
t.mutex.Unlock()
}
func (t *Tools) getMapValue(key string) (string, bool) {
t.mutex.RLock()
defer t.mutex.RUnlock()
value, ok := t.installed[key]
return value, ok
}
// writeMap() writes installed map to the json file "installed.json"
func (t *Tools) writeMap() error {
t.mutex.RLock()
defer t.mutex.RUnlock()
b, err := json.Marshal(t.installed)
if err != nil {
return err
}
filePath := t.directory.Join("installed.json")
return filePath.WriteFile(b)
}
// readMap() reads the installed map from json file "installed.json"
func (t *Tools) readMap() error {
t.mutex.Lock()
defer t.mutex.Unlock()
filePath := t.directory.Join("installed.json")
b, err := filePath.ReadFile()
if err != nil {
return err
}
return json.Unmarshal(b, &t.installed)
}
// GetLocation extracts the toolname from a command like
func (t *Tools) GetLocation(command string) (string, error) {
command = strings.Replace(command, "{runtime.tools.", "", 1)
command = strings.Replace(command, ".path}", "", 1)
var location string
var ok bool
// Load installed
err := t.readMap()
if err != nil {
return "", err
}
// use string similarity to resolve a runtime var with a "similar" map element
if location, ok = t.getMapValue(command); !ok {
maxSimilarity := 0.0
t.mutex.RLock()
defer t.mutex.RUnlock()
for i, candidate := range t.installed {
similarity := smetrics.Jaro(command, i)
if similarity > 0.8 && similarity > maxSimilarity {
maxSimilarity = similarity
location = candidate
}
}
}
return filepath.ToSlash(location), nil
}