|
| 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 checkregistry |
| 25 | + |
| 26 | +import ( |
| 27 | + "fmt" |
| 28 | + "os" |
| 29 | + "reflect" |
| 30 | + |
| 31 | + "github.com/arduino/libraries-repository-engine/internal/libraries" |
| 32 | +) |
| 33 | + |
| 34 | +// CheckRegistry runs the check-registry action |
| 35 | +func CheckRegistry(reposFile string) { |
| 36 | + info, err := os.Stat(reposFile) |
| 37 | + if err != nil { |
| 38 | + fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + if info.IsDir() { |
| 43 | + fmt.Fprintf(os.Stderr, "error: Registry data file argument %s is a folder, not a file\n", reposFile) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + rawRepos, err := libraries.LoadRepoListFromFile(reposFile) |
| 48 | + if err != nil { |
| 49 | + fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + filteredRepos, err := libraries.ListRepos(reposFile) |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(os.Stderr, "error: While filtering registry data file: %s\n", err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + if !reflect.DeepEqual(rawRepos, filteredRepos) { |
| 60 | + fmt.Fprintln(os.Stderr, "error: Registry data file contains duplicate URLs") |
| 61 | + os.Exit(1) |
| 62 | + } |
| 63 | + |
| 64 | + validTypes := map[string]bool{ |
| 65 | + "Arduino": true, |
| 66 | + "Contributed": true, |
| 67 | + "Partner": true, |
| 68 | + "Recommended": true, |
| 69 | + "Retired": true, |
| 70 | + } |
| 71 | + |
| 72 | + nameMap := make(map[string]bool) |
| 73 | + for _, entry := range rawRepos { |
| 74 | + // Check entry types |
| 75 | + if len(entry.Types) == 0 { |
| 76 | + fmt.Fprintf(os.Stderr, "error: Type not specified for library '%s'\n", entry.LibraryName) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + for _, entryType := range entry.Types { |
| 80 | + if _, valid := validTypes[entryType]; !valid { |
| 81 | + fmt.Fprintf(os.Stderr, "error: Invalid type '%s' used by library '%s'\n", entryType, entry.LibraryName) |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Check library name of the entry |
| 87 | + if _, found := nameMap[entry.LibraryName]; found { |
| 88 | + fmt.Fprintf(os.Stderr, "error: Registry data file contains duplicates of name '%s'\n", entry.LibraryName) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + nameMap[entry.LibraryName] = true |
| 92 | + } |
| 93 | +} |
0 commit comments