Skip to content

Commit 3f5245c

Browse files
committed
Moved path-creation utility in own package. Renamed some common paths.
This simplifies the overall codebase and avoids passing anonymous functions to discover paths, for example the following snippet: // DownloadIndex is a function to download a generic index. func DownloadIndex(indexPathFunc func() (string, error), URL string) error { file, err := indexPathFunc() ... becomes: func DownloadIndex(indexPath pathutils.Path, URL string) error { file, err := indexPath.Get() ... This commit also renames some of the paths defined in the commons package removing the Default prefix, since them are initialized with the default but they are updated with the actual path at runtime.
1 parent 96139c2 commit 3f5245c

27 files changed

+263
-151
lines changed

commands/board/attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
8484

8585
time.Sleep(duration)
8686

87-
homeFolder, err := common.GetDefaultArduinoHomeFolder()
87+
homeFolder, err := common.ArduinoHomeFolder.Get()
8888
if err != nil {
8989
formatter.PrintError(err, "Cannot Parse Board Index file.")
9090
os.Exit(commands.ErrCoreConfig)
9191
}
9292

93-
packageFolder, err := common.GetDefaultPkgFolder()
93+
packageFolder, err := common.PackagesFolder.Get()
9494
if err != nil {
9595
formatter.PrintError(err, "Cannot Parse Board Index file.")
9696
os.Exit(commands.ErrCoreConfig)

commands/board/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var listCommand = &cobra.Command{
3939
// runListCommand detects and lists the connected arduino boards
4040
// (either via serial or network ports).
4141
func runListCommand(cmd *cobra.Command, args []string) {
42-
packageFolder, err := common.GetDefaultPkgFolder()
42+
packageFolder, err := common.PackagesFolder.Get()
4343
if err != nil {
4444
formatter.PrintError(err, "Cannot Parse Board Index file.")
4545
os.Exit(commands.ErrCoreConfig)

commands/commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestLibDownload(t *testing.T) {
105105
defer cleanTempRedirect(t, tempFile)
106106

107107
// getting the paths to create the want path of the want object.
108-
stagingFolder, err := common.GetDownloadCacheFolder("libraries")
108+
stagingFolder, err := common.DownloadCacheFolder("libraries").Get()
109109
require.NoError(t, err, "Getting cache folder")
110110

111111
// desired output
@@ -159,7 +159,7 @@ func TestCoreDownload(t *testing.T) {
159159
defer cleanTempRedirect(t, tempFile)
160160

161161
// getting the paths to create the want path of the want object.
162-
stagingFolder, err := common.GetDownloadCacheFolder("packages")
162+
stagingFolder, err := common.DownloadCacheFolder("packages").Get()
163163
require.NoError(t, err, "Getting cache folder")
164164

165165
// desired output

commands/compile/compile.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,28 @@ func run(cmd *cobra.Command, args []string) {
144144
ctx := &types.Context{}
145145

146146
ctx.FQBN = fullyQualifiedBoardName
147-
ctx.SketchLocation = filepath.Join(common.SketchbookFolder, sketchName)
147+
sketchbookPath, err := common.SketchbookFolder.Get()
148+
if err != nil {
149+
formatter.PrintError(err, "Getting sketchbook folder")
150+
os.Exit(commands.ErrCoreConfig)
151+
}
152+
ctx.SketchLocation = filepath.Join(sketchbookPath, sketchName)
148153

149-
packagesFolder, err := common.GetDefaultPkgFolder()
154+
packagesFolder, err := common.PackagesFolder.Get()
150155
if err != nil {
151156
formatter.PrintError(err, "Cannot get packages folder.")
152157
os.Exit(commands.ErrCoreConfig)
153158
}
154159
ctx.HardwareFolders = []string{packagesFolder}
155160

156-
toolsFolder, err := common.GetDefaultToolsFolder(packageName)
161+
toolsFolder, err := common.ToolsFolder(packageName).Get()
157162
if err != nil {
158163
formatter.PrintError(err, "Cannot get tools folder.")
159164
os.Exit(commands.ErrCoreConfig)
160165
}
161166
ctx.ToolsFolders = []string{toolsFolder}
162167

163-
librariesFolder, err := common.GetDefaultLibFolder()
168+
librariesFolder, err := common.LibrariesFolder.Get()
164169
if err != nil {
165170
formatter.PrintError(err, "Cannot get libraries folder.")
166171
os.Exit(commands.ErrCoreConfig)
@@ -222,8 +227,15 @@ func run(cmd *cobra.Command, args []string) {
222227

223228
// Will be deprecated.
224229
ctx.ArduinoAPIVersion = "10600"
230+
225231
// Check if Arduino IDE is installed and get it's libraries location.
226-
ideProperties, err := properties.Load(filepath.Join(common.ArduinoDataFolder, "preferences.txt"))
232+
dataFolder, err := common.ArduinoDataFolder.Get()
233+
if err != nil {
234+
formatter.PrintError(err, "Cannot locate arduino data folder.")
235+
os.Exit(commands.ErrCoreConfig)
236+
}
237+
238+
ideProperties, err := properties.Load(filepath.Join(dataFolder, "preferences.txt"))
227239
if err == nil {
228240
lastIdeSubProperties := ideProperties.SubTree("last").SubTree("ide")
229241
// Preferences can contain records from previous IDE versions. Find the latest one.

commands/core/core.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/bcmi-labs/arduino-cli/common/formatter/output"
3838
"github.com/bcmi-labs/arduino-cli/common/formatter/pretty_print"
3939
"github.com/bcmi-labs/arduino-cli/cores"
40+
"github.com/bcmi-labs/arduino-cli/pathutils"
4041
"github.com/sirupsen/logrus"
4142
"github.com/spf13/cobra"
4243
)
@@ -55,17 +56,17 @@ var command = &cobra.Command{
5556

5657
// getInstalledCores gets the installed cores and puts them in the output struct.
5758
func getInstalledCores(packageName string, cores *[]output.InstalledStuff) {
58-
getInstalledStuff(packageName, cores, common.GetDefaultCoresFolder)
59+
getInstalledStuff(cores, common.CoresFolder(packageName))
5960
}
6061

6162
// getInstalledTools gets the installed tools and puts them in the output struct.
6263
func getInstalledTools(packageName string, tools *[]output.InstalledStuff) {
63-
getInstalledStuff(packageName, tools, common.GetDefaultToolsFolder)
64+
getInstalledStuff(tools, common.ToolsFolder(packageName))
6465
}
6566

6667
// getInstalledStuff is a generic procedure to get installed cores or tools and put them in an output struct.
67-
func getInstalledStuff(packageName string, stuff *[]output.InstalledStuff, defaultFolderFunc func(string) (string, error)) {
68-
stuffHome, err := defaultFolderFunc(packageName)
68+
func getInstalledStuff(stuff *[]output.InstalledStuff, folder pathutils.Path) {
69+
stuffHome, err := folder.Get()
6970
if err != nil {
7071
logrus.WithError(err).Warn("Cannot get default folder")
7172
return

commands/core/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9898
WithField("Version", item.Release.VersionName()).
9999
Info("Installing tool")
100100

101-
toolRoot, err := common.GetDefaultToolsFolder(item.Package)
101+
toolRoot, err := common.ToolsFolder(item.Package).Get()
102102
if err != nil {
103103
formatter.PrintError(err, "Cannot get tool install path, try again.")
104104
os.Exit(commands.ErrCoreConfig)
@@ -138,7 +138,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
138138
WithField("Version", item.Release.VersionName()).
139139
Info("Installing core")
140140

141-
coreRoot, err := common.GetDefaultCoresFolder(item.Package)
141+
coreRoot, err := common.CoresFolder(item.Package).Get()
142142
if err != nil {
143143
formatter.PrintError(err, "Cannot get core install path, try again.")
144144
os.Exit(commands.ErrCoreConfig)

commands/core/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var listCommand = &cobra.Command{
5656

5757
func runListCommand(cmd *cobra.Command, args []string) {
5858
logrus.Info("Executing `arduino core list`")
59-
pkgHome, err := common.GetDefaultPkgFolder()
59+
pkgHome, err := common.PackagesFolder.Get()
6060
if err != nil {
6161
formatter.PrintError(err, "Cannot get packages folder.")
6262
os.Exit(commands.ErrCoreConfig)

commands/lib/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
8585
logrus.Info("Download finished")
8686

8787
logrus.Info("Installing")
88-
folder, err := common.GetDefaultLibFolder()
88+
folder, err := common.LibrariesFolder.Get()
8989
if err != nil {
9090
formatter.PrintError(err, "Cannot get default lib install path.")
9191
os.Exit(commands.ErrCoreConfig)

commands/lib/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var listCommand = &cobra.Command{
6363
func runListCommand(cmd *cobra.Command, args []string) {
6464
logrus.Info("Executing `arduino lib list`")
6565

66-
libHome, err := common.GetDefaultLibFolder()
66+
libHome, err := common.LibrariesFolder.Get()
6767
if err != nil {
6868
formatter.PrintError(err, "Cannot get libraries folder.")
6969
os.Exit(commands.ErrCoreConfig)

commands/lib/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
7070
logrus.Info("Preparing")
7171
libs := libraries.ParseArgs(args)
7272

73-
libFolder, err := common.GetDefaultLibFolder()
73+
libFolder, err := common.LibrariesFolder.Get()
7474
if err != nil {
7575
formatter.PrintError(err, "Cannot get default libraries folder.")
7676
os.Exit(commands.ErrCoreConfig)

commands/root/root.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"os"
3535
"strings"
3636

37+
"github.com/bcmi-labs/arduino-cli/pathutils"
38+
3739
"github.com/bcmi-labs/arduino-cli/commands"
3840
"github.com/bcmi-labs/arduino-cli/commands/board"
3941
"github.com/bcmi-labs/arduino-cli/commands/compile"
@@ -170,19 +172,19 @@ func initConfigs() {
170172
}
171173
logrus.Info("Configuration set")
172174
commands.GlobalFlags.Configs = c
173-
common.ArduinoDataFolder = commands.GlobalFlags.Configs.ArduinoDataFolder
174-
common.ArduinoIDEFolder = configs.ArduinoIDEFolder
175-
common.SketchbookFolder = commands.GlobalFlags.Configs.SketchbookPath
175+
common.ArduinoDataFolder = pathutils.NewConstPath(commands.GlobalFlags.Configs.ArduinoDataFolder)
176+
common.ArduinoIDEFolder = pathutils.NewConstPath(configs.ArduinoIDEFolder)
177+
common.SketchbookFolder = pathutils.NewConstPath(commands.GlobalFlags.Configs.SketchbookPath)
176178
}
177179

178180
func initViper() {
179181
logrus.Info("Initiating viper config")
180182

181-
defHome, err := common.GetDefaultArduinoHomeFolder()
183+
defHome, err := common.ArduinoHomeFolder.Get()
182184
if err != nil {
183185
commands.ErrLogrus.WithError(err).Warn("Cannot get default Arduino Home")
184186
}
185-
defArduinoData, err := common.GetDefaultArduinoFolder()
187+
defArduinoData, err := common.ArduinoDataFolder.Get()
186188
if err != nil {
187189
logrus.WithError(err).Warn("Cannot get default Arduino folder")
188190
}

commands/sketch/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var syncCommand = &cobra.Command{
8181
func runSyncCommand(cmd *cobra.Command, args []string) {
8282
logrus.Info("Executing `arduino sketch sync`")
8383

84-
sketchbook, err := common.GetDefaultArduinoHomeFolder()
84+
sketchbook, err := common.ArduinoHomeFolder.Get()
8585
if err != nil {
8686
formatter.PrintError(err, "Cannot get sketchbook folder.")
8787
os.Exit(commands.ErrCoreConfig)

commands/validate/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var command = &cobra.Command{
5959

6060
func run(cmd *cobra.Command, args []string) {
6161
logrus.Info("Executing `arduino validate`")
62-
packagesFolder, err := common.GetDefaultPkgFolder()
62+
packagesFolder, err := common.PackagesFolder.Get()
6363
if err != nil {
6464
formatter.PrintError(err, "Cannot get packages folder.")
6565
os.Exit(commands.ErrCoreConfig)

common/net_functions.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ import (
3737
"net/http"
3838
"os"
3939
"time"
40+
41+
"github.com/bcmi-labs/arduino-cli/pathutils"
4042
)
4143

4244
// DownloadIndex is a function to download a generic index.
43-
func DownloadIndex(indexPathFunc func() (string, error), URL string) error {
44-
file, err := indexPathFunc()
45+
func DownloadIndex(indexPath pathutils.Path, URL string) error {
46+
file, err := indexPath.Get()
4547
if err != nil {
4648
return err
4749
}

0 commit comments

Comments
 (0)