Skip to content

Commit

Permalink
Fixes piranhacloud#3667 - Add coreprofile download command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Mar 7, 2024
1 parent c1830e0 commit cf7f00f
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 19 deletions.
9 changes: 7 additions & 2 deletions cli/cli/src/main/java/cloud/piranha/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
package cloud.piranha.cli;

import static cloud.piranha.cli.Util.stripFirstElement;

/**
* The Piranha CLI.
*
Expand Down Expand Up @@ -58,8 +60,9 @@ public void run() {
usage();
} else {
switch (arguments[0]) {
case "coreprofile" -> new CoreProfileCommand(stripFirstElement(arguments)).run();
case "help" -> usage();
case "version" -> new VersionCommand(arguments).run();
case "version" -> new VersionCommand(stripFirstElement(arguments)).run();
}
}
}
Expand All @@ -78,11 +81,13 @@ public void setArguments(String[] arguments) {
*/
private void usage() {
System.out.println();
System.out.println("usage: piranha [command]");
System.out.println("usage: piranha [command] [subcommand]");
System.out.println();
System.out.println("Available commands:");
System.out.println();
System.out.println(" coreprofile : Piranha Core Profile commands");
System.out.println(" help : Show help");
System.out.println(" version : Show version");
System.out.println();
}
}
89 changes: 89 additions & 0 deletions cli/cli/src/main/java/cloud/piranha/cli/CoreProfileCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.cli;

import static cloud.piranha.cli.Util.stripFirstElement;

/**
* The version command.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class CoreProfileCommand implements Runnable {

/**
* Stores the arguments.
*/
private final String[] arguments;

/**
* Constructor.
*
* @param arguments the arguments.
*/
public CoreProfileCommand(String[] arguments) {
this.arguments = arguments;
}

/**
* Run the command.
*/
@Override
public void run() {
if (arguments.length == 0) {
usage();
} else {
switch (arguments[0]) {
case "download" -> new CoreProfileDownloadCommand(stripFirstElement(arguments)).run();
case "help" -> usage();
}
}
}


/**
* Show the help.
*/
private void usage() {
System.out.println();
System.out.println("usage: piranha coreprofile [command] <options>");
System.out.println();
System.out.println("Available commands:");
System.out.println();
System.out.println(" download : Download the Piranha Core Profile distribution");
System.out.println(" help : Show help");
System.out.println();
System.out.println();
System.out.println("Command: download");
System.out.println("-----------------");
System.out.println();
System.out.println("Available options:");
System.out.println();
System.out.println(" --outputDirectory <directory> : specify the output directory");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.cli;

import static cloud.piranha.cli.Util.version;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
* The coreprofile download command.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class CoreProfileDownloadCommand implements Runnable {

/**
* Stores the Maven central prefix.
*/
private static final String MAVEN_CENTRAL_PREFIX
= "https://repo1.maven.org/maven2/cloud/piranha/dist/";

/**
* Stores the Sonatype SNAPSHOTs prefix.
*/
private static final String SONATYPE_SNAPSHOTS_PREFIX
= "https://oss.sonatype.org/content/repositories/snapshots/cloud/piranha/dist/";

/**
* Stores the arguments.
*/
private final String[] arguments;

/**
* Stores the filename we are going to download to.
*/
private String filename = "piranha-dist-coreprofile-" + version() + ".jar";

/**
* Stores the output directory.
*/
private File outputDirectory = new File(System.getProperty("user.home"),
".piranha/coreprofile/download");

/**
* Stores the URL we are going to download.
*/
private URL url;

/**
* Stores the version we are downloading.
*/
private String version = "24.2.0";

/**
* Constructor.
*
* @param arguments the arguments.
*/
public CoreProfileDownloadCommand(String[] arguments) {
this.arguments = arguments;
}

/**
* Download a release version.
*/
private void downloadRelease() {
String urlString = MAVEN_CENTRAL_PREFIX + "piranha-dist-coreprofile/"
+ version + "/piranha-dist-coreprofile-" + version + ".jar";

try {
url = new URL(urlString);
} catch (MalformedURLException mue) {
mue.printStackTrace(System.out);
System.exit(0);
}

downloadUrl();
}

/**
* Download a SNAPSHOT version.
*/
private void downloadSnapshot() {
String urlString = SONATYPE_SNAPSHOTS_PREFIX + "piranha-dist-coreprofile/"
+ version + "/piranha-dist-coreprofile-" + version + ".jar";

try {
url = new URL(urlString);
} catch (MalformedURLException mue) {
mue.printStackTrace(System.out);
System.exit(0);
}

downloadUrl();
}

/**
* Download the URL.
*/
private void downloadUrl() {
System.out.println("Downloading Piranha Core Profile - " + version);
System.out.println("From - " + url);
System.out.println("To - " + outputDirectory);

if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}

try (InputStream in = url.openStream()) {
Files.copy(in, Paths.get(new File(outputDirectory, filename).toURI()),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
System.exit(0);
}
}

@Override
public void run() {
if (version.endsWith("-SNAPSHOT")) {
downloadSnapshot();
} else {
downloadRelease();
}
}
}
77 changes: 77 additions & 0 deletions cli/cli/src/main/java/cloud/piranha/cli/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.cli;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Utility class.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class Util {

/**
* Private constructor.
*/
private Util() {
}

/**
* Strip first element and return a new array.
*
* @param array the array.
* @return the copied array minus the first element.
*/
public static String[] stripFirstElement(String[] array) {
String[] newArray = new String[array.length - 1];
for (int i = 1; i < array.length; i++) {
newArray[i - 1] = array[i];
}
return newArray;
}

/**
* Get the Piranha version.
*
* @return the Piranha version.
*/
public static String version() {
String version;
try {
version = Files.readAllLines(
Path.of(Util.class.getResource("/VERSION").toURI())).get(0);
} catch (IOException | URISyntaxException e) {
version = e.getMessage();
}
return version;
}
}

0 comments on commit cf7f00f

Please sign in to comment.