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

fix(cli): throw explicit error when commands are invoked outside of a cdktf project #854

Merged
merged 2 commits into from
Jul 29, 2021
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
2 changes: 1 addition & 1 deletion packages/cdktf-cli/bin/cdktf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import * as os from "os";
import * as fs from "fs-extra";
import { terraformVersion } from "./cmds/helper/terraform";
import { DISPLAY_VERSION } from "./cmds/version-check";
import { DISPLAY_VERSION } from "./cmds/helper/version-check";

const ensurePluginCache = (): string => {
const pluginCachePath =
Expand Down
2 changes: 1 addition & 1 deletion packages/cdktf-cli/bin/cmds/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yargs from "yargs";
import { convert } from "@cdktf/hcl2cdk";
import { displayVersionMessage } from "./version-check";
import { displayVersionMessage } from "./helper/version-check";
import { sendTelemetry } from "../../lib/checkpoint";
import { Errors } from "../../lib/errors";

Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as yargs from "yargs";
import React from "react";
import { Deploy } from "./ui/deploy";
import { readConfigSync } from "../../lib/config";
import { renderInk } from "./render-ink";
import { displayVersionMessage } from "./version-check";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand Down Expand Up @@ -39,6 +40,7 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("deploy");
await displayVersionMessage();
const command = argv.app;
const outdir = argv.output;
Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as yargs from "yargs";
import React from "react";
import { Destroy } from "./ui/destroy";
import { readConfigSync } from "../../lib/config";
import { renderInk } from "./render-ink";
import { displayVersionMessage } from "./version-check";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand Down Expand Up @@ -38,6 +39,7 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("destroy");
await displayVersionMessage();
const command = argv.app;
const outdir = argv.output;
Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import yargs from "yargs";
import React from "react";
import { Diff } from "./ui/diff";
import { readConfigSync } from "../../lib/config";
import { renderInk } from "./render-ink";
import { displayVersionMessage } from "./version-check";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand Down Expand Up @@ -34,6 +35,7 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("diff");
await displayVersionMessage();
const command = argv.app;
const outdir = argv.output;
Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from "../../lib/config";
import { Language, LANGUAGES } from "../../lib/get/constructs-maker";
import { Get } from "./ui/get";
import { renderInk } from "./render-ink";
import { displayVersionMessage } from "./version-check";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand Down Expand Up @@ -40,6 +41,7 @@ class Command implements yargs.CommandModule {
});

public async handler(argv: any) {
throwIfNotProjectDirectory("get");
await displayVersionMessage();
const args = argv as Arguments;
const providers = config.terraformProviders ?? [];
Expand Down
23 changes: 23 additions & 0 deletions packages/cdktf-cli/bin/cmds/helper/check-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from "path";
import { Errors } from "../../../lib/errors";
export function isCdktfProjectDirectory(directory: string): boolean {
try {
const cdktfPath = path.join(directory, "cdktf.json");
const cdktf = require(cdktfPath);
return cdktf.language && cdktf.app;
} catch {
return false;
}
}

export function throwIfNotProjectDirectory(
command: string,
directory = process.cwd()
): void {
if (!isCdktfProjectDirectory(directory)) {
throw Errors.Usage(
command,
`${directory} is not a cdktf project directory, no cdktf.json found or cdktf.json is missing language / app keys`
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from "ink";
import React from "react";
import { exit } from "process";
import { App } from "./ui/app";
import { terraformCheck } from "./terraform-check";
import { App } from "../ui/app";
import { terraformCheck } from "../helper/terraform-check";

export const renderInk = async (component: React.ReactElement) => {
await terraformCheck();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TerraformCli } from "./ui/models/terraform-cli";
import { TerraformCli } from "../ui/models/terraform-cli";
import * as semver from "semver";
import { SynthesizedStack } from "./helper/synth-stack";
import { SynthesizedStack } from "./synth-stack";
import { existsSync } from "fs-extra";
import * as path from "path";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const DISPLAY_VERSION = `${versionNumber()}`;

export function versionNumber(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require("../../package.json").version.replace(/\+[0-9a-f]+$/, "");
return require("../../../package.json").version.replace(/\+[0-9a-f]+$/, "");
}

export class VersionCheckTTL {
Expand Down
4 changes: 2 additions & 2 deletions packages/cdktf-cli/bin/cmds/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yargs from "yargs";
import { terraformCheck } from "./terraform-check";
import { displayVersionMessage } from "./version-check";
import { terraformCheck } from "./helper/terraform-check";
import { displayVersionMessage } from "./helper/version-check";
import { checkForEmptyDirectory, runInit, templates } from "./helper/init";

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import yargs from "yargs";
import React from "react";
import { List } from "./ui/list";
import { readConfigSync } from "../../lib/config";
import { renderInk } from "./render-ink";
import { displayVersionMessage } from "./version-check";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand All @@ -26,6 +27,7 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("list");
await displayVersionMessage();
const command = argv.app;
const outdir = argv.output;
Expand Down
4 changes: 2 additions & 2 deletions packages/cdktf-cli/bin/cmds/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import yargs from "yargs";
import { TerraformLogin } from "./helper/terraform-login";
import * as terraformCloudClient from "./helper/terraform-cloud-client";
import * as chalk from "chalk";
import { terraformCheck } from "./terraform-check";
import { displayVersionMessage } from "./version-check";
import { terraformCheck } from "./helper/terraform-check";
import { displayVersionMessage } from "./helper/version-check";

const chalkColour = new chalk.Instance();

Expand Down
6 changes: 4 additions & 2 deletions packages/cdktf-cli/bin/cmds/synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import yargs from "yargs";
import React from "react";
import { Synth } from "./ui/synth";
import { readConfigSync } from "../../lib/config";
import { renderInk } from "./render-ink";
import { renderInk } from "./helper/render-ink";
import * as fs from "fs-extra";
import { displayVersionMessage } from "./version-check";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";

const config = readConfigSync();

Expand Down Expand Up @@ -38,6 +39,7 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("synth");
await displayVersionMessage();
const command = argv.app;
const outdir = argv.output;
Expand Down
2 changes: 1 addition & 1 deletion packages/cdktf-cli/lib/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { format } from "url";
import { v4 as uuidv4 } from "uuid";
import * as os from "os";
import { processLogger } from "./logging";
import { versionNumber } from "../bin/cmds/version-check";
import { versionNumber } from "../bin/cmds/helper/version-check";

const BASE_URL = `https://checkpoint-api.hashicorp.com/v1/`;

Expand Down
2 changes: 1 addition & 1 deletion packages/cdktf-cli/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReportParams, ReportRequest } from "./checkpoint";
import { versionNumber } from "../bin/cmds/version-check";
import { versionNumber } from "../bin/cmds/helper/version-check";

// Errors that will emit telemetry events
async function report(command: string, payload: Record<string, any>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cdktf-cli/lib/get/constructs-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ProviderSchema, readSchema } from "./generator/provider-schema";
import { TerraformProviderGenerator } from "./generator/provider-generator";
import { ModuleGenerator } from "./generator/module-generator";
import { ModuleSchema } from "./generator/module-schema";
import { versionNumber } from "../../bin/cmds/version-check";
import { versionNumber } from "../../bin/cmds/helper/version-check";
import { ReportParams, ReportRequest } from "../checkpoint";
import { Errors } from "../errors";

Expand Down