Skip to content

Commit

Permalink
DCA11Y-1145: fix: fall back on reading $SHELL from bin/sh
Browse files Browse the repository at this point in the history
  • Loading branch information
flipatlas committed Oct 11, 2024
1 parent 91bf8b1 commit a10b762
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void install() throws InstallationException {
versionManagerRunner.installNodeAndUpdateCaches();
return;
} else {
this.logger.warn("Node version manager is no available, falling back to standard node installation.");
this.logger.warn("Node version manager is not available, falling back to standard node installation.");
}

// try to install the standard way
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getCodename(){
}
}

class Platform {
public class Platform {

/**
* Node.js supports Apple silicon since v16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public String executeAndCatchErrors(List<String> command) {
try {
int exitValue = execute(profiledShellCommand, stdout, stderr);
if (exitValue != 0) {
logger.debug("Command finished with error exit code {}, error output `{}`", exitValue, parseOutput(stderr));
logger.debug("Command finished with an error exit code {}", exitValue);
}
} catch (ProcessExecutionException e) {
logger.debug("Command threw unexpectedly, error output: `{}`", parseOutput(stderr));
logger.debug("Command threw unexpectedly");
}

String output = parseOutput(stdout);
logger.debug("Command output: `{}`", output);
logger.debug("Command output: `{}`\n error output `{}`", output, parseOutput(stderr));
return output;
}

Expand Down Expand Up @@ -80,10 +80,16 @@ private int execute(List<String> command, ByteArrayOutputStream stdout, ByteArra

private List<String> getShellCommand(List<String> command) {
List<String> profiledShellCommand = new ArrayList<>();
profiledShellCommand.add(getCurrentShell());
profiledShellCommand.add("--login");
profiledShellCommand.add("-c");
profiledShellCommand.add(String.join(" ", command));

if (config.getPlatform().isWindows()) {
logger.warn("Windows is currently not supported");
profiledShellCommand.add(String.join(" ", command));
} else {
profiledShellCommand.add(getCurrentUnixShell());
profiledShellCommand.add("--login");
profiledShellCommand.add("-c");
profiledShellCommand.add(String.join(" ", command));
}

return profiledShellCommand;
}
Expand All @@ -92,21 +98,21 @@ private String parseOutput(ByteArrayOutputStream stream) {
return stream.toString().trim();
}

private String getCurrentShell() {
private String getCurrentUnixShell() {
String shell = System.getenv("SHELL");
if (shell == null || shell.isEmpty()) {
logger.debug("SHELL is not available in environment variables. Trying to get it from child process.");

logger.debug("SHELL variable couldn't be found. Falling back on reading the variable from /bin/sh.");
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
try {
execute(Arrays.asList("echo", "$SHELL"), stdout, stdout);
execute(Arrays.asList("/bin/sh", "-c", "echo $SHELL"), stdout, stdout);
shell = parseOutput(stdout);
logger.debug("SHELL from /bin/sh: {}", shell);
} catch (ProcessExecutionException e) {
throw new RuntimeException(e);
}

if (shell.isEmpty()) {
throw new RuntimeException("SHELL was not available in child process. Falling back to bin/sh");
throw new RuntimeException("SHELL is not available in environment variables. Please provide the value of $SHELL with your preferred shell.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public VersionManagerRunner(InstallConfig installConfig, VersionManagerCache ver
public void installNodeAndUpdateCaches() {
if (!installConfig.isUseNodeVersionManager()) return;

logger.info("Installing node with {}", versionManagerCache.getVersionManagerType());
logger.info("Installing node with {}. (Relying on node version manager caching)", versionManagerCache.getVersionManagerType());

versionManagerClient.installNode();
populateCache();
Expand Down

0 comments on commit a10b762

Please sign in to comment.