Skip to content

Commit

Permalink
devonfw#103: refactored code
Browse files Browse the repository at this point in the history
- renamed methods in SystemPath
- split long method securityRiskInteraction into componets
  • Loading branch information
MattesMrzik committed Jan 21, 2024
1 parent 06cc433 commit 80ab231
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ public Path findBinary(Path toolPath) {
* @return the {@link Path} to the directory of the tool where the binaries can be found or {@code null} if the tool
* is not installed.
*/
public Path getPath(String tool) {
public Path retrievePath(String tool) {

return this.tool2pathMap.get(tool);
}

/**
* @param tool the name of the tool.
* @param path the new {@link #getPath(String) tool bin path}.
* @param path the new {@link #retrievePath(String) tool bin path}.
*/
public void setPath(String tool, Path path) {
public void addPath(String tool, Path path) {

this.paths.add(path);
this.tool2pathMap.put(tool, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected boolean doInstall(boolean silent) {
fileAccess.backup(toolPath);
}
fileAccess.symlink(installation.linkDir(), toolPath);
this.context.getPath().setPath(this.tool, installation.binDir());
this.context.getPath().addPath(this.tool, installation.binDir());
if (installedVersion == null) {
this.context.success("Successfully installed {} in version {}", this.tool, resolvedVersion);
} else {
Expand Down
87 changes: 67 additions & 20 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,38 +196,44 @@ protected VersionIdentifier securityRiskInteraction(VersionIdentifier configured
List<VersionIdentifier> allVersions = this.context.getUrls().getSortedVersions(this.tool, this.getEdition());
VersionIdentifier latest = allVersions.get(0);

int currentVersionIndex = allVersions.indexOf(current);
VersionIdentifier nextSafe = null;
for (int i = currentVersionIndex - 1; i >= 0; i--) {
if (!securityFile.contains(allVersions.get(i), true, this.context, securityFile.getParent())) {
nextSafe = allVersions.get(i);
break;
}
}
VersionIdentifier latestSafe = null;
for (int i = 0; i < allVersions.size(); i++) {
if (!securityFile.contains(allVersions.get(i), true, this.context, securityFile.getParent())) {
latestSafe = allVersions.get(i);
break;
}
}
VersionIdentifier nextSafe = getNextSafeVersion(current, securityFile, allVersions);
VersionIdentifier latestSafe = getLatestSafe(allVersions, securityFile);

String cves = securityFile.getMatchingSecurityWarnings(current).stream().map(UrlSecurityWarning::getCveName)
.collect(Collectors.joining(", "));
String currentIsUnsafe = "Currently, version " + current + " of " + this.getName() + " is selected, "
+ "which is has one or more vulnerabilities:\n\n" + cves + "\n\n(See also " + securityFile.getPath() + ")\n\n";

if (latestSafe == null) {
this.context.warning(currentIsUnsafe + "There is no safe version available.");
return configuredVersion;
}

return whichVersionDoYouWantToInstall(configuredVersion, current, nextSafe, latestSafe, latest, currentIsUnsafe);
}

/**
* Using all the safety information about the versions, this method asks the user which version to install.
*
* @param configuredVersion the version that was configured in the environment variables.
* @param current the current version that was resolved from the configured version.
* @param nextSafe the next safe version.
* @param latestSafe the latest safe version.
* @param latest the latest version.
* @param currentIsUnsafe the message that is printed if the current version is unsafe.
* @return the version that the user wants to install.
*/
private VersionIdentifier whichVersionDoYouWantToInstall(VersionIdentifier configuredVersion,
VersionIdentifier current, VersionIdentifier nextSafe, VersionIdentifier latestSafe, VersionIdentifier latest,
String currentIsUnsafe) {

String ask = "Which version do you want to install?";

String stay = "Stay with the current unsafe version (" + current + ").";
String installLatestSafe = "Install the latest safe version (" + latestSafe + ").";
String installSafeLatest = "Install the (safe) latest version (" + latest + ").";
String installNextSafe = "Install the next safe version (" + nextSafe + ").";

if (latestSafe == null) {
this.context.warning(currentIsUnsafe + "There is no safe version available.");
return configuredVersion;
}

if (current.equals(latest)) {
String answer = this.context.question(currentIsUnsafe + "There are no updates available. " + ask, stay,
installLatestSafe);
Expand Down Expand Up @@ -263,6 +269,47 @@ protected VersionIdentifier securityRiskInteraction(VersionIdentifier configured
}
}

/**
* Gets the latest safe version from the list of all versions.
*
* @param allVersions all versions of the tool.
* @param securityFile the {@link UrlSecurityJsonFile} to check if a version is safe or not.
* @return the latest safe version or {@code null} if there is no safe version.
*/
private VersionIdentifier getLatestSafe(List<VersionIdentifier> allVersions, UrlSecurityJsonFile securityFile) {

VersionIdentifier latestSafe = null;
for (int i = 0; i < allVersions.size(); i++) {
if (!securityFile.contains(allVersions.get(i), true, this.context, securityFile.getParent())) {
latestSafe = allVersions.get(i);
break;
}
}
return latestSafe;
}

/**
* Gets the next safe version from the list of all versions starting from the current version.
*
* @param current the current version.
* @param securityFile the {@link UrlSecurityJsonFile} to check if a version is safe or not.
* @param allVersions all versions of the tool.
* @return the next safe version or {@code null} if there is no safe version.
*/
private VersionIdentifier getNextSafeVersion(VersionIdentifier current, UrlSecurityJsonFile securityFile,
List<VersionIdentifier> allVersions) {

int currentVersionIndex = allVersions.indexOf(current);
VersionIdentifier nextSafe = null;
for (int i = currentVersionIndex - 1; i >= 0; i--) {
if (!securityFile.contains(allVersions.get(i), true, this.context, securityFile.getParent())) {
nextSafe = allVersions.get(i);
break;
}
}
return nextSafe;
}

/**
* Installs or updates the managed {@link #getName() tool}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void testBasicProjectEnvironment() {
assertThat(systemPath.toString()).isNotEqualTo(envPath).endsWith(envPath);
Path softwarePath = context.getSoftwarePath();
Path javaBin = softwarePath.resolve("java/bin");
assertThat(systemPath.getPath("java")).isEqualTo(javaBin);
assertThat(systemPath.retrievePath("java")).isEqualTo(javaBin);
Path mvnBin = softwarePath.resolve("mvn/bin");
assertThat(systemPath.getPath("mvn")).isEqualTo(mvnBin);
assertThat(systemPath.retrievePath("mvn")).isEqualTo(mvnBin);
assertThat(systemPath.toString()).contains(javaBin.toString(), mvnBin.toString());
assertThat(variables.getType()).isSameAs(EnvironmentVariablesType.RESOLVED);
assertThat(variables.getByType(EnvironmentVariablesType.RESOLVED)).isSameAs(variables);
Expand Down

0 comments on commit 80ab231

Please sign in to comment.