Skip to content

UpgradeTool: Add Yaml configuration upgrader to convert tags list to map #4762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 18, 2025

Conversation

jimtng
Copy link
Contributor

@jimtng jimtng commented Apr 28, 2025

  • Refactor Upgrade Tool so that each upgrader task is contained in a separate class
  • Add --list-commands parameter
  • Rename --dir to --userdata to accept a custom path to OPENHAB_USERDATA dir
  • Add --conf to accept a custom path to OPENHAB_CONF dir (See Set OPENHAB_CONF envvar for upgradetool openhab-distro#1740)
  • Avoid some null compiler warnings
  • Add a new upgrader to convert tags list to map structure in YAML configuration V1

Input file criteria:

  • Search only in CONF/tags/, or in the given directory, and its subdirectories
  • Contains a version key with value 1
  • it must contain a tags key that is a list
  • The tags list must contain a uid key
  • If the above criteria are not met, the file will not be modified

Output file will

  • Retain version: 1
  • convert tags list to a map with uid as key and the rest as map
  • Preserve the order of the tags
  • other keys will be unchanged
  • A backup of the original file will be created with the extension .yaml.org
  • If an .org file already exists, append a number to the end, e.g. yaml.org.1

See #3666 (comment)

Requires: openhab/openhab-distro#1740

@jimtng jimtng requested a review from a team as a code owner April 28, 2025 06:20
@jimtng
Copy link
Contributor Author

jimtng commented Apr 28, 2025

@lolodomo and @J-N-K

jimtng added 2 commits April 28, 2025 16:24
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng force-pushed the upgradetool-refactor branch from bf2f0da to 60844f8 Compare April 28, 2025 06:24
@@ -121,7 +101,7 @@ public void itemCopyUnitToMetadata() {
logger.debug("{}: Already contains a 'unit' metadata, skipping it", itemName);
} else {
String unit = null;
if (!noLink) {
if (linkStorage != null && thingStorage != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the test on noLink no more needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is equivalent, except that this one helps avoid compiler warnings about possible unchecked null value.

@lolodomo
Copy link
Contributor

Great job regarding tool refactoring to make it more modular.

I see your comment about the output loosing space and comments, that is a little annoying...

@jimtng jimtng changed the title Add Yaml configuration upgrader to convert tags list to map UpgradeTool: Add Yaml configuration upgrader to convert tags list to map Apr 29, 2025
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng force-pushed the upgradetool-refactor branch from 5079651 to d8b10b2 Compare April 30, 2025 09:45
@lolodomo
Copy link
Contributor

lolodomo commented May 11, 2025

If I run the upgrade tool just for upgrading tags, it requires the userdata folder. Can you avoid that?

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf D:/conf --command yamlTagsListToMap
Please either set the environment variable ${OPENHAB_USERDATA} or provide a directory through the --userdata option.

Then if I provide the userdata folder, it runs and converts my YAML file but also tries to generate JSON file in userdata filder , as if it was not running only YAML upgrade:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf D:/conf --command yamlTagsListToMap --userdata D:/conf/userdata
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'D:\conf'
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Checking D:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Found v1 yaml file with tags list D:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Converted D:\conf\tags\mesTags.yaml to map format, and the original file saved as D:\conf\tags\mesTags.yaml.org
[main] ERROR org.openhab.core.storage.json.internal.JsonStorage - Error writing JsonDB to D:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool. Cause D:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool (Le chemin d▒acc▒s sp▒cifi▒ est introuvable).

I see I need to provide my paths with this syntax D:/conf. While I am on Windows, my first attempt was to use D:\conf but it does not work, the tool is then considering the conf folder in my current location. Is it possible either to accept Windows syntax or to reject any path containing \? It would be better than considering a wrong folder.

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

The userdata folder is required to save the current state of the upgrade tool, marking that the yaml tag step was performed.
See this error message:

[main] ERROR org.openhab.core.storage.json.internal.JsonStorage - Error writing JsonDB to D:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

I can make it so that when:

  • --command is specified, --userdata becomes optional, and when it isn't available, skip updating the upgrade record.

wdyt?

I see I need to provide my paths with this syntax D:/conf. While I am on Windows, my first attempt was to use D:\conf but it does not work, the tool is then considering the conf folder in my current location. Is it possible either to accept Windows syntax

Unfortunately I don't have any Windows machine to test Java's behaviour, but I'll investigate this.

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

@lolodomo can you try this on a jshell session on Windows please?

jshell> Path.of("D:\\Windows\\System", "Test").getParent().getParent()
$9 ==> null

On Linux "D:\\Windows\\System" is seen as a single path component, so its parent is null. On Windows, according to what I read, it should return "D:\\Windows".

@lolodomo
Copy link
Contributor

I can make it so that when:

* `--command` is specified, `--userdata` becomes optional, and when it isn't available, skip updating the upgrade record.

wdyt?

Looks like a good idea.

@lolodomo
Copy link
Contributor

@lolodomo can you try this on a jshell session on Windows please?

jshell> Path.of("D:\\Windows\\System", "Test").getParent().getParent()
$9 ==> null
$ jshell
|  Welcome to JShell -- Version 21.0.5
|  For an introduction type: /help intro

jshell> Path.of("D:\\Windows\\System", "Test").getParent().getParent()
$1 ==> D:\Windows

jshell>

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

$ jshell
| Welcome to JShell -- Version 21.0.5
| For an introduction type: /help intro

jshell> Path.of("D:\Windows\System", "Test").getParent().getParent()
$1 ==> D:\Windows

jshell>

That means that providing a Windows style path should work.

@lolodomo
Copy link
Contributor

That means that providing a Windows style path should work.

Maybe I have to doubled the back slashes ?

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

That means that providing a Windows style path should work.

Maybe I have to doubled the back slashes ?

No, it should be just standard one backslash. The doubling is just to escape it within Java's string literal.

It needs to work with the standard Windows path convention on Windows otherwise it's a bug.

Can you paste me the output when you give it D:\conf please?

You can run it from inside D:\conf in case it uses your current directory.

@lolodomo
Copy link
Contributor

Can you paste me the output when you give it D:\conf please?

With your last version.

Without userdata:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf d:\conf --command yamlTagsListToMap
Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.Objects.requireNonNull(Objects.java:233)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:215)
        at java.base/java.nio.file.Path.of(Path.java:148)
        at org.openhab.core.tools.UpgradeTool.main(UpgradeTool.java:122)

With userdata:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf d:\conf --command yamlTagsListToMap --userdata d:\conf\userdata
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'D:\dev\openhab4\git\openhab-core\tools\upgradetool\target\conf'
[main] WARN org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Failed to process D:\dev\openhab4\git\openhab-core\tools\upgradetool\target\conf: D:\dev\openhab4\git\openhab-core\tools\upgradetool\target\conf
[main] ERROR org.openhab.core.storage.json.internal.JsonStorage - Error writing JsonDB to d:confuserdata\jsondb\org.openhab.core.tools.UpgradeTool. Cause d:confuserdata\jsondb\org.openhab.core.tools.UpgradeTool (Le chemin d▒acc▒s sp▒cifi▒ est introuvable).

@lolodomo
Copy link
Contributor

Ok, I found the problem, I have to put my paths inside double quotes like that:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf "d:\conf" --command yamlTagsListToMap --userdata "d:\conf\userdata"
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'd:\conf'
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Checking d:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Found v1 yaml file with tags list d:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Converted d:\conf\tags\mesTags.yaml to map format, and the original file saved as d:\conf\tags\mesTags.yaml.org
[main] ERROR org.openhab.core.storage.json.internal.JsonStorage - Error writing JsonDB to d:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool. Cause d:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool (Le chemin d▒acc▒s sp▒cifi▒ est introuvable).

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

Windows command shell must be escaping backslashes?

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng force-pushed the upgradetool-refactor branch from 4283911 to de9a253 Compare May 11, 2025 11:14
@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

it now prints the directories before being interpreted by Path.of. Could you run it without double quotes? I'm curious what the program "sees".

@lolodomo
Copy link
Contributor

lolodomo commented May 11, 2025

It simply consider that `\c is c. The backslash is then ignored.

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf d:\conf --command yamlTagsListToMap --userdata d:\conf\userdata
[main] INFO org.openhab.core.tools.UpgradeTool - Using userdataDir: d:confuserdata
[main] INFO org.openhab.core.tools.UpgradeTool - Using confDir: d:conf

if I double the backslash without double quotes, it also works:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf d:\\conf --command yamlTagsListToMap --userdata d:\\conf\\userdata
[main] INFO org.openhab.core.tools.UpgradeTool - Using userdataDir: d:\conf\userdata
[main] INFO org.openhab.core.tools.UpgradeTool - Using confDir: d:\conf
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'd:\conf'
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Checking d:\conf\tags\mesTags.yaml
[main] ERROR org.openhab.core.storage.json.internal.JsonStorage - Error writing JsonDB to d:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool. Cause d:\conf\userdata\jsondb\org.openhab.core.tools.UpgradeTool (Le chemin d▒acc▒s sp▒cifi▒ est introuvable).

I believe there is nothing more to investigate regarding Windows path. It is working as expected, I just have to provide paths properly, either by using double quotes around my path or by doubling baskslashes.

Can you have a look regarding your idea to not log in JSON DB?

@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

I believe there is nothing more to investigate regarding Windows path.

I might print a warning if something like this is detected "[a-z]:[a-z]" - but that won't help if people gave a relative path e.g. foo\bar (which will be seen as foobar, so yeah not much can be done. I will mention the quotes in the help though.

Can you have a look regarding your idea to not log in JSON DB?

This was done a few commits back dd0fefb

So if you specified --command but not --userdata it won't try to save the state.

@lolodomo
Copy link
Contributor

This was done a few commits back dd0fefb

So if you specified --command but not --userdata it won't try to save the state.

It crashes in that case!

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf d:\\conf --command yamlTagsListToMap
[main] INFO org.openhab.core.tools.UpgradeTool - Using confDir: d:\conf
Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.Objects.requireNonNull(Objects.java:233)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:215)
        at java.base/java.nio.file.Path.of(Path.java:148)
        at org.openhab.core.tools.UpgradeTool.main(UpgradeTool.java:126)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng
Copy link
Contributor Author

jimtng commented May 11, 2025

fixed

@lolodomo
Copy link
Contributor

Looks good now:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf "d:\conf" --command yamlTagsListToMap
[main] INFO org.openhab.core.tools.UpgradeTool - Using confDir: d:\conf
[main] ERROR org.openhab.core.tools.UpgradeTool - Upgrade records storage is not initialized.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'd:\conf'
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Checking d:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Found v1 yaml file with tags list d:\conf\tags\mesTags.yaml
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Converted d:\conf\tags\mesTags.yaml to map format, and the original file saved as d:\conf\tags\mesTags.yaml.org
[main] ERROR org.openhab.core.tools.UpgradeTool - Upgrade records storage is not initialized.

I am going to look at your code changes.

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng
Copy link
Contributor Author

jimtng commented May 12, 2025

I've modified the directory behaviour so that:

  • userdata and conf dir settings will default to 'userdata' and 'conf' subdirectory of the current working directory, if not specified and no environment variable is set. This way it can be executed from "above" conf dir without specifying it. I'm not sure if this is all that useful though. I don't want it to default to the "current" directory, because that could unintentionally scan the whole drive if you're running it from the root dir.
  • If the resolved directory doesn't exist, the corresponding "path" variable is set to null
  • each upgrader is responsible to check whether the directory that it needs is configured (i.e. not null)

Copy link
Contributor

@lolodomo lolodomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lolodomo
Copy link
Contributor

With the last changes, with a first call that do convert and a second call that do nothing:

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf "D:\conf"
[main] WARN org.openhab.core.tools.UpgradeTool - The 'userdata' directory 'D:\dev\openhab4\git\openhab-core\tools\upgradetool\target\userdata' does not exist. Some tasks may fail. To set it, either set the environment variable ${OPENHAB_USERDATA} or provide a directory through the --userdata option.
[main] WARN org.openhab.core.tools.UpgradeTool - Upgrade records storage is not initialized.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing itemCopyUnitToMetadata: Copy item unit from state description to metadata
[main] ERROR org.openhab.core.tools.internal.ItemUnitToMetadataUpgrader - itemCopyUnitToMetadata skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing linkUpgradeJSProfile: Upgrade JS profile configuration to new format
[main] ERROR org.openhab.core.tools.internal.JSProfileUpgrader - linkUpgradeJSProfile skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing linkUpgradeScriptProfile: Upgrade script profile configuration toHandlerScript to commandFromItemScript
[main] ERROR org.openhab.core.tools.internal.ScriptProfileUpgrader - linkUpgradeScriptProfile skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'D:\conf'
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - D:\conf\tags\mesTags.yaml converted to map format, and the original file saved as D:\conf\tags\mesTags.yaml.org

$ java -jar upgradetool-5.0.0-SNAPSHOT-jar-with-dependencies.jar --conf "D:\conf"
[main] WARN org.openhab.core.tools.UpgradeTool - The 'userdata' directory 'D:\dev\openhab4\git\openhab-core\tools\upgradetool\target\userdata' does not exist. Some tasks may fail. To set it, either set the environment variable ${OPENHAB_USERDATA} or provide a directory through the --userdata option.
[main] WARN org.openhab.core.tools.UpgradeTool - Upgrade records storage is not initialized.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing itemCopyUnitToMetadata: Copy item unit from state description to metadata
[main] ERROR org.openhab.core.tools.internal.ItemUnitToMetadataUpgrader - itemCopyUnitToMetadata skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing linkUpgradeJSProfile: Upgrade JS profile configuration to new format
[main] ERROR org.openhab.core.tools.internal.JSProfileUpgrader - linkUpgradeJSProfile skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing linkUpgradeScriptProfile: Upgrade script profile configuration toHandlerScript to commandFromItemScript
[main] ERROR org.openhab.core.tools.internal.ScriptProfileUpgrader - linkUpgradeScriptProfile skipped: no userdata directory found.
[main] INFO org.openhab.core.tools.UpgradeTool - Executing yamlTagsListToMap: Upgrade YAML 'tags' list to map format on V1 configuration files
[main] INFO org.openhab.core.tools.internal.YamlConfigurationV1TagsUpgrader - Upgrading YAML tags configurations in 'D:\conf'

@jimtng
Copy link
Contributor Author

jimtng commented May 18, 2025

With the last changes, with a first call that do convert and a second call that do nothing:

Was this a request to change something?

@lolodomo
Copy link
Contributor

Was this a request to change something?

Not at all, it was just to show how it behaves as expected ;)

@kaikreuzer kaikreuzer requested a review from Copilot May 18, 2025 17:01
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Upgrade Tool by splitting each upgrade task into its own class, adds new CLI options, and introduces a YAML tags upgrader.

  • Introduce Upgrader interface and move each upgrade task into a dedicated class
  • Add YamlConfigurationV1TagsUpgrader to convert V1 YAML tags lists into maps
  • Enhance UpgradeTool with --list-commands, --userdata, and --conf options

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tools/upgradetool/src/main/java/org/openhab/core/tools/internal/YamlConfigurationV1TagsUpgrader.java New class converting YAML tags list to map
tools/upgradetool/src/main/java/org/openhab/core/tools/internal/ScriptProfileUpgrader.java Extract script profile upgrader into its own class
tools/upgradetool/src/main/java/org/openhab/core/tools/internal/JSProfileUpgrader.java Extract JS profile upgrader into its own class
tools/upgradetool/src/main/java/org/openhab/core/tools/internal/ItemUnitToMetadataUpgrader.java Extract item-unit-to-metadata upgrader into its own class
tools/upgradetool/src/main/java/org/openhab/core/tools/Upgrader.java Add Upgrader interface
tools/upgradetool/src/main/java/org/openhab/core/tools/UpgradeTool.java Load all upgraders dynamically; add new CLI options
tools/upgradetool/pom.xml Add Jackson YAML dependencies

return path;
} else {
logger.warn(
"The '{}' directory '{}' does not exist. Some tasks may fail. To set it, either set the environment variable ${{}} or provide a directory through the --{} option.",
Copy link
Preview

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placeholder $ {{}} in the log message won’t print the environment variable name correctly. Replace it with a standard {} placeholder so env is substituted properly.

Suggested change
"The '{}' directory '{}' does not exist. Some tasks may fail. To set it, either set the environment variable ${{}} or provide a directory through the --{} option.",
"The '{}' directory '{}' does not exist. Some tasks may fail. To set it, either set the environment variable '{}' or provide a directory through the --{} option.",

Copilot uses AI. Check for mistakes.

JsonStorage<ItemChannelLink> linkStorage = new JsonStorage<>(linkJsonDatabasePath.toFile(), null, 5, 0, 0,
List.of());

List.copyOf(linkStorage.getKeys()).forEach(linkUid -> {
Copy link
Preview

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The iteration and update logic in ScriptProfileUpgrader and JSProfileUpgrader are nearly identical. Consider extracting common storage traversal and update steps into a shared utility or abstract base class to reduce duplication.

Copilot uses AI. Check for mistakes.

String command = commandLine.hasOption(OPT_COMMAND) ? commandLine.getOptionValue(OPT_COMMAND) : null;

if (command != null && UPGRADERS.stream().filter(u -> u.getName().equals(command)).findAny().isEmpty()) {
println("Unknown command: " + command);
System.exit(0);
Copy link
Preview

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an unknown command is encountered, it’s better to exit with a non-zero status (System.exit(1)) to signal failure rather than 0.

Suggested change
System.exit(0);
System.exit(1);

Copilot uses AI. Check for mistakes.

Comment on lines +162 to +178
fileContent.properties().forEach(entry -> {
String key = entry.getKey();
JsonNode node = entry.getValue();
if (key.equals("tags")) {
ObjectNode tagsMap = objectMapper.createObjectNode();
for (JsonNode tag : node) {
if (tag.hasNonNull("uid")) {
String uid = tag.get("uid").asText();
((ObjectNode) tag).remove("uid");
tagsMap.set(uid, tag);
} else {
logger.warn("Tag {} does not have a uid, skipping", tag);
}
}
((ObjectNode) fileContent).set(key, tagsMap);
}
});
Copy link
Preview

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Iterating over all properties to find and replace tags is less direct. You can retrieve the tags node directly via fileContent.get("tags"), transform it, and then replace it without scanning every property.

Suggested change
fileContent.properties().forEach(entry -> {
String key = entry.getKey();
JsonNode node = entry.getValue();
if (key.equals("tags")) {
ObjectNode tagsMap = objectMapper.createObjectNode();
for (JsonNode tag : node) {
if (tag.hasNonNull("uid")) {
String uid = tag.get("uid").asText();
((ObjectNode) tag).remove("uid");
tagsMap.set(uid, tag);
} else {
logger.warn("Tag {} does not have a uid, skipping", tag);
}
}
((ObjectNode) fileContent).set(key, tagsMap);
}
});
if (tagsNode != null && tagsNode.isArray()) {
ObjectNode tagsMap = objectMapper.createObjectNode();
for (JsonNode tag : tagsNode) {
if (tag.hasNonNull("uid")) {
String uid = tag.get("uid").asText();
((ObjectNode) tag).remove("uid");
tagsMap.set(uid, tag);
} else {
logger.warn("Tag {} does not have a uid, skipping", tag);
}
}
((ObjectNode) fileContent).set("tags", tagsMap);
}

Copilot uses AI. Check for mistakes.

Copy link
Member

@kaikreuzer kaikreuzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take the copilot comments as an fyi, there doesn't seem to be any problematic in the code.

@kaikreuzer kaikreuzer merged commit ff2cafa into openhab:main May 18, 2025
4 checks passed
@kaikreuzer kaikreuzer added the enhancement An enhancement or new feature of the Core label May 18, 2025
@kaikreuzer kaikreuzer added this to the 5.0 milestone May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants