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

Implement context usage #30

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

<properties>
<project.build.outputTimestamp>1668725936</project.build.outputTimestamp>
<java.version>17</java.version>
<executable-suffix/>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.release>${java.version}</maven.compiler.release>
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to set these properties specific to the cli submodule, or should we apply in /parent/pom.xml so they apply across all the modules?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it to the parent pom.xml file. However, somehow I could not make my IDE recognize the java version from the parent, so I added it to the submodule.

</properties>

<dependencies>
Expand Down Expand Up @@ -68,6 +72,11 @@
<groupId>org.jboss.logging</groupId>
<artifactId>commons-logging-jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>com.github.freva</groupId>
<artifactId>ascii-table</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>

<build>
Expand Down
12 changes: 12 additions & 0 deletions cli/src/main/java/com/fwmotion/threescale/cms/cli/CommandBase.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.fwmotion.threescale.cms.cli;

import com.fwmotion.threescale.cms.cli.support.ExecutionExceptionHandler;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.fwmotion.threescale.cms.cli.version.VersionProperties;
import com.fwmotion.threescale.cms.cli.version.VersionProvider;

import io.quarkus.picocli.runtime.PicocliCommandLineFactory;
import picocli.CommandLine;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;

@CommandLine.Command(
Expand All @@ -26,4 +31,11 @@ public class CommandBase {
@Inject
VersionProperties versionProperties;

@Inject
ConfigurationContext context;

@Produces
CommandLine getCommandLineInstance(PicocliCommandLineFactory factory) {
return factory.create().setExecutionExceptionHandler(new ExecutionExceptionHandler(context));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fwmotion.threescale.cms.cli;

import picocli.CommandLine.Command;

@Command(name = "config", subcommands = { SetContextCommand.class, GetContextsCommand.class,
CurrentContextCommand.class, UseContextCommand.class }, description = "Sets or retrieves the configuration of the tenant"

)
public class ConfigCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fwmotion.threescale.cms.cli;

import javax.inject.Inject;

import com.fwmotion.threescale.cms.cli.support.Context;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.github.freva.asciitable.AsciiTable;
import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;

import picocli.CommandLine.Command;

@Command(name = "current-context", description = "Displays the current context")
public class CurrentContextCommand implements Runnable {

@Inject
ConfigurationContext context;

@Override
public void run() {
Context currentContext = context.getCurrentContext();
String[][] data = new String[2][];
data[0] = new String[]{ "Provider Domain", currentContext.getProviderDomain().toASCIIString() };
data[1] = new String[]{ "Access Token", currentContext.getAccessToken() };

String table = AsciiTable.getTable(AsciiTable.NO_BORDERS,
new Column[]{
new Column().header("KEY").dataAlign(HorizontalAlign.LEFT),
new Column().header("VALUE").dataAlign(HorizontalAlign.LEFT)
},
data);

System.out.println("Using context '" + context.getCurrentContextName() + "'");
System.out.println();
System.out.println(table);

}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fwmotion.threescale.cms.cli.support.CmsObjectPathKeyGenerator;
import com.fwmotion.threescale.cms.cli.support.LocalRemoteObjectTreeComparator;
import com.fwmotion.threescale.cms.cli.support.LocalRemoteTreeComparisonDetails;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.fwmotion.threescale.cms.model.CmsLayout;
import io.quarkus.logging.Log;
import picocli.CommandLine;
Expand All @@ -17,6 +18,9 @@
)
public class DiffCommand extends CommandBase implements Callable<Integer> {

@Inject
ConfigurationContext configContext;

@Inject
LocalRemoteObjectTreeComparator treeComparator;

Expand All @@ -43,7 +47,7 @@ public Integer diffDetails() throws Exception {
}

private void showDiff(boolean includeDetails) throws Exception {
InfoCommand.displayCmsUrl(topLevelCommand.getProviderDomain());
InfoCommand.displayCmsUrl(configContext.getCurrentContext().getProviderDomain().toASCIIString());

LocalRemoteTreeComparisonDetails details = treeComparator.compareLocalAndRemoteCmsObjectTrees(
topLevelCommand.getCmsObjects().stream(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fwmotion.threescale.cms.cli;

import java.util.Map;

import javax.inject.Inject;

import com.fwmotion.threescale.cms.cli.support.Context;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.github.freva.asciitable.AsciiTable;
import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;

import picocli.CommandLine.Command;

@Command(name = "get-contexts", description = "Get all contexts")
public class GetContextsCommand implements Runnable {

@Inject
ConfigurationContext configContext;

@Override
public void run() {
Map<String, Context> contexts = configContext.getContexts();
String current = configContext.getCurrentContextName();

String[][] data = contexts.entrySet()
.stream()
.map(e -> new String[]{ e.getKey() + (e.getKey().equals(current) ? "*" : ""), e.getValue().getProviderDomain().toASCIIString() })
.toArray(size -> new String[size][]);

String table = AsciiTable.getTable(AsciiTable.NO_BORDERS,
new Column[]{
new Column().header("NAME").dataAlign(HorizontalAlign.LEFT),
new Column().header("PROVIDER DOMAIN URI").dataAlign(HorizontalAlign.LEFT)
},
data);

System.out.println(table);

}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fwmotion.threescale.cms.cli.support.CmsObjectPathKeyGenerator;
import com.fwmotion.threescale.cms.cli.support.LocalRemoteObjectTreeComparator;
import com.fwmotion.threescale.cms.cli.support.LocalRemoteTreeComparisonDetails;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.fwmotion.threescale.cms.model.CmsLayout;
import com.fwmotion.threescale.cms.model.CmsObject;
import io.quarkus.logging.Log;
Expand All @@ -26,6 +27,9 @@
)
public class InfoCommand extends CommandBase implements Callable<Integer> {

@Inject
ConfigurationContext configContext;

@Inject
LocalRemoteObjectTreeComparator treeComparator;

Expand All @@ -52,7 +56,7 @@ public Integer infoDetails() throws Exception {
}

private void showInfo(boolean includeDetails) throws Exception {
displayCmsUrl(topLevelCommand.getProviderDomain());
displayCmsUrl(configContext.getCurrentContext().getProviderDomain().toASCIIString());

List<CmsObject> allRemoteObjects = topLevelCommand.getCmsObjects();
LocalRemoteTreeComparisonDetails details = treeComparator.compareLocalAndRemoteCmsObjectTrees(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.fwmotion.threescale.cms.cli;

import java.net.URI;
import java.util.concurrent.Callable;

import com.fwmotion.threescale.cms.cli.support.Context;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = "set-context", description = "Configures the specified context with the provided arguments")
public class SetContextCommand implements Callable<Integer> {

@Parameters(index = "0", description = "Context name")
String contextName;

@Option(names = { "-p" , "--provider-domain"}, required = true, description = "Base URL of the 3scale admin portal to connect to; for example: https://3scale-admin.apps.example.com/")
String domain;

@Option(names = { "-t", "--access-token" }, required = true, description = "Access token for full control of the target tenant. The access token must be granted permissions to both Account Management API and the Content Management API")
String accessToken;

@Override
public Integer call() throws Exception {
ConfigurationContext context = new ConfigurationContext();

context.setContext(contextName, new Context(URI.create(domain), accessToken));
System.out.println("Configured context " + contextName);

if (!context.getCurrentContextName().equals(contextName)) {
System.out.println("Run 3scale-cms config use-context " + contextName + " for using this context");
}

return 0;
}

}
106 changes: 37 additions & 69 deletions cli/src/main/java/com/fwmotion/threescale/cms/cli/TopLevelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fwmotion.threescale.cms.ThreescaleCmsClient;
import com.fwmotion.threescale.cms.ThreescaleCmsClientFactory;
import com.fwmotion.threescale.cms.cli.util.ConfigurationContext;
import com.fwmotion.threescale.cms.model.CmsObject;
import io.quarkus.picocli.runtime.annotations.TopCommand;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -12,70 +13,45 @@
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

@TopCommand
@CommandLine.Command(
header = {"3scale Content Management System CLI Tool"},
name = "3scale-cms",
description = "Tool for interacting with 3scale's Content Management " +
"System for managing content and templates that will be used to " +
"render a tenant's Developer Portal",
subcommands = {
InfoCommand.class,
DiffCommand.class,
DownloadCommand.class,
UploadCommand.class,
DeleteCommand.class,
},
synopsisSubcommandLabel = "[COMMAND] ",
commandListHeading = "%n@|green,bold COMMANDS|@%n"
)
@CommandLine.Command(header = {
"3scale Content Management System CLI Tool" }, name = "3scale-cms", description = "Tool for interacting with 3scale's Content Management "
+
"System for managing content and templates that will be used to " +
"render a tenant's Developer Portal", subcommands = {
ConfigCommand.class,
InfoCommand.class,
DiffCommand.class,
DownloadCommand.class,
UploadCommand.class,
DeleteCommand.class,
}, synopsisSubcommandLabel = "[COMMAND] ", commandListHeading = "%n@|green,bold COMMANDS|@%n")
Comment on lines -16 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to see what, if anything, changed here... And the annotation properties are now combined at interesting places. Seems like a bad autoformat run?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you let me know if you're using a specific formatter? We could add a maven plugin to validate this before submitting.

public class TopLevelCommand extends CommandBase {

@CommandLine.Option(
names = {"-k", "--insecure"},
description = "Proceed with server connections that fail TLS " +
"certificate validation"
)
private final ConfigurationContext context;

@Inject
public TopLevelCommand(ConfigurationContext context) {
this.context = context;
}

// Hack : Picocli currently require an empty constructor to generate the
// completion file
public TopLevelCommand() {
context = new ConfigurationContext();
}

@CommandLine.Option(names = { "-k", "--insecure" }, description = "Proceed with server connections that fail TLS " +
"certificate validation")
private boolean useInsecureConnections;

@CommandLine.Parameters(
index = "0",
paramLabel = "PROVIDER_KEY",
arity = "1",
description = "Provider Key for full control of the target tenant. " +
"This will be ignored if --access-token is specified."
)
private String providerKey;

@CommandLine.Parameters(
index = "1",
paramLabel = "PROVIDER_DOMAIN",
arity = "1",
description = {
"The base URL of the admin portal; for example:",
"https://3scale-admin.apps.example.com/"
}
)
private String providerDomain;

@CommandLine.Option(
names = {"-a", "--access-token"},
paramLabel = "ACCESS_TOKEN",
arity = "1",
description = "Use an access token instead of a provider key. The " +
"access token must be granted permissions to both " +
"Account Management API and the hidden Content Management API"
)
private String accessToken;

@CommandLine.Option(
names = {"-d", "--directory"},
paramLabel = "DIRECTORY",
arity = "1",
description = "Specify local directory path for determining files " +
"to upload, download, or compare between local filesystem and " +
"3scale CMS content"
)
@CommandLine.Option(names = { "-d",
"--directory" }, paramLabel = "DIRECTORY", arity = "1", description = "Specify local directory path for determining files "
+
"to upload, download, or compare between local filesystem and " +
"3scale CMS content")
private File rootDirectory;
Comment on lines +50 to 55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting of this annotation is strange. For annotations with many properties, let's start each new property on a new line for readability. Also, it seems that your IDE is not respecting the .editorconfig directive of indenting by 4 spaces.


private ThreescaleCmsClientFactory factory;
Expand All @@ -84,12 +60,8 @@ public class TopLevelCommand extends CommandBase {
public ThreescaleCmsClient getClient() {
if (factory == null) {
factory = new ThreescaleCmsClientFactory();
factory.setBaseUrl(providerDomain);
if (StringUtils.isNotBlank(accessToken)) {
factory.setAccessToken(accessToken);
} else {
factory.setProviderKey(providerKey);
}
factory.setBaseUrl(context.getCurrentContext().getProviderDomain().toASCIIString());
factory.setAccessToken(context.getCurrentContext().getAccessToken());
factory.setUseInsecureConnections(useInsecureConnections);
Comment on lines -87 to 65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this removes the option to use provider key. Is that intentional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your experience with the provider key? Is that something that is still commonly used? Since a couple of versions, the standard is to use access tokens. For the sake of maintenance makes more sense to deprecate/remove that option.

}

Expand All @@ -104,10 +76,6 @@ public List<CmsObject> getCmsObjects() {
return new ArrayList<>(cmsObjects);
}

public String getProviderDomain() {
return providerDomain;
}

public File getRootDirectory() throws IOException {
if (rootDirectory == null) {
String threescaleCmsRoot = System.getenv("THREESCALE_CMS_ROOT");
Expand Down
Loading