Skip to content

Commit

Permalink
Refactor new CLI Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
imedina committed Oct 17, 2023
1 parent c65fae6 commit 080dcf9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.opencb.opencga.app.cli.conf;
package org.opencb.opencga.app.cli.config;

import java.util.Arrays;

public class Category {
public class CliCategory {

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

public Category() {
public CliCategory() {
}

public Category(String name, String description, String[] options) {
public CliCategory(String name, String description, String[] options) {
this.name = name;
this.description = description;
this.options = options;
Expand All @@ -31,7 +31,7 @@ public String getName() {
return name;
}

public Category setName(String name) {
public CliCategory setName(String name) {
this.name = name;
return this;
}
Expand All @@ -40,7 +40,7 @@ public String getDescription() {
return description;
}

public Category setDescription(String description) {
public CliCategory setDescription(String description) {
this.description = description;
return this;
}
Expand All @@ -49,7 +49,7 @@ public String[] getOptions() {
return options;
}

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
Expand All @@ -8,62 +8,63 @@
import java.io.File;
import java.io.IOException;

public class Configuration {
public class CliConfiguration {

/**
* The instance of Usage that stores the "usage" information
* The instance of Configuration that this Class is storing
*/
private static Usage usage;
private static CliConfiguration instance;

/**
* LOGGER is an instance of the Logger class so that we can do proper
* logging
* The instance of Usage that stores the "usage" information
*/
private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
private static CliUsage cliUsage;

/**
* The instance of Configuration that this Class is storing
* FILENAME is the file location of the configuration yml file
*/
private static Configuration instance = null;
public static final String CLI_USAGE_FILENAME = "cli-usage.yml";

/**
* FILENAME is the file location of the configuration yml file
* LOGGER is an instance of the Logger class so that we can do proper
* logging
*/
public static final String FILENAME = "usage.yml";
private static final Logger logger = LoggerFactory.getLogger(CliConfiguration.class);

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

return Configuration.instance;
return CliConfiguration.instance;
}

private static Usage loadConfiguration() throws IOException {
private static CliUsage loadConfiguration() throws IOException {
// Loading the YAML file from the /conf folder
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String appHome = System.getProperty("app.home", System.getenv("OPENCGA_HOME"));
String conf = appHome + "/conf";
File file = new File(conf + File.separator + FILENAME);
File file = new File(conf + File.separator + CLI_USAGE_FILENAME);

// Mapping the config from the YAML file to the Configuration class
logger.info("Loading CLI configuration from: " + file.getAbsolutePath());
ObjectMapper om = new ObjectMapper(new YAMLFactory());
return om.readValue(file, Usage.class);
ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
return yamlObjectMapper.readValue(file, CliUsage.class);
}

public static Usage getUsage() {
if (usage == null) {
/*
* We keep an instance of cliUsage for the Shell
*/
public static CliUsage getUsage() {
if (cliUsage == null) {
try {
usage = loadConfiguration();
cliUsage = loadConfiguration();
} catch (IOException e) {
logger.error("Loading CLI configuration from: " + FILENAME + " Failed");

logger.error("Loading CLI configuration from: " + CLI_USAGE_FILENAME + " Failed");
}
}
return usage;
return cliUsage;
}

public static void setUsage(Usage usage) {
Configuration.usage = usage;
public static void setUsage(CliUsage cliUsage) {
CliConfiguration.cliUsage = cliUsage;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.opencb.opencga.app.cli.conf;
package org.opencb.opencga.app.cli.config;

import java.util.Arrays;

public class Usage {
public class CliUsage {

Category[] categories;
CliCategory[] categories;

public Usage() {
public CliUsage() {
}

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

Expand All @@ -21,11 +21,11 @@ public String toString() {
return sb.toString();
}

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

public Usage setCategories(Category[] categories) {
public CliUsage setCategories(CliCategory[] categories) {
this.categories = categories;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
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.conf.Category;
import org.opencb.opencga.app.cli.conf.Configuration;
import org.opencb.opencga.app.cli.conf.Usage;
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;
Expand Down Expand Up @@ -103,16 +103,19 @@ private String getPrefix() {
return "opencga.sh ";
}

/**
* Read cli-usage.yml file to print the usage.
*/
@Override
protected void printMainUsage() {
Usage usage = Configuration.getUsage();
Category[] categories = usage.getCategories();
for (Category category : categories) {
String[] options = category.getOptions();
PrintUtils.println(PrintUtils.format(category.getDescription(), PrintUtils.Color.GREEN));
for (int i = 0; i < options.length; i++) {
CliUsage cliUsage = CliConfiguration.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(options[i])) {
if (command.equals(option)) {
PrintUtils.printCommandHelpFormattedString(command, jCommander.getCommandDescription(command));
}
}
Expand Down
File renamed without changes.

0 comments on commit 080dcf9

Please sign in to comment.