Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove collie kit compile command and add opentofu support #296

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 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 Expand Up @@ -114,9 +118,6 @@ export class CliApiFacadeFactory {
return azure;
}

// buildCustom() {
// }

public buildGit() {
const detectorRunner = this.buildQuietLoggingProcessRunner();
const detector = new GitCliDetector(detectorRunner);
Expand Down Expand Up @@ -166,18 +167,6 @@ export class CliApiFacadeFactory {
return new TerraformDocsCliFacade(repo, processRunner);
}

public buildTerraform() {
const quietRunner = this.buildQuietLoggingProcessRunner();
const detector = new TerraformCliDetector(quietRunner);

const processRunner = this.wrapFacadeProcessRunner(
quietRunner,
new ProcessRunnerErrorResultHandler(detector),
);

return new TerraformCliFacade(processRunner);
}

// DESIGN: we need to build up the ProcessRunner behavior in the following order (from outer to inner)
// - DefaultsProcessRunnerDecorator -> customise the command that gets run
// - ResultHandlerProcessRunnerDecorator -> retry/print error on what actually ran
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");
}
}
22 changes: 0 additions & 22 deletions src/api/terraform/TerraformCliFacade.ts

This file was deleted.

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);
}
}
}
87 changes: 0 additions & 87 deletions src/commands/kit/compile.command.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/commands/kit/kit.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { makeTopLevelCommand, TopLevelCommand } from "../TopLevelCommand.ts";
import { registerApplyCmd } from "./apply.command.ts";
import { registerCompileCmd } from "./compile.command.ts";
import { registerImportCmd } from "./import.command.ts";
import { registerNewCmd } from "./new.command.ts";
import { registerTreeCmd } from "./tree.command.ts";
Expand All @@ -11,7 +10,6 @@ export function registerKitCommand(program: TopLevelCommand) {
registerApplyCmd(kitCommands);
registerImportCmd(kitCommands);
registerTreeCmd(kitCommands);
registerCompileCmd(kitCommands);

program
.command("kit", kitCommands)
Expand Down
Loading