-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from arduino/cleanups
Some cleanups to the codebase
- Loading branch information
Showing
26 changed files
with
403 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// This file is part of libraries-repository-engine. | ||
// | ||
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
|
@@ -21,21 +21,26 @@ | |
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package libraries | ||
package cli | ||
|
||
import ( | ||
"github.com/arduino/libraries-repository-engine/internal/libraries" | ||
checkregistry "github.com/arduino/libraries-repository-engine/internal/command/check-registry" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// LoadRepoListFromFile returns an unfiltered list of library registry entries loaded from the given data file. | ||
func LoadRepoListFromFile(filename string) ([]*Repo, error) { | ||
return libraries.LoadRepoListFromFile(filename) | ||
} | ||
|
||
// Repo is the type for the library repository data. | ||
type Repo = libraries.Repo | ||
func init() { | ||
// checkRegistryCmd defines the `check-registry` CLI subcommand. | ||
var checkRegistryCmd = &cobra.Command{ | ||
Short: "Check the registry.txt file format", | ||
Long: "Check the registry.txt file format", | ||
DisableFlagsInUseLine: true, | ||
Use: `check-registry FLAG... /path/to/registry.txt | ||
// ListRepos returns a filtered list of library registry entries loaded from the given data file. | ||
func ListRepos(reposFilename string) ([]*Repo, error) { | ||
return libraries.ListRepos(reposFilename) | ||
Validate the registry.txt format and correctness.`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
checkregistry.CheckRegistry(args[0]) | ||
}, | ||
} | ||
rootCmd.AddCommand(checkRegistryCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// This file is part of libraries-repository-engine. | ||
// | ||
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package checkregistry | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/arduino/libraries-repository-engine/internal/libraries" | ||
) | ||
|
||
// CheckRegistry runs the check-registry action | ||
func CheckRegistry(reposFile string) { | ||
if err := runcheck(reposFile); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func runcheck(reposFile string) error { | ||
info, err := os.Stat(reposFile) | ||
if err != nil { | ||
return fmt.Errorf("while loading registry data file: %w", err) | ||
} | ||
|
||
if info.IsDir() { | ||
return fmt.Errorf("registry data file argument %s is a folder, not a file", reposFile) | ||
} | ||
|
||
rawRepos, err := libraries.LoadRepoListFromFile(reposFile) | ||
if err != nil { | ||
return fmt.Errorf("while loading registry data file: %w", err) | ||
} | ||
|
||
filteredRepos, err := libraries.ListRepos(reposFile) | ||
if err != nil { | ||
return fmt.Errorf("while filtering registry data file: %w", err) | ||
} | ||
|
||
if !reflect.DeepEqual(rawRepos, filteredRepos) { | ||
return errors.New("registry data file contains duplicate URLs") | ||
} | ||
|
||
validTypes := map[string]bool{ | ||
"Arduino": true, | ||
"Contributed": true, | ||
"Partner": true, | ||
"Recommended": true, | ||
"Retired": true, | ||
} | ||
|
||
nameMap := make(map[string]bool) | ||
for _, entry := range rawRepos { | ||
// Check entry types | ||
if len(entry.Types) == 0 { | ||
return fmt.Errorf("type not specified for library '%s'", entry.LibraryName) | ||
} | ||
for _, entryType := range entry.Types { | ||
if _, valid := validTypes[entryType]; !valid { | ||
return fmt.Errorf("invalid type '%s' used by library '%s'", entryType, entry.LibraryName) | ||
} | ||
} | ||
|
||
// Check library name of the entry | ||
if _, found := nameMap[entry.LibraryName]; found { | ||
return fmt.Errorf("registry data file contains duplicates of name '%s'", entry.LibraryName) | ||
} | ||
nameMap[entry.LibraryName] = true | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// This file is part of libraries-repository-engine. | ||
// | ||
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package checkregistry | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRegistryValidation(t *testing.T) { | ||
type testcase struct { | ||
Name string | ||
TestFile string | ||
ExpectedResult string | ||
} | ||
tests := []testcase{ | ||
{"EmptyArg", "", "registry data file argument testdata is a folder, not a file"}, | ||
{"NonExistentFile", "nonexistent.txt", "while loading registry data file: stat testdata/nonexistent.txt: no such file or directory"}, | ||
{"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"}, | ||
{"InvalidUrlFormat", "invalid-url-format.txt", "while filtering registry data file: Following URL are unknown or unsupported git repos:\nhttps://github.com/arduino-libraries/SD"}, | ||
{"MissingType", "no-type.txt", "invalid type '' used by library 'SD'"}, | ||
{"InvalidType", "invalid-type.txt", "invalid type 'foo' used by library 'SD'"}, | ||
{"DuplicateRepoURL", "duplicate-url.txt", "registry data file contains duplicate URLs"}, | ||
{"DuplicateLibName", "duplicate-name.txt", "registry data file contains duplicates of name 'SD'"}, | ||
{"ValidList", "valid.txt", ""}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
err := runcheck(filepath.Join("testdata", test.TestFile)) | ||
if test.ExpectedResult == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.EqualError(t, err, test.ExpectedResult) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git|Partner|SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo | ||
https://github.com/arduino-libraries/Foo.git|Contributed|SD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git|Partner|SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo | ||
https://github.com/arduino-libraries/SD.git|Contributed|Foo |
3 changes: 3 additions & 0 deletions
3
internal/command/check-registry/testdata/invalid-data-format.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git|Partner;SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git|foo|SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo |
3 changes: 3 additions & 0 deletions
3
internal/command/check-registry/testdata/invalid-url-format.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD|Partner|SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git||SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler | ||
https://github.com/arduino-libraries/SD.git|Partner|SD | ||
https://github.com/arduino-libraries/Servo.git|Recommended|Servo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.