Skip to content

Commit

Permalink
feat: make collie info detect opentofu or terraform
Browse files Browse the repository at this point in the history
closes #289
  • Loading branch information
JohannesRudolph committed Aug 27, 2024
1 parent 39aad97 commit 73eef4b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/api/CliApiFacadeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import { TerraformDocsCliFacade } from "./terraform-docs/TerraformDocsCliFacade.
import { CollieRepository } from "../model/CollieRepository.ts";
import { GitCliDetector } from "./git/GitCliDetector.ts";
import { GitCliFacade } from "./git/GitCliFacade.ts";
import { TerraformCliFacade } from "./terraform/TerraformCliFacade.ts";
import { TofuOrTerraformCliDetector } from "./terraform/TofuOrTerraformCliDetector.ts";
import { OpenTofuCliDetector } from "./terraform/OpenTofuCliDetector.ts";

export class CliApiFacadeFactory {
constructor(
Expand All @@ -49,7 +50,10 @@ export class CliApiFacadeFactory {
new AzCliDetector(processRunner),
new GcloudCliDetector(processRunner),
new GitCliDetector(processRunner),
new TerraformCliDetector(processRunner),
new TofuOrTerraformCliDetector(
new OpenTofuCliDetector(processRunner),
new TerraformCliDetector(processRunner),
),
new TerragruntCliDetector(processRunner),
new TerraformDocsCliDetector(processRunner),
new NpmCliDetector(processRunner),
Expand Down
7 changes: 6 additions & 1 deletion src/api/CliDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export type CliDetectionResult =
info: string;
};

export abstract class CliDetector {
export interface ICliDetector {
tryRaiseInstallationStatusError(): Promise<void>;
detect(): Promise<CliDetectionResult>;
}

export abstract class CliDetector implements ICliDetector {
constructor(
protected readonly cli: string,
protected readonly runner: IProcessRunner<ProcessResultWithOutput>,
Expand Down
17 changes: 17 additions & 0 deletions src/api/terraform/OpenTofuCliDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IProcessRunner } from "../../process/IProcessRunner.ts";
import { ProcessResultWithOutput } from "../../process/ProcessRunnerResult.ts";
import { CliDetector } from "../CliDetector.ts";

export class OpenTofuCliDetector extends CliDetector {
constructor(runner: IProcessRunner<ProcessResultWithOutput>) {
super("tofu", runner);
}

protected parseVersion(versionCmdOutput: string): string {
return versionCmdOutput.split("\n")[0].substring("OpenTofu ".length);
}

protected isSupportedVersion(version: string): boolean {
return CliDetector.testSemverSatisfiesRange(version, ">=1.0.0");
}
}
32 changes: 32 additions & 0 deletions src/api/terraform/TofuOrTerraformCliDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CliDetectionResult, ICliDetector } from "../CliDetector.ts";
import { InstallationStatus } from "/api/CliInstallationStatus.ts";
import { CliInstallationStatusError } from "/errors.ts";
import { OpenTofuCliDetector } from "./OpenTofuCliDetector.ts";
import { TerraformCliDetector } from "./TerraformCliDetector.ts";

export class TofuOrTerraformCliDetector implements ICliDetector {
constructor(
private readonly tofu: OpenTofuCliDetector,
private readonly terraform: TerraformCliDetector,
) {
}
async detect(): Promise<CliDetectionResult> {
const tofuResult = await this.tofu.detect();
if (tofuResult.status === InstallationStatus.Installed) {
return tofuResult;
}

return this.terraform.detect();
}

async tryRaiseInstallationStatusError() {
const { status } = await this.detect();
switch (status) {
case InstallationStatus.Installed:
break;
case InstallationStatus.NotInstalled:
case InstallationStatus.UnsupportedVersion:
throw new CliInstallationStatusError("tofu or terraform", status);
}
}
}

0 comments on commit 73eef4b

Please sign in to comment.