@@ -17,49 +17,89 @@ package tools
1717
1818import (
1919 "encoding/json"
20- "fmt"
21- "os"
22- "os/user"
23- "path"
2420 "path/filepath"
2521 "strings"
2622 "sync"
2723
2824 "github.com/arduino/arduino-create-agent/index"
25+ "github.com/arduino/go-paths-helper"
2926 "github.com/xrash/smetrics"
3027)
3128
3229// Tools handle the tools necessary for an upload on a board.
3330// It provides a means to download a tool from the arduino servers.
3431//
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
3936//
4037// 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)
4742
4843// Tools will represent the installed tools
4944type 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 )
5348 installed map [string ]string
5449 mutex sync.RWMutex
5550}
5651
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 ) {
5969 t .mutex .Lock ()
60- t .installed = make ( map [ string ] string )
70+ t .installed [ key ] = value
6171 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 )
63103}
64104
65105// GetLocation extracts the toolname from a command like
@@ -71,22 +111,16 @@ func (t *Tools) GetLocation(command string) (string, error) {
71111 var ok bool
72112
73113 // Load installed
74- t .mutex .RLock ()
75- fmt .Println (t .installed )
76- t .mutex .RUnlock ()
77-
78114 err := t .readMap ()
79115 if err != nil {
80116 return "" , err
81117 }
82118
83- t .mutex .RLock ()
84- defer t .mutex .RUnlock ()
85- fmt .Println (t .installed )
86-
87119 // 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 {
89121 maxSimilarity := 0.0
122+ t .mutex .RLock ()
123+ defer t .mutex .RUnlock ()
90124 for i , candidate := range t .installed {
91125 similarity := smetrics .Jaro (command , i )
92126 if similarity > 0.8 && similarity > maxSimilarity {
@@ -97,32 +131,3 @@ func (t *Tools) GetLocation(command string) (string, error) {
97131 }
98132 return filepath .ToSlash (location ), nil
99133}
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