Skip to content

Commit d0b926b

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

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

internal/cli/check-registry.go

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

0 commit comments

Comments
 (0)