2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.main;
19 import java.io.IOException;
20 import java.nio.charset.Charset;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.Optional;
29 import org.apache.commons.io.IOUtils;
30 import org.onap.cli.fw.cmd.OnapCommand;
31 import org.onap.cli.fw.conf.OnapCommandConfig;
32 import org.onap.cli.fw.conf.OnapCommandConstants;
33 import org.onap.cli.fw.error.OnapCommandException;
34 import org.onap.cli.fw.error.OnapCommandHelpFailed;
35 import org.onap.cli.fw.error.OnapCommandInvalidSample;
36 import org.onap.cli.fw.error.OnapCommandWarning;
37 import org.onap.cli.fw.input.OnapCommandParameter;
38 import org.onap.cli.fw.output.OnapCommandPrintDirection;
39 import org.onap.cli.fw.output.OnapCommandResult;
40 import org.onap.cli.fw.output.OnapCommandResultAttribute;
41 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
42 import org.onap.cli.fw.output.OnapCommandResultType;
43 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
44 import org.onap.cli.fw.store.OnapCommandExecutionStore;
45 import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext;
46 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
47 import org.onap.cli.main.conf.OnapCliConstants;
48 import org.onap.cli.main.interactive.StringCompleter;
49 import org.onap.cli.main.utils.OnapCliArgsParser;
50 import org.onap.cli.sample.yaml.SampleYamlGenerator;
51 import org.open.infc.grpc.Result;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import jline.TerminalFactory;
56 import jline.console.ConsoleReader;
59 * OCLIP Command Line Interface (CLI).
62 public class OnapCli {
64 private static Logger log = LoggerFactory.getLogger(OnapCli.class);
66 private List<String> args = new ArrayList<>();
68 private List<String> argsParamFile = new ArrayList<>();
70 private String product = null;
72 private String profile = null;
74 private String paramFile = null;
76 private String rpcHost = null;
78 private String rpcPort = null;
80 private boolean printHelp = false;
82 private boolean printVersion = false;
84 private String requestId = null;
86 private String cmdName = null;
88 private int exitCode = -1;
90 public OnapCli(String[] args) {
97 public void resetExitCode() {
101 public void setArgs(String [] args) {
102 //--help --version --requestId --rpc-host xxx --rpc-port xxx --product xxx --profile xxx --param-file xxx CMD blah blah
104 int cmdIdx = 0; //index of CMD
105 while(args.length > cmdIdx) {
106 //no options given, directly command name invoked
107 if (!args[cmdIdx].startsWith("-")) {
111 if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PRODUCT))) {
112 this.product = args[++cmdIdx];
113 cmdIdx++; //move to next option
114 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PROFILE))) {
115 this.profile = args[++cmdIdx];
116 cmdIdx++; //move to next option
117 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_HOST))) {
118 this.rpcHost = args[++cmdIdx];
119 cmdIdx++; //move to next option
120 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PORT))) {
121 this.rpcPort = args[++cmdIdx];
122 cmdIdx++; //move to next option
123 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_PARAM_FILE_LONG))) {
124 this.paramFile = args[++cmdIdx];
125 cmdIdx++; //move to next option
126 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_REQID))) {
127 this.requestId = args[++cmdIdx];
128 cmdIdx++; //move to next option
129 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_HELP_LOGN)) ||
130 args[cmdIdx].equals(OnapCommandParameter.printShortOption(OnapCliConstants.PARAM_HELP_SHORT))) {
131 this.printHelp = true;
132 cmdIdx++; //move to next option
133 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_VERSION_LONG)) ||
134 args[cmdIdx].equals(OnapCommandParameter.printShortOption(OnapCliConstants.PARAM_VERSION_SHORT))) {
135 this.printVersion = true;
136 cmdIdx++; //move to next option
140 if (args.length > cmdIdx) {
141 this.cmdName = args[cmdIdx];
147 //add all args starting from the command name
148 for (int i=cmdIdx; i<args.length; i++) {
149 this.args.add(args[i]);
153 private void exitSuccessfully() {
154 this.exitCode = OnapCliConstants.EXIT_SUCCESS;
157 private void exitFailure() {
158 this.exitCode = OnapCliConstants.EXIT_FAILURE;
161 protected void print(String msg) {
162 System.out.println(msg); //NOSONAR
165 protected void printerr(String msg) {
166 System.err.println(msg); //NOSONAR
169 private void print(Throwable throwable) {
170 String error = throwable.getMessage() != null ? throwable.getMessage() : "";
172 log.error(error, throwable);
175 private String getShortOption(String opt) {
176 return OnapCommandParameter.printShortOption(opt);
179 private String getLongOption(String opt) {
180 return OnapCommandParameter.printLongOption(opt);
183 public int getExitCode() {
184 return this.exitCode;
188 * Handles help. --help or -h
190 public void handleHelp() {
192 if (this.printHelp) {
193 this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt") , (Charset) null));
194 String help = OnapCommandRegistrar.getRegistrar().getHelp();
196 this.exitSuccessfully();
198 } catch (Exception e) {
205 * Handles version. --version or -v
207 public void handleVersion() {
209 if (this.printVersion) {
210 String version = OnapCommandRegistrar.getRegistrar().getVersion();
212 this.exitSuccessfully();
214 } catch (Exception e) {
222 * Handles profile. --profile
224 public void handleProfile() {
226 if (this.profile != null) {
227 OnapCommandRegistrar.getRegistrar().setProfile(
232 } catch (Exception e) {
239 * Handles batch command. --param-file
241 public void handleBatchCommand() {
243 if (this.paramFile != null) {
244 //Read YAML and loop thru it
246 // - param-long-option-1: value
247 // - param-long-option-1: value
251 // - param-long-option-1: value
252 // - param-long-option-1: value
256 Map<String, Object> values = OnapCommandDiscoveryUtils.loadYaml(this.paramFile);
258 for (Entry<String, Object> cmdsParam: values.entrySet()) {
259 for (Object param: (List)cmdsParam.getValue()) {
260 if (param instanceof Map) { //optional args
261 Map <String, String> paramMap = (Map<String, String>) param;
262 String paramName = paramMap.keySet().iterator().next();
263 Object paramValue = paramMap.get(paramName);
264 argsParamFile.add(this.getLongOption(paramName));
265 argsParamFile.add(paramValue.toString());
266 } else { //positional args
267 argsParamFile.add(param.toString());
272 } catch (Exception e) { // NOSONAR
273 this.print("Failed to read param file " + this.paramFile);
277 } catch (Exception e) {
283 public void verifyCommand(OnapCommand cmd) throws OnapCommandException {
285 OnapCliArgsParser.populateParams(cmd.getParameters(), args);
287 Optional<OnapCommandParameter> contextOptArg = cmd.getParameters().stream()
288 .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
291 List<Map<String, Object>> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite(
293 cmd.getInfo().getProduct());
295 OnapCommandResult testSuiteResult = new OnapCommandResult();
296 testSuiteResult.setType(OnapCommandResultType.TABLE);
297 testSuiteResult.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
298 testSuiteResult.setIncludeTitle(true);
300 OnapCommandResultAttribute sampleFileAtt = new OnapCommandResultAttribute();
301 OnapCommandResultAttribute sampleIdAtt = new OnapCommandResultAttribute();
302 OnapCommandResultAttribute resultAtt = new OnapCommandResultAttribute();
304 sampleFileAtt.setName("Test");
305 sampleIdAtt.setName("SampleId");
306 resultAtt.setName("Result");
308 testSuiteResult.setRecords(Arrays.asList(sampleFileAtt,
312 for (Map<String, ?> sampleTest : testSuite) {
314 sampleFileAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID));
315 sampleIdAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_ID));
317 cmd = OnapCommandRegistrar.getRegistrar().get(cmd.getName(),
318 cmd.getInfo().getProduct());
319 List<String> arguments = (List<String>) sampleTest.get(OnapCommandConstants.VERIFY_INPUT);
321 OnapCliArgsParser.populateParams(cmd.getParameters(), arguments);
322 this.print("\n***************Test Command: \n" + sampleTest.get(OnapCommandConstants.VERIFY_INPUT).toString());
324 cmd.getParametersMap().get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).setValue(Boolean.TRUE);
326 Optional<OnapCommandParameter> contextOpt = cmd.getParameters().stream()
327 .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
330 if (contextOpt.isPresent()) {
331 HashMap<String, Object> map = new HashMap<>();
333 Object moco = sampleTest.get(OnapCommandConstants.VERIFY_MOCO);
337 map.put(OnapCommandConstants.VERIFY_MOCO, moco);
339 if (contextOptArg.isPresent()) {
340 OnapCommandParameter contextArg = contextOptArg.get();
341 map.putAll((Map) contextArg.getValue());
344 contextOpt.get().setValue(map);
347 OnapCommandResult testResult = cmd.execute();
348 String actualOutput = testResult.print().trim();
349 String expectedOutput = (String) sampleTest.get(OnapCommandConstants.VERIFY_OUPUT);
350 expectedOutput = expectedOutput == null ? "" : expectedOutput.trim();
352 if (actualOutput.equals(expectedOutput)) {
353 resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_PASS);
355 resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_FAIL);
357 this.printerr(testResult.getDebugInfo());
358 this.print("\n***************Expected Output: \n" + expectedOutput);
359 this.print("\n***************Actual Output: \n" + actualOutput);
362 this.print(testSuiteResult.print());
366 * Handles Interactive Mode.
368 public void handleInteractive() { // NOSONAR
369 if (this.cmdName == null) {
370 ConsoleReader console = null;
372 OnapCommandRegistrar.getRegistrar().setInteractiveMode(true);
373 console = createConsoleReader();
376 while ((line = console.readLine()) != null) {
378 if (OnapCliConstants.PARAM_INTERACTIVE_EXIT.equalsIgnoreCase(line)) {
380 } else if (OnapCliConstants.PARAM_INTERACTIVE_CLEAR.equalsIgnoreCase(line)) {
381 console.clearScreen();
383 this.args = new ArrayList<>();
384 this.args.addAll(Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN)));
386 if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) {
387 if (args.size() == 1) {
388 this.print("Please use it in the form of use <product-version>.\nSupported versions: " +
389 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions());
391 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1));
392 console = createConsoleReader();
395 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_HELP)) {
396 this.print(OnapCommandRegistrar.getRegistrar().getHelpForEnabledProductVersion());
397 this.print(OnapCli.getDirectiveHelp());
398 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_VERSION)) {
399 this.printVersion = true;
402 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) {
403 if (args.size() == 1) {
404 this.print("Please use it in the form of 'profile <profile-name>'\n");
405 this.print("Available profiles: ");
406 this.print(OnapCommandRegistrar.getRegistrar().getUserProfiles().toString());
408 this.profile = args.get(1);
412 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_SET)) {
413 if (args.size() > 1) {
414 String[] paramEntry = args.get(1).trim().split("=", 2);
415 if (paramEntry.length == 2) {
416 OnapCommandRegistrar.getRegistrar().addParamCache(paramEntry[0].trim(), paramEntry[1].trim());
418 this.print("Please use it in the form of 'set <param-name>=<param-value>'");
421 this.print(OnapCommandRegistrar.getRegistrar().getParamCache().toString());
424 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_UNSET)) {
425 if (args.size() > 1) {
426 for (int i = 1; i < args.size(); i++) {
427 OnapCommandRegistrar.getRegistrar().removeParamCache(args.get(i));
431 if (!(args.size() == 1 && args.get(0).trim().isEmpty())) {
432 this.setArgs(this.args.toArray(new String[]{}));
438 } catch (IOException e) { // NOSONAR
439 this.print("Failed to read console, " + e.getMessage());
440 } catch (OnapCommandException e) {
445 TerminalFactory.get().restore();
446 } catch (Exception e) { // NOSONAR
448 this.exitSuccessfully();
456 public void handleCommand() {
457 OnapCommand cmd = null;
458 if (this.cmdName != null) {
460 if (this.product != null) {
461 cmd = OnapCommandRegistrar.getRegistrar().get(this.cmdName, this.product);
463 cmd = OnapCommandRegistrar.getRegistrar().get(this.cmdName);
465 } catch (Exception e) {
471 ExecutionStoreContext executionStoreContext = null;
474 //Registrar identified this command marked with rpc as true and it will make direct RPC command call...
475 if (cmd.isRpc() && !this.cmdName.equals("schema-rpc")) {
476 this.handleRpcCommand(cmd);
481 if(args.contains(OnapCommandConstants.VERIFY_LONG_OPTION)
482 || args.contains(OnapCommandConstants.VERIFY_SHORT_OPTION)) {
484 this.exitSuccessfully();
487 // check for help or version
488 if (args.size() == 2) {
489 if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1))
490 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(1))) {
491 String help = cmd.printHelp();
493 this.exitSuccessfully();
495 } else if (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(1))
496 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(1))) {
497 String version = cmd.printVersion();
499 this.exitSuccessfully();
504 //refer params from profile
505 if (this.profile != null) {
507 Map<String, String> paramCache = new HashMap<>(); //NOSONAR
508 if (this.product == null)
509 paramCache = OnapCommandRegistrar.getRegistrar().getParamCache();
511 paramCache = OnapCommandRegistrar.getRegistrar().getParamCache(this.product);
513 for (OnapCommandParameter param: cmd.getParameters()) {
514 if (paramCache.containsKey(
515 cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())) {
516 param.setValue(paramCache.get(
517 cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption()));
518 } else if (paramCache.containsKey(
519 cmd.getInfo().getService() + ":" + param.getLongOption())) {
520 param.setValue(paramCache.get(
521 cmd.getInfo().getService() + ":" + param.getLongOption()));
522 } else if (paramCache.containsKey(param.getLongOption())) {
523 param.setValue(paramCache.get(param.getLongOption()));
528 //load the parameters value from the map read from param-file
529 if (!this.argsParamFile.isEmpty()) {
530 OnapCliArgsParser.populateParams(cmd.getParameters(), this.argsParamFile);
533 OnapCliArgsParser.populateParams(cmd.getParameters(), this.args);
535 //start the execution
536 if (this.requestId != null && this.product != null && !this.requestId.isEmpty()&& !(this.product.equalsIgnoreCase("open-cli") &&
537 this.cmdName.equalsIgnoreCase("execution-list"))) {
538 String input = cmd.getArgsJson(true);
539 executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart(
541 cmd.getInfo().getProduct(),
542 cmd.getInfo().getService(),
548 cmd.setExecutionContext(executionStoreContext);
549 OnapCommandResult result = cmd.execute();
551 this.handleTracking(cmd);
553 if (result.isPassed()) {
554 this.exitSuccessfully();
555 generateSmapleYaml(cmd);
559 } catch (OnapCommandWarning w) {
560 if (cmd.getExecutionContext() != null) {
561 OnapCommandExecutionStore.getStore().storeExectutionEnd(
562 cmd.getExecutionContext(),
565 cmd.getResult().getDebugInfo(),
566 cmd.getResult().isPassed());
570 this.printerr(cmd.getResult().getDebugInfo());
571 this.exitSuccessfully();
572 } catch (Exception e) {
573 if (executionStoreContext != null) {
574 OnapCommandExecutionStore.getStore().storeExectutionEnd(
575 executionStoreContext,
578 cmd.getResult().getDebugInfo(),
583 this.printerr(cmd.getResult().getDebugInfo());
589 public void handleTracking(OnapCommand cmd) throws OnapCommandException {
590 if (cmd.getResult().isDebug())
591 this.printerr(cmd.getResult().getDebugInfo());
593 String printOut = cmd.getResult().print();
594 this.print(printOut);
596 if (cmd.getExecutionContext() != null) {
597 OnapCommandExecutionStore.getStore().storeExectutionEnd(
598 cmd.getExecutionContext(),
601 cmd.getResult().getDebugInfo(),
602 cmd.getResult().isPassed());
606 * When user invokes cli with RPC arguments...
608 public void handleRpc() {
609 if (this.rpcHost != null && this.rpcPort != null && this.product != null) {
611 OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("schema-rpc", "open-cli");
612 cmd.getParametersMap().get(OnapCommandConstants.RPC_HOST).setValue(this.rpcHost);
613 cmd.getParametersMap().get(OnapCommandConstants.RPC_PORT).setValue(this.rpcPort);
614 cmd.getParametersMap().get(OnapCommandConstants.RPC_PRODUCT).setValue(this.product);
615 cmd.getParametersMap().get(OnapCommandConstants.RPC_CMD).setValue(this.cmdName);
617 this.handleRpcCommand(cmd);
618 } catch (Exception e) {
625 private void handleRpcCommand(OnapCommand cmd) throws OnapCommandException {
626 Map<String, List<String>> argsMap = new HashMap<>();
627 argsMap.put(OnapCommandConstants.RPC_ARGS, this.args);
628 if (this.profile != null )
629 cmd.getParametersMap().get(OnapCommandConstants.RPC_PROFILE).setValue(this.profile);
630 cmd.getParametersMap().get(OnapCommandConstants.RPC_REQID).setValue(this.requestId);
631 cmd.getParametersMap().get(OnapCommandConstants.RPC_MODE).setValue(OnapCommandConstants.RPC_MODE_RUN_CLI);
632 cmd.getParametersMap().get(OnapCommandConstants.RPC_ARGS).setValue(argsMap);
634 OnapCommandResult result = cmd.execute();
635 Result output = (Result) result.getOutput();
637 this.exitCode = output.getExitCode();
638 this.print(output.getOutput());
641 * Handles all client input.
643 public void handle() {
646 if (this.exitCode == -1) {
650 if (this.exitCode == -1) {
651 this.handleVersion();
654 if (this.exitCode == -1) {
655 this.handleProfile();
658 if (this.exitCode == -1) {
659 this.handleBatchCommand();
662 if (this.exitCode == -1) {
663 this.handleInteractive();
666 if (this.exitCode == -1) {
667 this.handleCommand();
671 public static String getDirectiveHelp() throws OnapCommandHelpFailed {
672 OnapCommandResult help = new OnapCommandResult();
673 help.setType(OnapCommandResultType.TABLE);
674 help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
676 OnapCommandResultAttribute attr = new OnapCommandResultAttribute();
677 attr.setName(OnapCommandConstants.NAME.toUpperCase());
678 attr.setDescription(OnapCommandConstants.DESCRIPTION);
679 attr.setScope(OnapCommandResultAttributeScope.SHORT);
680 help.getRecords().add(attr);
682 OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
683 attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase());
684 attrDesc.setDescription(OnapCommandConstants.DESCRIPTION);
685 attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
686 help.getRecords().add(attrDesc);
688 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
689 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG);
691 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT);
692 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG);
694 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION);
695 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG);
697 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE);
698 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG);
700 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET);
701 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG);
703 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET);
704 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG);
706 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP);
707 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG);
709 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
710 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG);
712 return "\n\nDirectives:\n" + help.print();
713 } catch (OnapCommandException e) {
714 throw new OnapCommandHelpFailed(e);
719 * Creates console reader object.
721 * @return ConsoleReader
722 * @throws IOException
725 private ConsoleReader createConsoleReader() throws IOException {
726 ConsoleReader console = new ConsoleReader(); // NOSONAR
728 //ignore system commands
729 StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
730 strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
731 OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
732 OnapCliConstants.PARAM_INTERACTIVE_USE,
733 OnapCliConstants.PARAM_INTERACTIVE_HELP,
734 OnapCliConstants.PARAM_INTERACTIVE_VERSION,
735 OnapCliConstants.PARAM_INTERACTIVE_SET,
736 OnapCliConstants.PARAM_INTERACTIVE_UNSET,
737 OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
738 console.addCompleter(strCompleter);
739 console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
740 } catch (OnapCommandException e) { // NOSONAR
741 this.print("Failed to load oclip commands," + e.getMessage());
748 private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException {
749 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
751 SampleYamlGenerator.generateSampleYaml(cmd.getName(), args, cmd.getResult().print(),
752 cmd.getInfo().getProduct(),
753 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml",
754 cmd.getResult().isDebug(),
755 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_NAME));
756 } catch (IOException error) {
757 throw new OnapCommandInvalidSample(this.cmdName, error);
768 public static void main(String[] args) {
769 OnapCli cli = new OnapCli(args); //NOSONAR
771 System.exit(cli.getExitCode());