@@ -18,48 +18,111 @@ package tools
1818import (
1919 "encoding/json"
2020 "fmt"
21- "os"
22- "os/user"
23- "path"
2421 "path/filepath"
2522 "strings"
2623 "sync"
2724
2825 "github.com/arduino/arduino-create-agent/index"
26+ "github.com/arduino/go-paths-helper"
2927 "github.com/xrash/smetrics"
3028)
3129
3230// Tools handle the tools necessary for an upload on a board.
3331// It provides a means to download a tool from the arduino servers.
3432//
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
3937//
4038// 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)
4743
4844// Tools will represent the installed tools
4945type 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 )
5349 installed map [string ]string
5450 mutex sync.RWMutex
5551}
5652
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 ) {
5986 t .mutex .Lock ()
60- t .installed = make ( map [ string ] string )
87+ t .installed [ key ] = value
6188 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 )
63126}
64127
65128// GetLocation extracts the toolname from a command like
@@ -71,22 +134,20 @@ func (t *Tools) GetLocation(command string) (string, error) {
71134 var ok bool
72135
73136 // Load installed
74- t .mutex .RLock ()
75- fmt .Println (t .installed )
76- t .mutex .RUnlock ()
137+ t .printMap ()
77138
78139 err := t .readMap ()
79140 if err != nil {
80141 return "" , err
81142 }
82143
83- t .mutex .RLock ()
84- defer t .mutex .RUnlock ()
85- fmt .Println (t .installed )
144+ t .printMap ()
86145
87146 // 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 {
89148 maxSimilarity := 0.0
149+ t .mutex .RLock ()
150+ defer t .mutex .RUnlock ()
90151 for i , candidate := range t .installed {
91152 similarity := smetrics .Jaro (command , i )
92153 if similarity > 0.8 && similarity > maxSimilarity {
@@ -97,32 +158,3 @@ func (t *Tools) GetLocation(command string) (string, error) {
97158 }
98159 return filepath .ToSlash (location ), nil
99160}
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