Skip to content

Commit

Permalink
Fix error when tofu with version used (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui authored Oct 15, 2024
1 parent 7f7c636 commit 3024835
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
@Service
public class OpenTofuDirectoryService {

private static final String STATE_FILE_NAME = "openTofu.tfstate";
private static final String STATE_FILE_NAME = "terraform.tfstate";
private static final String TEST_FILE_NAME = "hello-world.tf";
private static final String HEALTH_CHECK_DIR = UUID.randomUUID().toString();
private static final List<String> EXCLUDED_FILE_SUFFIX_LIST =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public boolean checkIfExecutorIsMatchedRequiredVersion(File executorFile,
* @return If true, the executor can be executed, otherwise return false.
*/
public boolean checkIfExecutorCanBeExecuted(File executorFile) {
String versionOutput = getVersionCommandOutput(executorFile);
String versionOutput = getVersionCommandOutput(executorFile.getAbsolutePath());
return StringUtils.isNotBlank(versionOutput);
}

Expand All @@ -191,7 +191,7 @@ public boolean checkIfExecutorCanBeExecuted(File executorFile) {
* @return exact version of executor.
*/
public String getExactVersionOfExecutor(String executorPath) {
String versionOutput = getVersionCommandOutput(new File(executorPath));
String versionOutput = getVersionCommandOutput(executorPath);
Matcher matcher = OPENTOFU_VERSION_OUTPUT_PATTERN.matcher(versionOutput);
if (matcher.find()) {
// return only the version number.
Expand All @@ -201,12 +201,10 @@ public String getExactVersionOfExecutor(String executorPath) {
}


private String getVersionCommandOutput(File executorFile) {
private String getVersionCommandOutput(String executorPath) {
try {
if (!executorFile.exists() && !executorFile.isFile()) {
return null;
}
if (!executorFile.canExecute()) {
File executorFile = new File(executorPath);
if (executorFile.exists() && !executorFile.canExecute()) {
SystemCmdResult chmodResult = systemCmd.execute(
String.format("chmod +x %s", executorFile.getAbsolutePath()),
5, System.getProperty("java.io.tmpdir"), false, new HashMap<>());
Expand All @@ -215,18 +213,20 @@ private String getVersionCommandOutput(File executorFile) {
}
}
SystemCmdResult versionCheckResult =
systemCmd.execute(executorFile.getAbsolutePath() + " version",
systemCmd.execute(executorPath + " version",
5, System.getProperty("java.io.tmpdir"), false, new HashMap<>());
if (versionCheckResult.isCommandSuccessful()) {
log.info("Get version of executor {} output: {}", executorPath,
versionCheckResult.getCommandStdOutput());
return versionCheckResult.getCommandStdOutput();
} else {
log.error(versionCheckResult.getCommandStdError());
return null;
log.error("Get version of executor {} output error: {}", executorPath,
versionCheckResult.getCommandStdError());
}
} catch (Exception e) {
log.error("Failed to get version of executor {}.", executorFile.getAbsolutePath(), e);
return null;
log.error("Failed to get version of executor {}.", executorPath, e);
}
return null;
}


Expand Down

0 comments on commit 3024835

Please sign in to comment.