Skip to content

Commit a0f0326

Browse files
committed
Move URL parsing in utility function
1 parent 7edf79e commit a0f0326

File tree

3 files changed

+93
-23
lines changed

3 files changed

+93
-23
lines changed

arduino/utils/url.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package utils
17+
18+
import (
19+
"net/url"
20+
"runtime"
21+
)
22+
23+
// URLParse parses a raw URL string and handles local files URLs depending on the platform
24+
func URLParse(rawURL string) (*url.URL, error) {
25+
URL, err := url.Parse(rawURL)
26+
if err != nil {
27+
return nil, err
28+
}
29+
if URL.Scheme == "file" && runtime.GOOS == "windows" {
30+
// Parsed local file URLs on Windows are returned with a leading /
31+
// so we remove it
32+
URL.Path = URL.Path[1:]
33+
}
34+
return URL, nil
35+
}

arduino/utils/url_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package utils
17+
18+
import (
19+
"fmt"
20+
"runtime"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestURLParse(t *testing.T) {
27+
type test struct {
28+
URL string
29+
ExpectedHost string
30+
ExpectedPath string
31+
Skip bool
32+
}
33+
onWindows := runtime.GOOS == "Windows"
34+
tests := []test{
35+
{"https://example.com", "example.com", "", false},
36+
{"https://example.com/some/path", "example.com", "/some/path", false},
37+
{"file:///home/user/some/path", "", "/home/user/some/path", onWindows},
38+
{"file:///C:/Users/me/some/path", "", "C:/Users/me/some/path", !onWindows},
39+
}
40+
41+
for i, test := range tests {
42+
t.Run(fmt.Sprintf("URLParseTest%02d", i), func(t *testing.T) {
43+
if test.Skip {
44+
t.Skip("Skipped")
45+
}
46+
res, err := URLParse(test.URL)
47+
require.NoError(t, err)
48+
require.Equal(t, test.ExpectedPath, res.Path)
49+
})
50+
}
51+
}

commands/instances.go

+7-23
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"net/url"
2424
"os"
2525
"path"
26-
"runtime"
2726

2827
"github.com/arduino/arduino-cli/arduino/builder"
2928
"github.com/arduino/arduino-cli/arduino/cores"
@@ -33,6 +32,7 @@ import (
3332
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
3433
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
3534
"github.com/arduino/arduino-cli/arduino/security"
35+
"github.com/arduino/arduino-cli/arduino/utils"
3636
"github.com/arduino/arduino-cli/cli/globals"
3737
"github.com/arduino/arduino-cli/configuration"
3838
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -207,7 +207,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
207207
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
208208
for _, u := range urls {
209209
logrus.Info("URL: ", u)
210-
URL, err := url.Parse(u)
210+
URL, err := utils.URLParse(u)
211211
if err != nil {
212212
logrus.Warnf("unable to parse additional URL: %s", u)
213213
continue
@@ -217,23 +217,13 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
217217

218218
if URL.Scheme == "file" {
219219
path := paths.New(URL.Path)
220-
if runtime.GOOS == "windows" {
221-
// Parsed local file URLs on Windows are returned with a leading /
222-
// so we remove it
223-
path = paths.New(URL.Path[1:])
224-
}
225-
pathJSON, err := path.Abs()
226-
if err != nil {
227-
return nil, fmt.Errorf("can't get absolute path of %v: %w", path, err)
228-
}
229-
230-
if _, err := packageindex.LoadIndexNoSign(pathJSON); err != nil {
231-
return nil, fmt.Errorf("invalid package index in %s: %s", pathJSON, err)
220+
if _, err := packageindex.LoadIndexNoSign(path); err != nil {
221+
return nil, fmt.Errorf("invalid package index in %s: %s", path, err)
232222
}
233223

234224
fi, _ := os.Stat(path.String())
235225
downloadCB(&rpc.DownloadProgress{
236-
File: "Updating index: " + pathJSON.Base(),
226+
File: "Updating index: " + path.Base(),
237227
TotalSize: fi.Size(),
238228
})
239229
downloadCB(&rpc.DownloadProgress{Completed: true})
@@ -667,25 +657,19 @@ func createInstance(ctx context.Context, getLibOnly bool) (*createInstanceResult
667657
urls := []string{globals.DefaultIndexURL}
668658
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
669659
for _, u := range urls {
670-
URL, err := url.Parse(u)
660+
URL, err := utils.URLParse(u)
671661
if err != nil {
672662
logrus.Warnf("Unable to parse index URL: %s, skip...", u)
673663
continue
674664
}
675665

676666
if URL.Scheme == "file" {
677667
path := paths.New(URL.Path)
678-
if runtime.GOOS == "windows" {
679-
// Parsed local file URLs on Windows are returned with a leading /
680-
// so we remove it
681-
path = paths.New(URL.Path[1:])
682-
}
683-
pathJSON, err := path.Abs()
684668
if err != nil {
685669
return nil, fmt.Errorf("can't get absolute path of %v: %w", path, err)
686670
}
687671

688-
_, err = res.Pm.LoadPackageIndexFromFile(pathJSON)
672+
_, err = res.Pm.LoadPackageIndexFromFile(path)
689673
if err != nil {
690674
res.PlatformIndexErrors = append(res.PlatformIndexErrors, err.Error())
691675
}

0 commit comments

Comments
 (0)