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