Skip to content

Commit

Permalink
[feature] ability to run oras with --debug, --verbose, --insecure and…
Browse files Browse the repository at this point in the history
… --artifactType flags.
  • Loading branch information
Hashemi Saeed authored and Hashemi Saeed committed Dec 19, 2023
1 parent 8a8d4d3 commit 507b71f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ The following is an example configuration that explicitly sets the directory in
| `<outputDirectory>` | string | oras.outputDirectory | false | artifacts output directory (default: `${project.build.directory}/oras`) |
| `<workingDirectory>` | string | oras.workingDirectory | true | root directory of your artifacts |
| `<artifacts>` | list of strings | oras.artifacts | true | list of artifacts to include. |
| `<excludes>` | list of strings | oras.excludes | false | list of artifacts to exclude |
| `<excludes>` | list of strings | oras.excludes | false | list of artifacts to exclude. |
| `<artifactType>` | string | oras.artifactType | false | artifact type. |
| `<uploadName>` | string | oras.uploadName | false | The name of the app to be upload. |
| `<uploadVersion>` | string | oras.uploadVersion | false | The version of the app to be upload. |
| `<stableRepository>` | [OCIRegistry](src/main/java/com/tosan/plugin/oras/util/OCIRegistry.java) | oci.stable | true | Upload repository for stable artifacts |
| `<snapshotRepository>` | [OCIRegistry](src/main/java/com/tosan/plugin/oras/util/OCIRegistry.java) | oci.snapshot | false | Upload repository for snapshot artifacts (determined by version postfix 'SNAPSHOT') |
| `<registryConfig>` | string | oci.registryConfig | false | path to the registry config file |
| `<repositoryCache>` | string | oci.repositoryCache | false | path to the file containing cached repository indexes |
| `<repositoryConfig>` | string | oci.repositoryConfig | false | path to the file containing repository names and URLs |
| `<stableRepository>` | [OCIRegistry](src/main/java/com/tosan/plugin/oras/util/OCIRegistry.java) | oci.stable | true | Upload registry for stable artifacts |
| `<snapshotRepository>` | [OCIRegistry](src/main/java/com/tosan/plugin/oras/util/OCIRegistry.java) | oci.snapshot | false | Upload registry for snapshot artifacts (determined by version postfix 'SNAPSHOT') |
| `<ociSecurity>` | string | oci.security | false | path to your [settings-security.xml](https://maven.apache.org/guides/mini/guide-encryption.html) (default: `~/.m2/settings-security.xml`) |
| `<insecure>` | boolean | oras.insecure | false | allow connections to SSL registry without certs (default: `false`) |
| `<skip>` | boolean | oras.skip | false | skip plugin execution |
| `<skipPush>` | boolean | oras.upload.skip | false | skip push to goals |
| `<debug>` | boolean | oras.debug | false | debug mode (default: `false`) |
| `<verbose>` | boolean | oras.verbose | false | verbose output (default: `false`) |
71 changes: 45 additions & 26 deletions src/main/java/com/tosan/plugin/oras/AbstractOrasMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@Setter
public abstract class AbstractOrasMojo extends AbstractMojo {
protected static final String LOGIN_TEMPLATE = "registry login -u %s %s --password-stdin";
protected static final String PUSH_TEMPLATE = "push %s/%s:%s %s";
protected static final String PUSH_TEMPLATE = "%s/%s:%s %s";

private final Clock clock = Clock.systemDefaultZone();

Expand All @@ -58,6 +58,9 @@ public abstract class AbstractOrasMojo extends AbstractMojo {
@Parameter(property = "oras.artifacts", required = true)
private String[] artifacts;

@Parameter(property = "oras.artifactType")
private String artifactType;

@Parameter(property = "oras.excludes")
private String[] excludes;

Expand Down Expand Up @@ -85,15 +88,6 @@ public abstract class AbstractOrasMojo extends AbstractMojo {
@Parameter(property = "oci.snapshot")
private OCIRegistry snapshotRepository;

@Parameter(property = "oci.registryConfig")
private String registryConfig;

@Parameter(property = "oci.repositoryCache")
private String repositoryCache;

@Parameter(property = "oci.repositoryConfig")
private String repositoryConfig;

@Parameter(property = "oci.security", defaultValue = "~/.m2/settings-security.xml")
private String ociSecurity;

Expand All @@ -103,6 +97,15 @@ public abstract class AbstractOrasMojo extends AbstractMojo {
@Parameter(property = "oras.skip", defaultValue = "false")
protected boolean skip;

@Parameter(property = "oras.debug", defaultValue = "false")
private boolean debug;

@Parameter(property = "oras.verbose", defaultValue = "false")
private boolean verbose;

@Parameter(property = "oras.insecure", defaultValue = "false")
private boolean insecure;

@Parameter(defaultValue = "${settings}", readonly = true)
private Settings settings;

Expand All @@ -127,6 +130,31 @@ Path getOrasExecutablePath() throws MojoExecutionException {
return path.orElseThrow(() -> new MojoExecutionException("Oras executable is not found."));
}

private String getCommandFlags(String command) {
String flags = "";

//common flags:
if (debug) {
flags += " --debug";
}
if (verbose) {
flags += " --verbose";
}
if (insecure) {
flags += " --insecure";
}

//push flags:
if (command.equals("push")) {
if (StringUtils.isNotEmpty(artifactType)) {
flags += " --artifact-type " + artifactType;
}

}

return flags;
}

/**
* Finds the absolute path to a given {@code executable} in {@code PATH} environment variable.
*
Expand Down Expand Up @@ -158,27 +186,18 @@ void checkArtifacts(String[] artifacts) throws MojoFailureException, FileNotFoun
}
}

void oras(String arguments, String errorMessage) throws MojoExecutionException {
oras(arguments, errorMessage, null);
void oras(String command, String arguments, String errorMessage) throws MojoExecutionException {
oras(command, arguments, errorMessage, null);
}

void oras(String arguments, String errorMessage, String stdin) throws MojoExecutionException {
String command = getOrasExecutablePath() + " " + arguments;
if (StringUtils.isNotEmpty(registryConfig)) {
command += " --registry-config=" + registryConfig;
}
if (StringUtils.isNotEmpty(repositoryConfig)) {
command += " --repository-config=" + repositoryConfig;
}
if (StringUtils.isNotEmpty(repositoryCache)) {
command += " --repository-cache=" + repositoryCache;
}
void oras(String command, String arguments, String errorMessage, String stdin) throws MojoExecutionException {
String fullCommand = getOrasExecutablePath() + " " + command + " " + getCommandFlags(command) + " " + arguments;

// execute oras
getLog().debug(command);
getLog().debug(fullCommand);
int exitValue;
try {
Process p = Runtime.getRuntime().exec(command, null, new File(getWorkingDirectory()));
Process p = Runtime.getRuntime().exec(fullCommand, null, new File(getWorkingDirectory()));
new Thread(() -> {
if (StringUtils.isNotEmpty(stdin)) {
try (OutputStream outputStream = p.getOutputStream()) {
Expand Down Expand Up @@ -212,7 +231,7 @@ void oras(String arguments, String errorMessage, String stdin) throws MojoExecut
p.waitFor();
exitValue = p.exitValue();
} catch (Exception e) {
getLog().error("Error processing command [" + command + "]", e);
getLog().error("Error processing command [" + fullCommand + "]", e);
throw new MojoExecutionException("Error processing command", e);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tosan/plugin/oras/PackagePushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public void execute() throws MojoExecutionException {
private void uploadArchive(OCIRegistry registry, String archiveDirectory, String archiveFile) throws MojoExecutionException {
getLog().debug("Uploading to " + registry.getUrl());
setWorkingDirectory(archiveDirectory);
oras(String.format(PUSH_TEMPLATE, registry.getUrl(), getUploadName(), getUploadVersion(), archiveFile), "Upload failed");
oras("push", String.format(PUSH_TEMPLATE, registry.getUrl(), getUploadName(), getUploadVersion(), archiveFile), "Upload failed");
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/tosan/plugin/oras/PushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void execute() throws MojoExecutionException {

private void upload(OCIRegistry registry, String artifacts) throws MojoExecutionException {
getLog().debug("Uploading to " + registry.getUrl());
oras(String.format(PUSH_TEMPLATE, registry.getUrl(), getUploadName(), getUploadVersion(), artifacts), "Upload failed");
oras("push", String.format(PUSH_TEMPLATE, registry.getUrl(), getUploadName(), getUploadVersion(), artifacts), "Upload failed");
}
}

0 comments on commit 507b71f

Please sign in to comment.