Add command use 05/9405/5
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Wed, 30 Aug 2017 12:28:56 +0000 (17:58 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 31 Aug 2017 05:26:58 +0000 (10:56 +0530)
use command helps to choose the product
version to use while there are more than
one product commands present in cli

CLI-37
Change-Id: I4020bbbcc7574cfcc73ddcd4d46c627087990d20
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java
framework/src/main/java/org/onap/cli/fw/conf/Constants.java
framework/src/main/java/org/onap/cli/fw/error/OnapCommandProductVersionInvalid.java [new file with mode: 0644]
framework/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand
main/src/main/java/org/onap/cli/main/OnapCli.java
main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java

index 56f04e1..39f684c 100644 (file)
@@ -31,6 +31,7 @@ import org.onap.cli.fw.error.OnapCommandException;
 import org.onap.cli.fw.error.OnapCommandHelpFailed;
 import org.onap.cli.fw.error.OnapCommandInvalidRegistration;
 import org.onap.cli.fw.error.OnapCommandNotFound;
+import org.onap.cli.fw.error.OnapCommandProductVersionInvalid;
 import org.onap.cli.fw.error.OnapCommandRegistrationFailed;
 import org.onap.cli.fw.error.OnapCommandRegistrationVersionMissing;
 import org.onap.cli.fw.output.OnapCommandResult;
@@ -54,6 +55,16 @@ public class OnapCommandRegistrar {
 
     private String enabledProductVersion = OnapCommandConfg.getEnabledProductVersion();
 
+    private boolean isInteractiveMode = false;
+
+    public boolean isInteractiveMode() {
+        return isInteractiveMode;
+    }
+
+    public void setInteractiveMode(boolean isInteractiveMode) {
+        this.isInteractiveMode = isInteractiveMode;
+    }
+
     private static OnapCommandRegistrar registrar = null;
 
     /**
@@ -126,7 +137,11 @@ public class OnapCommandRegistrar {
         return this.availableProductVersions;
     }
 
-    public void setEnabledProductVersion(String version) {
+    public void setEnabledProductVersion(String version) throws OnapCommandProductVersionInvalid {
+        if (!this.availableProductVersions.contains(version)) {
+            throw new OnapCommandProductVersionInvalid(version, availableProductVersions);
+        }
+
         this.enabledProductVersion = version;
     }
 
@@ -225,7 +240,8 @@ public class OnapCommandRegistrar {
         String errorNote = "";
         if (!this.availableProductVersions.contains(configuredProductVersion)) {
             errorNote = "** CUATION: Please configure the enabled product version to use one of " + this.availableProductVersions.toString() +
-                        ".\nTo enable a product version, set env variable CLI_PRODUCT_VERSION or cli.product.version in onap.properties";
+                        ".\nTo enable a product version, use one of following methods:\n\t 1. set env variable CLI_PRODUCT_VERSION"
+                        + "\n\t 2. set cli.product.version in onap.properties \n\t 3. in interactive mode, use the directive 'use <product version>'";
         }
         return "CLI version               : " + version + "\n"
                 + "Available product versions: " + this.availableProductVersions.toString() + "\n"
index 980b94d..d27649b 100644 (file)
@@ -192,6 +192,7 @@ public class Constants {
     public static final String HTTP_SUCCESS_CODE_INVALID = "Invalid http success code.";
     public static final String HTTP_SAMPLE_RESPONSE_EMPTY = "Sample response cann't be null or empty";
     public static final String HTTP_SAMPLE_RESPONSE_FAILED_PARSING = "The http Sample response json is failed to parse.";
+    public static final String USE_DIRECTIVE = "use";
 
     private Constants() {
     }
diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandProductVersionInvalid.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandProductVersionInvalid.java
new file mode 100644 (file)
index 0000000..7ec69e6
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed 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.onap.cli.fw.error;
+
+import java.util.Set;
+
+/**
+ * ONAP command product version is invalid
+ *
+ */
+public class OnapCommandProductVersionInvalid extends OnapCommandException {
+
+    private static final long serialVersionUID = 5513297861129088463L;
+
+    public OnapCommandProductVersionInvalid(String invalidVersion, Set<String> validVersions) {
+        super("0x0031", "Given product version " + invalidVersion + " is invalid. Please use one of " + validVersions);
+    }
+}
index 89648bf..422da56 100644 (file)
@@ -1,2 +1,2 @@
 org.onap.cli.fw.cmd.OnapSchemaValidateCommand
-org.onap.cli.fw.cmd.OnapSchemaRefreshCommand
\ No newline at end of file
+org.onap.cli.fw.cmd.OnapSchemaRefreshCommand
index 2695c3f..528c3e1 100644 (file)
@@ -119,8 +119,10 @@ public class OnapCli {
      */
     public void handleInteractive() { // NOSONAR
         if (isInteractive()) {
+
             ConsoleReader console = null;
             try {
+                OnapCommandRegistrar.getRegistrar().setInteractiveMode(true);
                 console = createConsoleReader();
                 String line = null;
                 while ((line = console.readLine()) != null) {
@@ -132,10 +134,30 @@ public class OnapCli {
                         continue;
                     }
                     this.args = Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN));
+
+                    if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) {
+                        if (args.size() == 1) {
+                            this.print("Please input the product version to use, supported versions: " +
+                        OnapCommandRegistrar.getRegistrar().getAvailableProductVersions());
+                        } else {
+                            try {
+                                OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1));
+                                console.close();
+                                console = createConsoleReader();
+                            } catch (OnapCommandException e) {
+                                this.print(e);
+                            }
+                        }
+
+                        continue;
+                    }
                     handleCommand();
                 }
             } catch (IOException e) { // NOSONAR
                 this.print("Failed to read console, " + e.getMessage());
+            } catch (OnapCommandException e) {
+                this.print(e);
+                this.exitFailure();
             } finally {
                 try {
                     TerminalFactory.get().restore();
@@ -175,7 +197,8 @@ public class OnapCli {
         try {
             StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
             strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
-                    OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
+                    OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
+                    OnapCliConstants.PARAM_INTERACTIVE_USE);
             console.addCompleter(strCompleter);
             console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT);
         } catch (OnapCommandException e) { // NOSONAR
index 7fb51cb..4db26f7 100644 (file)
@@ -33,6 +33,7 @@ public final class OnapCliConstants {
     public static final String PARAM_INTERACTIVE_EXIT = "exit";
     public static final String PARAM_INTERACTIVE_BYE = "bye";
     public static final String PARAM_INTERACTIVE_CLEAR = "clear";
+    public static final String PARAM_INTERACTIVE_USE = "use";
     public static final String PARAM_INTERACTIVE_ARG_SPLIT_PATTERN = "\\s+";
 
     private OnapCliConstants(){}