Repository: quarkusio/quarkus
Issue: NPE when adding new registry in Quarkus CLI

### Describe the bug

When new registry is added into freshly installed Quarkus CLI, the operation fails with NullPointerException.

### Expected behavior

New registry is added successfully.

### Actual behavior

Exception in console:
```
java.lang.NullPointerException
	at io.quarkus.registry.config.RegistriesConfigMapperHelper.serialize(RegistriesConfigMapperHelper.java:45)
	at io.quarkus.registry.config.RegistriesConfig$Mutable.persist(RegistriesConfig.java:111)
	at io.quarkus.cli.RegistryAddCommand.call(RegistryAddCommand.java:54)
	at io.quarkus.cli.RegistryAddCommand.call(RegistryAddCommand.java:12)
	at picocli.CommandLine.executeUserObject(CommandLine.java:1953)
	at picocli.CommandLine.access$1300(CommandLine.java:145)
	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2358)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2352)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2314)
	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
	at picocli.CommandLine$RunLast.execute(CommandLine.java:2316)
	at picocli.CommandLine.execute(CommandLine.java:2078)
	at io.quarkus.cli.QuarkusCli.run(QuarkusCli.java:50)
	at io.quarkus.cli.QuarkusCli_ClientProxy.run(Unknown Source)
	at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:125)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:67)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:41)
	at io.quarkus.cli.Main.main(Main.java:9)
```

### How to Reproduce?

Steps to reproduce:

1. `jbang app uninstall quarkus`
2. `rm -r ~/.quarkus/`
3. `jbang app install --fresh --force quarkus@quarkusio`
4. 
```
$ quarkus version
2.6.1.Final # 2.5.2.Final worked as expected
```
5. `quarkus registry add registry.quarkus.redhat.com`

### Output of `uname -a` or `ver`

OS name: "linux", version: "5.15.6-100.fc34.x86_64"

### Output of `java -version`

11.0.13, vendor: Eclipse Adoptium

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

_No response_

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

_No response_

### Additional information

_No response_

Changed files:
independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigLocator.java
devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java
independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigImpl.java

diff --git a/devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java b/devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java
index 016147cb321..7ce770d8546 100644
--- a/devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java
+++ b/devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java
@@ -30,16 +30,13 @@ public Integer call() throws Exception {
             existingConfig = Files.exists(configYaml);
         }
 
+        registryClient.refreshRegistryCache(output);
         final RegistriesConfig.Mutable config;
-        if (existingConfig) {
-            registryClient.refreshRegistryCache(output);
-            config = registryClient.resolveConfig().mutable();
-            if (config.getSource().getFilePath() == null) {
-                output.error("Can only modify file-based configuration. Config source is " + config.getSource().describe());
-                return CommandLine.ExitCode.SOFTWARE;
-            }
-        } else {
+        if (configYaml != null && !existingConfig) {
+            // we're creating a new configuration for a new file
             config = RegistriesConfig.builder();
+        } else {
+            config = registryClient.resolveConfig().mutable();
         }
 
         boolean persist = false;
@@ -48,10 +45,10 @@ public Integer call() throws Exception {
         }
 
         if (persist) {
-            if (existingConfig) {
-                config.persist();
-            } else {
+            if (configYaml != null) {
                 config.persist(configYaml);
+            } else {
+                config.persist();
             }
         }
         return CommandLine.ExitCode.OK;
diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigImpl.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigImpl.java
index dd969f9982e..495e8f3827e 100644
--- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigImpl.java
+++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigImpl.java
@@ -217,7 +217,9 @@ public String toString() {
 
     static void persistConfigSource(RegistriesConfigImpl config) throws IOException {
         Path targetFile = config.configSource.getFilePath();
-        if (targetFile == null) {
+        if (config.configSource == ConfigSource.DEFAULT) {
+            targetFile = RegistriesConfigLocator.getDefaultConfigYamlLocation();
+        } else if (targetFile == null) {
             throw new UnsupportedOperationException(
                     String.format("Can not write configuration as it was read from an alternate source: %s",
                             config.configSource.describe()));
diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigLocator.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigLocator.java
index f26ac04d706..66ce462cf0d 100644
--- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigLocator.java
+++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/config/RegistriesConfigLocator.java
@@ -159,8 +159,6 @@ public static RegistryConfig getDefaultRegistry() {
     }
 
     /**
-     * TODO: compare set *.json.RegistriesConfigLocator#initFromEnvironmentOrNull
-     * 
      * @param map A Map containing environment variables, e.g. {@link System#getenv()}
      * @return A RegistriesConfig object initialized from environment variables.
      */