Skip to content
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

TASK-5055 - ERROR CLI Opencga-enterprise #2354

Merged
merged 11 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions opencga-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
<include>compileR.sh</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>cli-usage.yml</include>
</includes>
<targetPath>${project.basedir}/target/classes</targetPath>
</resource>
<resource>
<directory>app/cloud/docker/compose</directory>
<filtering>true</filtering>
Expand Down Expand Up @@ -346,6 +353,7 @@
<arg value="${project.basedir}/../opencga-core/target/classes/log4j2.internal.xml"/>
<arg value="${project.basedir}/../opencga-storage/opencga-storage-core/target/classes/storage-configuration.yml"/>
<arg value="${project.basedir}/../opencga-client/target/classes/client-configuration.yml"/>
<!-- <arg value="${project.basedir}/src/main/resources/usage.yml"/>-->
<arg value="${build.dir}/conf"/>
</exec>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opencb.opencga.app.cli.config;

import java.util.Arrays;

public class CliCategory {

private String name;
private String description;
private String[] options;

public CliCategory() {
}

public CliCategory(String name, String description, String[] options) {
this.name = name;
this.description = description;
this.options = options;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Category{");
sb.append("name='").append(name).append('\'');
sb.append(", description='").append(description).append('\'');
sb.append(", options=").append(Arrays.toString(options));
sb.append('}');
return sb.toString();
}

public String getName() {
return name;
}

public CliCategory setName(String name) {
this.name = name;
return this;
}

public String getDescription() {
return description;
}

public CliCategory setDescription(String description) {
this.description = description;
return this;
}

public String[] getOptions() {
return options;
}

public CliCategory setOptions(String[] options) {
this.options = options;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.opencb.opencga.app.cli.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;

public class CliConfiguration {

/**
* The instance of Configuration that this Class is storing
*/
private static CliConfiguration instance;

/**
* The instance of Usage that stores the "usage" information
*/
private CliUsage cliUsage;

/**
* FILENAME is the file location of the configuration yml file
*/
private String cliUsageFileName = "cli-usage.yml";

/**
* LOGGER is an instance of the Logger class so that we can do proper
* logging
*/
private static final Logger logger = LoggerFactory.getLogger(CliConfiguration.class);

public static CliConfiguration getInstance() {
if (CliConfiguration.instance == null) {
CliConfiguration.instance = new CliConfiguration();
}
return CliConfiguration.instance;
}

private CliUsage loadConfiguration() throws IOException {
try (InputStream is = getClass().getClassLoader().getResourceAsStream(cliUsageFileName)) {
// Mapping the config from the YAML file to the Configuration class
ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
return yamlObjectMapper.readValue(is, CliUsage.class);
}
}

/*
* We keep an instance of cliUsage for the Shell
*/
public CliUsage getUsage() {
if (cliUsage == null) {
try {
cliUsage = loadConfiguration();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return cliUsage;
}

public void setUsage(CliUsage cliUsage) {
this.cliUsage = cliUsage;
}

public String getCliUsageFileName() {
return cliUsageFileName;
}

public void setCliUsageFileName(String cliUsageFileName) {
this.cliUsageFileName = cliUsageFileName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.opencb.opencga.app.cli.config;

import java.util.Arrays;

public class CliUsage {

CliCategory[] categories;

public CliUsage() {
}

public CliUsage(CliCategory[] categories) {
this.categories = categories;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Usage{");
sb.append("categories=").append(Arrays.toString(categories));
sb.append('}');
return sb.toString();
}

public CliCategory[] getCategories() {
return categories;
}

public CliUsage setCategories(CliCategory[] categories) {
this.categories = categories;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023-09-12 OpenCB
* Copyright 2015-2023-10-13 OpenCB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023-09-12 OpenCB
* Copyright 2015-2023-10-13 OpenCB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public static void main(String[] args) {
}
}



private static void checkMode(String[] args) {
if (ArrayUtils.contains(args, "--shell")) {
setMode(Mode.SHELL);
Expand Down Expand Up @@ -130,7 +132,7 @@ public static void executeShell(String[] args) {
}
Shell.printShellHeaderMessage();
// Create a shell executor instance
shell = new Shell(options);
shell = new Shell(options, new OpenCgaCompleterImpl(), new CommandProcessor());
logger.debug("Shell created ");
// Launch execute command to begin the execution
shell.execute();
Expand Down Expand Up @@ -162,7 +164,7 @@ public static String[] parseCliParams(String[] args) {
}
logger.debug("CLI parsed params ::: " + CommandLineUtils.argsToString(args));
String shortcut = CommandLineUtils.getShortcut(args);
args = CommandLineUtils.processShortCuts(args);
args = CommandLineUtils.processShortCuts(args, new OpencgaCliOptionsParser());
if (args != null) {
logger.debug("Process shortcut result ::: " + CommandLineUtils.argsToString(args));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import org.opencb.opencga.app.cli.CliOptionsParser;
import org.opencb.opencga.app.cli.GeneralCliOptions;
import org.opencb.opencga.app.cli.admin.AdminCliOptionsParser;
import org.opencb.opencga.app.cli.config.CliCategory;
import org.opencb.opencga.app.cli.config.CliConfiguration;
import org.opencb.opencga.app.cli.config.CliUsage;
import org.opencb.opencga.app.cli.main.OpencgaMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomCliOptionsParser extends CliOptionsParser {

Expand Down Expand Up @@ -67,6 +72,7 @@ public void printUsage() {
} else {
PrintUtils.println(PrintUtils.getKeyValueAsFormattedString("Usage:", " " + getPrefix() + "[-h|--help] [--shell] [--host] [--version] <command> [options]"));
}

System.err.println();
printMainUsage();
System.err.println();
Expand Down Expand Up @@ -97,6 +103,40 @@ private String getPrefix() {
return "opencga.sh ";
}

/**
* Read cli-usage.yml file to print the usage.
*/
@Override
protected void printMainUsage() {
CliUsage cliUsage = CliConfiguration.getInstance().getUsage();
CliCategory[] categories = cliUsage.getCategories();
for (CliCategory cliCategory : categories) {
String[] options = cliCategory.getOptions();
PrintUtils.println(PrintUtils.format(cliCategory.getDescription(), PrintUtils.Color.GREEN));
for (String option : options) {
for (String command : jCommander.getCommands().keySet()) {
if (command.equals(option)) {
PrintUtils.printCommandHelpFormattedString(command, jCommander.getCommandDescription(command));
}
}
}
System.err.println();
}
printOpencgaCommands();
}

private void printOpencgaCommands() {
Map<String, String> opencgaCommands = getOpencgaCommands();
if (!OpencgaMain.isShellMode()) {
PrintUtils.println(PrintUtils.format("Opencga options:", PrintUtils.Color.GREEN));
} else {
PrintUtils.println(PrintUtils.format("Opencga commands:", PrintUtils.Color.GREEN));
}
for (Map.Entry entry : opencgaCommands.entrySet()) {
PrintUtils.printCommandHelpFormattedString(entry.getKey().toString(), entry.getValue().toString());
}
}
/*
@Override
protected void printMainUsage() {
Set<String> analysisCommands = new HashSet<>(Arrays.asList("alignments", "variant", "clinical"));
Expand Down Expand Up @@ -153,7 +193,7 @@ protected void printMainUsage() {
for (Map.Entry entry : opencgaCommands.entrySet()) {
PrintUtils.printCommandHelpFormattedString(entry.getKey().toString(), entry.getValue().toString());
}
}
}*/

private Map<String, String> getOpencgaCommands() {
Map<String, String> h = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.opencb.commons.utils.PrintUtils;
import org.opencb.opencga.app.cli.GeneralCliOptions;
import org.opencb.opencga.app.cli.main.OpenCgaCompleterImpl;
import org.opencb.opencga.app.cli.main.OpencgaCliOptionsParser;
import org.opencb.opencga.app.cli.main.OpencgaMain;
import org.opencb.opencga.app.cli.main.executors.OpencgaCommandExecutor;
import org.opencb.opencga.app.cli.main.processors.CommandProcessor;
Expand All @@ -36,13 +37,17 @@ public class Shell extends OpencgaCommandExecutor {


// Create a command processor to process all the shell commands
private final CommandProcessor processor = new CommandProcessor();
private final CommandProcessor processor;
private LineReader lineReader = null;
private Terminal terminal = null;
private String host = null;

public Shell(GeneralCliOptions.CommonCommandOptions options) throws CatalogAuthenticationException {
private Completer completer;

public Shell(GeneralCliOptions.CommonCommandOptions options, Completer completer, CommandProcessor processor) throws CatalogAuthenticationException {
super(options);
this.completer=completer;
this.processor=processor;
if (options.host != null) {
host = options.host;
}
Expand Down Expand Up @@ -104,7 +109,7 @@ private LineReader getTerminal() {
reader = LineReaderBuilder.builder()
.terminal(terminal)
.highlighter(new DefaultHighlighter())
.history(defaultHistory).completer(new OpenCgaCompleterImpl())
.history(defaultHistory).completer(this.completer)
.build();
} catch (Exception e) {
CommandLineUtils.error("Failed to create terminal ", e);
Expand Down Expand Up @@ -209,7 +214,7 @@ public String[] parseParams(String[] args) throws CatalogAuthenticationException
return args;
}
}
return CommandLineUtils.processShortCuts(args);
return CommandLineUtils.processShortCuts(args, new OpencgaCliOptionsParser());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.opencb.commons.utils.PrintUtils;
import org.opencb.opencga.app.cli.main.OpencgaCliOptionsParser;
import org.opencb.opencga.app.cli.main.OpencgaMain;
import org.opencb.opencga.app.cli.main.custom.CustomCliOptionsParser;
import org.opencb.opencga.core.common.GitRepositoryState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,8 +62,8 @@ public static void error(String message, Exception e) {
}
}

public static String[] processShortCuts(String[] args) {
OpencgaCliOptionsParser cliOptionsParser = new OpencgaCliOptionsParser();
public static String[] processShortCuts(String[] args, CustomCliOptionsParser parser) {

switch (getShortcut(args)) {
case "login":
return LoginUtils.parseLoginCommand(args);
Expand All @@ -78,7 +79,7 @@ public static String[] processShortCuts(String[] args) {
}
}
try {
cliOptionsParser.printUsage(args);
parser.printUsage(args);
} catch (Exception e) {
// malformed command
return args;
Expand Down
Loading