Skip to content

Commit

Permalink
chore: Improve tool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Feb 1, 2024
1 parent c059287 commit d8854be
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
public class ToolInstallationDescriptor {
private String installDir;
private String binDir;
private String globalBinDir;

public ToolInstallationDescriptor(Path installPath, Path binPath) {
this.installDir = installPath.toAbsolutePath().toString();
this.binDir = binPath.toAbsolutePath().toString();
public ToolInstallationDescriptor(Path installPath, Path binPath, Path globalBinPath) {
this.installDir = installPath==null ? null : installPath.toAbsolutePath().normalize().toString();
this.binDir = binPath==null ? null : binPath.toAbsolutePath().normalize().toString();
this.globalBinDir = globalBinPath==null ? null : globalBinPath.toAbsolutePath().normalize().toString();
}

public static final ToolInstallationDescriptor load(String toolName, ToolDefinitionVersionDescriptor versionDescriptor) {
Expand Down Expand Up @@ -70,6 +72,10 @@ public Path getBinPath() {
return asPath(binDir);
}

public Path getGlobalBinPath() {
return asPath(binDir);
}

private static final Path asPath(String dir) {
return StringUtils.isNotBlank(dir) ? Paths.get(dir) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ToolInstallationOutputDescriptor {
private Map<String, ToolDefinitionArtifactDescriptor> binaries;
private final String installDir;
private final String binDir;
private final String globalBinDir;
private final String installed;
private final String __action__;

Expand All @@ -53,6 +54,7 @@ public ToolInstallationOutputDescriptor(String toolName, ToolDefinitionVersionDe
this.binaries = versionDescriptor.getBinaries();
this.installDir = installationDescriptor==null ? null : installationDescriptor.getInstallDir();
this.binDir = installationDescriptor==null ? null : installationDescriptor.getBinDir();
this.globalBinDir = installationDescriptor==null ? null : installationDescriptor.getGlobalBinDir();
this.installed = StringUtils.isBlank(this.installDir) ? "No" : "Yes";
this.__action__ = action;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ private final ToolInstallationResult install(ToolDefinitionArtifactDescriptor ar
progressWriter.writeProgress("Running post-install actions");
postInstallAction.accept(this, result);
updateBinPermissions(result.getInstallationDescriptor().getBinPath());
writeInstallationInfo(result);
return result;
} catch ( IOException e ) {
throw new RuntimeException("Error installing "+toolName, e);
Expand Down Expand Up @@ -244,7 +245,8 @@ private final void copyOrExtract(ToolDefinitionArtifactDescriptor artifactDescri
private final ToolInstallationDescriptor createAndSaveInstallationDescriptor() {
var installPath = getTargetPath();
var binPath = getBinPath();
var installationDescriptor = new ToolInstallationDescriptor(installPath, binPath);
var globalBinPath = getGlobalBinPath();
var installationDescriptor = new ToolInstallationDescriptor(installPath, binPath, globalBinPath);
installationDescriptor.save(toolName, getVersionDescriptor());
return installationDescriptor;
}
Expand All @@ -269,6 +271,13 @@ private final void warnIfDifferentTargetPath() {
}
}

private final void writeInstallationInfo(ToolInstallationResult installationResult) {
var globalBinDir = installationResult.getInstallationDescriptor().getGlobalBinDir();
var binDir = installationResult.getInstallationDescriptor().getBinDir();
progressWriter.writeWarning("INFO: Add the following directory to PATH for easy tool invocation:\n %s\n",
globalBinDir==null ? binDir : globalBinDir);
}

private final void checkEmptyTargetPath() throws IOException {
var targetPath = getTargetPath();
if ( Files.exists(targetPath) && Files.list(targetPath).findFirst().isPresent() ) {
Expand All @@ -277,8 +286,10 @@ private final void checkEmptyTargetPath() throws IOException {
}

private static final void updateBinPermissions(Path binPath) throws IOException {
try (Stream<Path> walk = Files.walk(binPath)) {
walk.forEach(ToolInstaller::updateFilePermissions);
if ( binPath!=null ) {
try (Stream<Path> walk = Files.walk(binPath)) {
walk.forEach(ToolInstaller::updateFilePermissions);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
*******************************************************************************/
package com.fortify.cli.tool.fcli.cli.cmd;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.util.FileUtils;
Expand All @@ -39,13 +41,31 @@ protected String getDefaultArtifactType() {

@Override @SneakyThrows
protected void postInstall(ToolInstaller installer, ToolInstallationResult installationResult) {
installBinariesAndScripts(installer);
installFcliCompletion(installer);
}

private void installFcliCompletion(ToolInstaller installer) throws IOException {
var globalBinPath = installer.getGlobalBinPath();
var originalFcliCompletionScript = installer.getBinPath().resolve("fcli_completion");
if ( Files.exists(originalFcliCompletionScript) ) {
var pw = installer.getProgressWriter();
Path targetFcliCompletionScript = originalFcliCompletionScript;
if ( globalBinPath!=null ) {
targetFcliCompletionScript = globalBinPath.resolve(originalFcliCompletionScript.getFileName());
Files.copy(originalFcliCompletionScript, targetFcliCompletionScript, StandardCopyOption.REPLACE_EXISTING);
}
pw.writeWarning("INFO: Run the following command to update fcli auto-completion:\n source %s", targetFcliCompletionScript.toAbsolutePath().normalize());
}
}

private void installBinariesAndScripts(ToolInstaller installer) {
Path installPath = installer.getTargetPath();
FileUtils.moveFiles(installPath, installer.getBinPath(), "fcli(_completion)?(\\.exe)?");
if ( Files.exists(installPath.resolve("fcli.jar")) ) {
installer.installJavaBinScripts("fcli", "fcli.jar");
} else {
installer.installGlobalBinScript(BinScriptType.bash, "fcli", "bin/fcli");
installer.installGlobalBinScript(BinScriptType.bash, "fcli_completion", "bin/fcli_completion");
installer.installGlobalBinScript(BinScriptType.bat, "fcli.bat", "bin/fcli.exe");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public class Fcli {
}
return this
}
final FcliResult expectZeroExitCode() {
if ( nonZeroExitCode ) {
throw new UnexpectedFcliResultException("Fcli unexpectedly terminated unsuccessfully\n "
+stderr.join("\n "), this)
}
return this
}
}

public static class UnexpectedFcliResultException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ToolBugTrackerUtilitySpec extends FcliBaseSpec {
def "install"() {
def args = "tool bugtracker-utility install -y -v=${version} --progress=none -b ${baseDir}"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ToolDebrickedSpec extends FcliBaseSpec {
def "install"() {
def args = "tool debricked-cli install -y -v=${version} --progress=none -b ${baseDir} --platform windows/x64"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ToolFcliSpec extends FcliBaseSpec {
def "install"() {
def args = "tool fcli install -y -v=${version} --progress=none -b ${baseDir} --platform windows/x64"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ToolFoDUploaderSpec extends FcliBaseSpec {
def "installLatest"() {
def args = "tool fod-uploader install -y -v=${version} --progress=none -b ${baseDir}"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down Expand Up @@ -74,7 +74,7 @@ class ToolFoDUploaderSpec extends FcliBaseSpec {
def "installV5"() {
def args = "tool fod-uploader install -y -v=5 --progress=none"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand All @@ -87,7 +87,7 @@ class ToolFoDUploaderSpec extends FcliBaseSpec {
def "installV50"() {
def args = "tool fod-uploader install -y -v=5.0 --progress=none"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand All @@ -100,7 +100,7 @@ class ToolFoDUploaderSpec extends FcliBaseSpec {
def "installV500"() {
def args = "tool fod-uploader install -y -v=5.0.0 --progress=none"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ToolScClientSpec extends FcliBaseSpec {
def "install"() {
def args = "tool sc-client install -y -v=${version} --progress=none -b ${baseDir}"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ToolVulnExporterSpec extends FcliBaseSpec {
def "install"() {
def args = "tool vuln-exporter install -y -v=${version} --progress=none -b ${baseDir}"
when:
def result = Fcli.run(args)
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
Expand Down

0 comments on commit d8854be

Please sign in to comment.