Skip to content

Commit 84583e5

Browse files
committed
Added new command check-registry
1 parent 2d95155 commit 84583e5

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

internal/cli/check-registry.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 [email protected].
23+
24+
package cli
25+
26+
import (
27+
checkregistry "github.com/arduino/libraries-repository-engine/internal/cli/check-registry"
28+
"github.com/spf13/cobra"
29+
)
30+
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
38+
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)
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package checkregistry
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
8+
"github.com/arduino/libraries-repository-engine/internal/libraries"
9+
)
10+
11+
// CheckRegistry runs the check-registry action
12+
func CheckRegistry(reposFile string) {
13+
info, err := os.Stat(reposFile)
14+
if err != nil {
15+
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
16+
os.Exit(1)
17+
}
18+
19+
if info.IsDir() {
20+
fmt.Fprintf(os.Stderr, "error: Registry data file argument %s is a folder, not a file\n", reposFile)
21+
os.Exit(1)
22+
}
23+
24+
rawRepos, err := libraries.LoadRepoListFromFile(reposFile)
25+
if err != nil {
26+
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
27+
os.Exit(1)
28+
}
29+
30+
filteredRepos, err := libraries.ListRepos(reposFile)
31+
if err != nil {
32+
fmt.Fprintf(os.Stderr, "error: While filtering registry data file: %s\n", err)
33+
os.Exit(1)
34+
}
35+
36+
if !reflect.DeepEqual(rawRepos, filteredRepos) {
37+
fmt.Fprintln(os.Stderr, "error: Registry data file contains duplicate URLs")
38+
os.Exit(1)
39+
}
40+
41+
validTypes := map[string]bool{
42+
"Arduino": true,
43+
"Contributed": true,
44+
"Partner": true,
45+
"Recommended": true,
46+
"Retired": true,
47+
}
48+
49+
nameMap := make(map[string]bool)
50+
for _, entry := range rawRepos {
51+
// Check entry types
52+
if len(entry.Types) == 0 {
53+
fmt.Fprintf(os.Stderr, "error: Type not specified for library '%s'\n", entry.LibraryName)
54+
os.Exit(1)
55+
}
56+
for _, entryType := range entry.Types {
57+
if _, valid := validTypes[entryType]; !valid {
58+
fmt.Fprintf(os.Stderr, "error: Invalid type '%s' used by library '%s'\n", entryType, entry.LibraryName)
59+
os.Exit(1)
60+
}
61+
}
62+
63+
// Check library name of the entry
64+
if _, found := nameMap[entry.LibraryName]; found {
65+
fmt.Fprintf(os.Stderr, "error: Registry data file contains duplicates of name '%s'\n", entry.LibraryName)
66+
os.Exit(1)
67+
}
68+
nameMap[entry.LibraryName] = true
69+
}
70+
}

0 commit comments

Comments
 (0)