-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
private ThreescaleCmsClientFactory factory; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
|
@@ -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"); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.