Skip to content

Commit a386e95

Browse files
authored
Merge pull request #293 from arduino/cleanups
Some cleanups to the codebase
2 parents 62cdbbf + a613262 commit a386e95

File tree

26 files changed

+403
-430
lines changed

26 files changed

+403
-430
lines changed
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of libraries-repository-engine.
22
//
3-
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
44
//
55
// This program is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU Affero General Public License as published
@@ -21,21 +21,26 @@
2121
// Arduino software without disclosing the source code of your own applications.
2222
// To purchase a commercial license, send an email to license@arduino.cc.
2323

24-
package libraries
24+
package cli
2525

2626
import (
27-
"github.com/arduino/libraries-repository-engine/internal/libraries"
27+
checkregistry "github.com/arduino/libraries-repository-engine/internal/command/check-registry"
28+
"github.com/spf13/cobra"
2829
)
2930

30-
// LoadRepoListFromFile returns an unfiltered list of library registry entries loaded from the given data file.
31-
func LoadRepoListFromFile(filename string) ([]*Repo, error) {
32-
return libraries.LoadRepoListFromFile(filename)
33-
}
34-
35-
// Repo is the type for the library repository data.
36-
type Repo = libraries.Repo
31+
func init() {
32+
// checkRegistryCmd defines the `check-registry` CLI subcommand.
33+
var checkRegistryCmd = &cobra.Command{
34+
Short: "Check the registry.txt file format",
35+
Long: "Check the registry.txt file format",
36+
DisableFlagsInUseLine: true,
37+
Use: `check-registry FLAG... /path/to/registry.txt
3738
38-
// ListRepos returns a filtered list of library registry entries loaded from the given data file.
39-
func ListRepos(reposFilename string) ([]*Repo, error) {
40-
return libraries.ListRepos(reposFilename)
39+
Validate the registry.txt format and correctness.`,
40+
Args: cobra.ExactArgs(1),
41+
Run: func(cmd *cobra.Command, args []string) {
42+
checkregistry.CheckRegistry(args[0])
43+
},
44+
}
45+
rootCmd.AddCommand(checkRegistryCmd)
4146
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to license@arduino.cc.
23+
24+
package checkregistry
25+
26+
import (
27+
"errors"
28+
"fmt"
29+
"os"
30+
"reflect"
31+
32+
"github.com/arduino/libraries-repository-engine/internal/libraries"
33+
)
34+
35+
// CheckRegistry runs the check-registry action
36+
func CheckRegistry(reposFile string) {
37+
if err := runcheck(reposFile); err != nil {
38+
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
39+
os.Exit(1)
40+
}
41+
}
42+
43+
func runcheck(reposFile string) error {
44+
info, err := os.Stat(reposFile)
45+
if err != nil {
46+
return fmt.Errorf("while loading registry data file: %w", err)
47+
}
48+
49+
if info.IsDir() {
50+
return fmt.Errorf("registry data file argument %s is a folder, not a file", reposFile)
51+
}
52+
53+
rawRepos, err := libraries.LoadRepoListFromFile(reposFile)
54+
if err != nil {
55+
return fmt.Errorf("while loading registry data file: %w", err)
56+
}
57+
58+
filteredRepos, err := libraries.ListRepos(reposFile)
59+
if err != nil {
60+
return fmt.Errorf("while filtering registry data file: %w", err)
61+
}
62+
63+
if !reflect.DeepEqual(rawRepos, filteredRepos) {
64+
return errors.New("registry data file contains duplicate URLs")
65+
}
66+
67+
validTypes := map[string]bool{
68+
"Arduino": true,
69+
"Contributed": true,
70+
"Partner": true,
71+
"Recommended": true,
72+
"Retired": true,
73+
}
74+
75+
nameMap := make(map[string]bool)
76+
for _, entry := range rawRepos {
77+
// Check entry types
78+
if len(entry.Types) == 0 {
79+
return fmt.Errorf("type not specified for library '%s'", entry.LibraryName)
80+
}
81+
for _, entryType := range entry.Types {
82+
if _, valid := validTypes[entryType]; !valid {
83+
return fmt.Errorf("invalid type '%s' used by library '%s'", entryType, entry.LibraryName)
84+
}
85+
}
86+
87+
// Check library name of the entry
88+
if _, found := nameMap[entry.LibraryName]; found {
89+
return fmt.Errorf("registry data file contains duplicates of name '%s'", entry.LibraryName)
90+
}
91+
nameMap[entry.LibraryName] = true
92+
}
93+
return nil
94+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to license@arduino.cc.
23+
24+
package checkregistry
25+
26+
import (
27+
"path/filepath"
28+
"testing"
29+
30+
"github.com/stretchr/testify/require"
31+
)
32+
33+
func TestRegistryValidation(t *testing.T) {
34+
type testcase struct {
35+
Name string
36+
TestFile string
37+
ExpectedResult string
38+
}
39+
tests := []testcase{
40+
{"EmptyArg", "", "registry data file argument testdata is a folder, not a file"},
41+
{"NonExistentFile", "nonexistent.txt", "while loading registry data file: stat testdata/nonexistent.txt: no such file or directory"},
42+
{"InvalidDataFormat", "invalid-data-format.txt", "while loading registry data file: invalid line format (3 fields are required): https://github.com/arduino-libraries/SD.git|Partner;SD"},
43+
{"InvalidUrlFormat", "invalid-url-format.txt", "while filtering registry data file: Following URL are unknown or unsupported git repos:\nhttps://github.com/arduino-libraries/SD"},
44+
{"MissingType", "no-type.txt", "invalid type '' used by library 'SD'"},
45+
{"InvalidType", "invalid-type.txt", "invalid type 'foo' used by library 'SD'"},
46+
{"DuplicateRepoURL", "duplicate-url.txt", "registry data file contains duplicate URLs"},
47+
{"DuplicateLibName", "duplicate-name.txt", "registry data file contains duplicates of name 'SD'"},
48+
{"ValidList", "valid.txt", ""},
49+
}
50+
for _, test := range tests {
51+
t.Run(test.Name, func(t *testing.T) {
52+
err := runcheck(filepath.Join("testdata", test.TestFile))
53+
if test.ExpectedResult == "" {
54+
require.NoError(t, err)
55+
} else {
56+
require.EqualError(t, err, test.ExpectedResult)
57+
}
58+
})
59+
}
60+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
4+
https://github.com/arduino-libraries/Foo.git|Contributed|SD
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
4+
https://github.com/arduino-libraries/SD.git|Contributed|Foo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner;SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|foo|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git||SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo

0 commit comments

Comments
 (0)