@@ -17,49 +17,89 @@ package tools
17
17
18
18
import (
19
19
"encoding/json"
20
- "fmt"
21
- "os"
22
- "os/user"
23
- "path"
24
20
"path/filepath"
25
21
"strings"
26
22
"sync"
27
23
28
24
"github.com/arduino/arduino-create-agent/index"
25
+ "github.com/arduino/go-paths-helper"
29
26
"github.com/xrash/smetrics"
30
27
)
31
28
32
29
// Tools handle the tools necessary for an upload on a board.
33
30
// It provides a means to download a tool from the arduino servers.
34
31
//
35
- // - *Directory * contains the location where the tools are downloaded.
36
- // - *IndexURL * contains the url where the tools description is contained.
37
- // - *Logger * is a StdLogger used for reporting debug and info messages
38
- // - *installed* contains a map of the tools and their exact location
32
+ // - *directory * contains the location where the tools are downloaded.
33
+ // - *indexURL * contains the url where the tools description is contained.
34
+ // - *logger * is a StdLogger used for reporting debug and info messages
35
+ // - *installed* contains a map[string]string of the tools installed and their exact location
39
36
//
40
37
// Usage:
41
- // You have to instantiate the struct by passing it the required parameters:
42
- // _tools := tools.Tools{
43
- // Directory: "/home/user/.arduino-create",
44
- // IndexURL: "https://downloads.arduino.cc/packages/package_index.json"
45
- // Logger: log.Logger
46
- // }
38
+ // You have to call the New() function passing it the required parameters:
39
+ //
40
+ // index = index.Init("https://downloads.arduino.cc/packages/package_staging_index.json", dataDir)
41
+ // tools := tools.New(dataDir, index, logger)
47
42
48
43
// Tools will represent the installed tools
49
44
type Tools struct {
50
- Directory string
51
- Index * index.Resource
52
- Logger func (msg string )
45
+ directory * paths. Path
46
+ index * index.Resource
47
+ logger func (msg string )
53
48
installed map [string ]string
54
49
mutex sync.RWMutex
55
50
}
56
51
57
- // Init creates the Installed map and populates it from a file in .arduino-create
58
- func (t * Tools ) Init () {
52
+ // New will return a Tool object, allowing the caller to execute operations on it.
53
+ // The New functions accept the directory to use to host the tools,
54
+ // an index (used to download the tools),
55
+ // and a logger to log the operations
56
+ func New (directory * paths.Path , index * index.Resource , logger func (msg string )) * Tools {
57
+ t := & Tools {
58
+ directory : directory ,
59
+ index : index ,
60
+ logger : logger ,
61
+ installed : map [string ]string {},
62
+ mutex : sync.RWMutex {},
63
+ }
64
+ _ = t .readMap ()
65
+ return t
66
+ }
67
+
68
+ func (t * Tools ) setMapValue (key , value string ) {
59
69
t .mutex .Lock ()
60
- t .installed = make ( map [ string ] string )
70
+ t .installed [ key ] = value
61
71
t .mutex .Unlock ()
62
- t .readMap ()
72
+ }
73
+
74
+ func (t * Tools ) getMapValue (key string ) (string , bool ) {
75
+ t .mutex .RLock ()
76
+ defer t .mutex .RUnlock ()
77
+ value , ok := t .installed [key ]
78
+ return value , ok
79
+ }
80
+
81
+ // writeMap() writes installed map to the json file "installed.json"
82
+ func (t * Tools ) writeMap () error {
83
+ t .mutex .RLock ()
84
+ defer t .mutex .RUnlock ()
85
+ b , err := json .Marshal (t .installed )
86
+ if err != nil {
87
+ return err
88
+ }
89
+ filePath := t .directory .Join ("installed.json" )
90
+ return filePath .WriteFile (b )
91
+ }
92
+
93
+ // readMap() reads the installed map from json file "installed.json"
94
+ func (t * Tools ) readMap () error {
95
+ t .mutex .Lock ()
96
+ defer t .mutex .Unlock ()
97
+ filePath := t .directory .Join ("installed.json" )
98
+ b , err := filePath .ReadFile ()
99
+ if err != nil {
100
+ return err
101
+ }
102
+ return json .Unmarshal (b , & t .installed )
63
103
}
64
104
65
105
// GetLocation extracts the toolname from a command like
@@ -71,22 +111,16 @@ func (t *Tools) GetLocation(command string) (string, error) {
71
111
var ok bool
72
112
73
113
// Load installed
74
- t .mutex .RLock ()
75
- fmt .Println (t .installed )
76
- t .mutex .RUnlock ()
77
-
78
114
err := t .readMap ()
79
115
if err != nil {
80
116
return "" , err
81
117
}
82
118
83
- t .mutex .RLock ()
84
- defer t .mutex .RUnlock ()
85
- fmt .Println (t .installed )
86
-
87
119
// use string similarity to resolve a runtime var with a "similar" map element
88
- if location , ok = t .installed [ command ] ; ! ok {
120
+ if location , ok = t .getMapValue ( command ) ; ! ok {
89
121
maxSimilarity := 0.0
122
+ t .mutex .RLock ()
123
+ defer t .mutex .RUnlock ()
90
124
for i , candidate := range t .installed {
91
125
similarity := smetrics .Jaro (command , i )
92
126
if similarity > 0.8 && similarity > maxSimilarity {
@@ -97,32 +131,3 @@ func (t *Tools) GetLocation(command string) (string, error) {
97
131
}
98
132
return filepath .ToSlash (location ), nil
99
133
}
100
-
101
- // writeMap() writes installed map to the json file "installed.json"
102
- func (t * Tools ) writeMap () error {
103
- t .mutex .Lock ()
104
- b , err := json .Marshal (t .installed )
105
- defer t .mutex .Unlock ()
106
- if err != nil {
107
- return err
108
- }
109
- filePath := path .Join (dir (), "installed.json" )
110
- return os .WriteFile (filePath , b , 0644 )
111
- }
112
-
113
- // readMap() reads the installed map from json file "installed.json"
114
- func (t * Tools ) readMap () error {
115
- t .mutex .Lock ()
116
- defer t .mutex .Unlock ()
117
- filePath := path .Join (dir (), "installed.json" )
118
- b , err := os .ReadFile (filePath )
119
- if err != nil {
120
- return err
121
- }
122
- return json .Unmarshal (b , & t .installed )
123
- }
124
-
125
- func dir () string {
126
- usr , _ := user .Current ()
127
- return path .Join (usr .HomeDir , ".arduino-create" )
128
- }
0 commit comments