PakhomovAlexander commented on code in PR #897:
URL: https://github.com/apache/ignite-3/pull/897#discussion_r907841659


##########
modules/cli/src/main/java/org/apache/ignite/cli/config/ini/IniConfigManager.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cli.config.ini;
+
+import static org.apache.ignite.cli.config.ConfigConstants.CLUSTER_URL;
+import static org.apache.ignite.cli.config.ConfigConstants.CURRENT_PROFILE;
+import static org.apache.ignite.cli.config.ConfigConstants.JDBC_URL;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.NoSuchElementException;
+import org.apache.ignite.cli.config.ConfigConstants;
+import org.apache.ignite.cli.config.ConfigInitializationException;
+import org.apache.ignite.cli.config.ConfigManager;
+import org.apache.ignite.cli.config.Profile;
+import org.apache.ignite.cli.config.ProfileNotFoundException;
+
+/**
+ * Implementation of {@link ConfigManager} based on {@link IniFile}.
+ */
+public class IniConfigManager implements ConfigManager {
+    private static final String INTERNAL_SECTION_NAME = "ignitecli_internal";
+
+    private final IniFile configFile;
+
+    private String currentProfileName;
+
+    /**
+     * Constructor.
+     *
+     * @param file ini file.
+     */
+    public IniConfigManager(File file) {
+        IniFile configFile;
+        try {
+            configFile = new IniFile(file);
+            findCurrentProfile(configFile);
+        } catch (IOException | NoSuchElementException e) {
+            configFile = createDefaultConfig();

Review Comment:
   Shall we log that something went wrong during the initialization and we 
apply some fallback logic?



##########
modules/cli/src/test/java/org/apache/ignite/cli/commands/cliconfig/CliConfigCreateProfileCommandTest.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cli.commands.cliconfig;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.assertAll;
+
+import jakarta.inject.Inject;
+import org.apache.ignite.cli.commands.CliCommandTestBase;
+import org.apache.ignite.cli.config.ProfileNotFoundException;
+import org.apache.ignite.cli.config.ini.IniConfigManager;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CliConfigCreateProfileCommandTest extends CliCommandTestBase {
+    @Inject
+    TestConfigManagerProvider configManagerProvider;
+
+    @Override
+    protected Class<?> getCommandClass() {
+        return CliConfigCreateProfileCommand.class;
+    }
+
+    @BeforeEach
+    void configManagerRefresh() {
+        configManagerProvider.configManager = new 
IniConfigManager(TestConfigManagerHelper.createSectionWithInternalPart());
+    }
+
+    @Test
+    public void testWithoutProfileName() {
+        execute();
+
+        assertAll(
+                this::assertOutputIsEmpty,
+                () -> assertErrOutputContains("Missing required option: 
'--name=<profileName>")
+        );
+    }
+
+    @Test
+    public void testProfileCreation() {
+        execute("--name profileName");
+
+        assertAll(
+                this::assertErrOutputIsEmpty,
+                () -> assertOutputContains("Profile profileName was created 
successfully"),
+                () -> 
assertThat(configManagerProvider.get().getCurrentConfig().getName()).isNotEqualTo("profileName")
+        );
+    }
+
+    @Test
+    public void testProfileCopyFrom() {
+        execute("--name profileName --copy-from database");
+
+        assertAll(
+                this::assertErrOutputIsEmpty,
+                () -> assertOutputContains("Profile profileName was created 
successfully"),
+                () -> 
assertThat(configManagerProvider.get().getConfig("profileName").getAll()).containsAllEntriesOf(
+                        
configManagerProvider.get().getConfig("database").getAll())
+        );
+    }
+
+    @Test
+    public void testCopyFromNonExist() {
+        execute("--name profileName --copy-from notExist");
+
+        assertAll(
+                () -> assertExitCodeIs(1),
+                () -> assertErrOutputContains("Profile notExist not found"),
+                this::assertOutputIsEmpty,
+                () -> assertThatThrownBy(() -> 
configManagerProvider.get().getConfig("profileName"))
+                        .isInstanceOf(ProfileNotFoundException.class)
+        );
+    }
+
+    @Test
+    public void testProfileActivate() {
+        execute("--name profileName --activate");
+
+        assertAll(
+                () -> assertOutputContains("Profile profileName was created 
successfully"),
+                this::assertErrOutputIsEmpty,
+                () -> 
assertThat(configManagerProvider.get().getCurrentConfig().getName()).isEqualTo("profileName")
+        );
+    }
+
+    @Test
+    public void testCreateExistedProfile() {
+        execute("--name profileName");
+        execute("--name profileName");

Review Comment:
   In order to test double execution, it is better to do it step by step: 
   1) create a profile
   2) check the profile is created
   3) create the same profile again
   4) check the error



##########
modules/cli/src/main/java/org/apache/ignite/cli/config/ConfigManager.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cli.config;
+
+/**
+ * Manager of CLI config.
+ */
+public interface ConfigManager {
+    Profile getCurrentConfig();
+
+    Profile getConfig(String profile);

Review Comment:
   `getConfig` -> `getProfile` , `getCurrentConfig` -> `getCurrentProfile`



##########
modules/cli/src/main/java/org/apache/ignite/cli/commands/cliconfig/CliConfigActivateCommand.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cli.commands.cliconfig;
+
+import jakarta.inject.Inject;
+import java.util.concurrent.Callable;
+import org.apache.ignite.cli.call.cliconfig.CliConfigActivateCall;
+import org.apache.ignite.cli.commands.BaseCommand;
+import org.apache.ignite.cli.core.call.CallExecutionPipeline;
+import org.apache.ignite.cli.core.call.StringCallInput;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Parameters;
+
+/**
+ * Command for activate profile as current.
+ */
+@Command(name = "activate", description = "Profile activation command.")

Review Comment:
   Just thinking about the "use" name instead of "activate". It is much shorter 
and does not lose the meaning. What do you think?



##########
modules/cli/src/test/java/org/apache/ignite/cli/commands/CliCommandTestBase.java:
##########
@@ -46,7 +50,7 @@ public abstract class CliCommandTestBase {
 
     @BeforeEach
     public void setUp() {
-        cmd = new CommandLine(getCommandClass(), new 
MicronautFactory(context));
+        cmd = cache.computeIfAbsent(getCommandClass(), clazz -> new 
CommandLine(clazz, new MicronautFactory(context)));

Review Comment:
   It would be nice to add a comment on why we chache commands.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to