Skip to content

Commit 4ebc8a0

Browse files
committed
Add types modification capability to modify command
Although much of the Library Manager content is based on the contents of the individual library repositories, some is defined during the library registration, and so a change to this after the time of the registration requires direct action by a maintainer. A `--types` flag is now added to the command to allow the library types list (e.g., "Arduino", "Contributed", "Retired") to be modified in the releases database for the given library. ``` libraries-repository-engine modify --types=NEW_TYPES LIBRARY_NAME ``` will change the types list of all releases of the library name specified via the positional argument (LIBRARY_NAME) to the new list specified via the `--types` flag's comma separated list (NEW_FLAGS).
1 parent a8e130b commit 4ebc8a0

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

internal/cli/modify.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Modify the registration data of library name LIBRARY_NAME according to the FLAGs
4242

4343
func init() {
4444
modifyCmd.Flags().String("repo-url", "", "New library repository URL")
45+
modifyCmd.Flags().String("types", "", "New types list for the library's releases (comma separated)")
4546

4647
rootCmd.AddCommand(modifyCmd)
4748
}

internal/command/modify/modify.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package modify
2727
import (
2828
"fmt"
2929
"os"
30+
"strings"
3031

3132
"github.com/arduino/go-paths-helper"
3233
"github.com/arduino/libraries-repository-engine/internal/backup"
@@ -121,6 +122,10 @@ func modifications(flags *pflag.FlagSet) (bool, error) {
121122
if err != nil {
122123
return false, err
123124
}
125+
newTypes, err := flags.GetString("types")
126+
if err != nil {
127+
return false, err
128+
}
124129

125130
if newRepositoryURL != "" {
126131
if err := modifyRepositoryURL(newRepositoryURL); err != nil {
@@ -130,6 +135,14 @@ func modifications(flags *pflag.FlagSet) (bool, error) {
130135
didModify = true
131136
}
132137

138+
if newTypes != "" {
139+
if err := modifyTypes(newTypes); err != nil {
140+
return false, err
141+
}
142+
143+
didModify = true
144+
}
145+
133146
if !didModify {
134147
return false, fmt.Errorf("No modification flags provided so nothing happened. See 'libraries-repository-engine modify --help'")
135148
}
@@ -215,3 +228,48 @@ func modifyRepositoryURL(newRepositoryURL string) error {
215228

216229
return nil
217230
}
231+
232+
func modifyTypes(rawTypes string) error {
233+
newTypes := strings.Split(rawTypes, ",")
234+
for i := range newTypes {
235+
newTypes[i] = strings.TrimSpace(newTypes[i])
236+
}
237+
238+
sameTypes := func(oldTypes []string) bool {
239+
if len(oldTypes) != len(newTypes) {
240+
return false
241+
}
242+
243+
for _, oldType := range oldTypes {
244+
found := false
245+
for _, newType := range newTypes {
246+
if oldType == newType {
247+
found = true
248+
break
249+
}
250+
}
251+
if !found {
252+
return false
253+
}
254+
}
255+
256+
return true
257+
}
258+
259+
typesChanged := false
260+
261+
for _, releaseData := range releasesData {
262+
if !typesChanged {
263+
// Compare old and new types for this release
264+
typesChanged = !sameTypes(releaseData.Types)
265+
}
266+
267+
releaseData.Types = newTypes
268+
}
269+
270+
if !typesChanged {
271+
return fmt.Errorf("Library %s already has types %s", libraryName, rawTypes)
272+
}
273+
274+
return nil
275+
}

test/test_modify.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,65 @@ def get_release_archive_url(name, version):
333333
)
334334
assert canary_release_archive_path.exists()
335335
assert get_release_archive_url(name=canary_name, version=canary_release) == canary_release_archive_url
336+
337+
338+
def test_types(configuration, run_command):
339+
"""Test the `--types` modification flag in action."""
340+
name = "SpacebrewYun"
341+
raw_old_types = "Arduino"
342+
raw_new_types = "Arduino, Retired"
343+
canary_name = "Arduino Uno WiFi Dev Ed Library"
344+
raw_canary_types = "Partner"
345+
# Run the sync command to generate test data
346+
engine_command = [
347+
"sync",
348+
"--config-file",
349+
configuration.path,
350+
test_data_path.joinpath("test_modify", "test_types", "repos.txt"),
351+
]
352+
result = run_command(cmd=engine_command)
353+
assert result.ok
354+
assert pathlib.Path(configuration.data["LibrariesDB"]).exists()
355+
356+
def assert_types(name, raw_types):
357+
with pathlib.Path(configuration.data["LibrariesDB"]).open(mode="r", encoding="utf-8") as library_db_file:
358+
library_db = json.load(fp=library_db_file)
359+
for release in library_db["Releases"]:
360+
if release["LibraryName"] == name and release["Types"] != [
361+
raw_type.strip() for raw_type in raw_types.split(sep=",")
362+
]:
363+
return False
364+
return True
365+
366+
# Verify the pre-command DB is as expected
367+
assert assert_types(name=name, raw_types=raw_old_types)
368+
assert assert_types(name=canary_name, raw_types=raw_canary_types)
369+
370+
# Run the modification command with existing types
371+
engine_command = [
372+
"modify",
373+
"--config-file",
374+
configuration.path,
375+
"--types",
376+
raw_old_types,
377+
name,
378+
]
379+
result = run_command(cmd=engine_command)
380+
assert not result.ok
381+
assert f"{name} already has types {raw_old_types}" in result.stderr
382+
383+
# Run the modification command with existing types
384+
engine_command = [
385+
"modify",
386+
"--config-file",
387+
configuration.path,
388+
"--types",
389+
raw_new_types,
390+
name,
391+
]
392+
result = run_command(cmd=engine_command)
393+
assert result.ok
394+
395+
# Verify the effect of the command was as expected
396+
assert assert_types(name=name, raw_types=raw_new_types)
397+
assert assert_types(name=canary_name, raw_types=raw_canary_types)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
https://github.com/arduino-libraries/SpacebrewYun.git|Arduino|SpacebrewYun
2+
https://github.com/arduino-libraries/UnoWiFi-Developer-Edition-Lib.git|Partner|Arduino Uno WiFi Dev Ed Library

0 commit comments

Comments
 (0)