Skip to content

Commit

Permalink
feat(cli): implement import command
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jul 19, 2021
1 parent 9528dd5 commit f22c21a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
9 changes: 5 additions & 4 deletions packages/cdktf-cli/bin/cmds/helper/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { logFileName, logger } from "../../../lib/logging";

const chalkColour = new chalk.Instance();

const templatesDir = path.join(__dirname, "..", "..", "templates");
const templatesDir = path.join(__dirname, "..", "..", "..", "templates");
const availableTemplates = fs
.readdirSync(templatesDir)
.filter((x) => !x.startsWith("."));
const templates: string[] = [];
export const templates: string[] = [];
for (const template of availableTemplates) {
templates.push(template);
}
Expand Down Expand Up @@ -46,8 +46,9 @@ type Options = {
projectDescription?: string;
cdktfVersion?: string;
dist?: string;
destination: string;
};
export async function runInit(argv: Options = {}) {
export async function runInit(argv: Options = { destination: "." }) {
let token = "";
if (!argv.local) {
// We ask the user to login to Terraform Cloud and set a token
Expand Down Expand Up @@ -102,7 +103,7 @@ This means that your Terraform state file will be stored locally on disk in a fi
.map(([key, value]) => `"${key}": "${value}"`)
.join(`,\n`);

await sscaff(templateInfo.Path, ".", {
await sscaff(templateInfo.Path, argv.destination, {
...deps,
...projectInfo,
futureFlags,
Expand Down
89 changes: 89 additions & 0 deletions packages/cdktf-cli/bin/cmds/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import yargs from "yargs";
import { convertProject, getProjectTerraformFiles } from "@cdktf/hcl2cdk";
import { displayVersionMessage } from "./version-check";
import * as path from "path";
import { checkForEmptyDirectory, runInit, templates } from "./helper/init";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require("../../package.json");

class Command implements yargs.CommandModule {
public readonly command = "import [OPTIONS] <source> <destination>";
public readonly describe =
"Takes the Terraform project and converts it to a CDKTF version";

public readonly builder = (args: yargs.Argv) =>
args
.example(
"cdktf import --dest=../cdktf-project ./",
"Takes the Terraform project in ./ and writes a CDKTF version in ../cdktf-project"
)

.option("language", {
choices: ["typescript"],
default: "typescript",
})
.option("template", {
type: "string",
desc: `The template to be used to create a new project. Either URL to zip file or one of the built-in templates: [${templates
.map((t) => `"${t}"`)
.join(", ")}]`,
})
.option("project-name", {
type: "string",
desc: "The name of the project.",
})
.option("project-description", {
type: "string",
desc: "The description of the project.",
})
.option("dist", {
type: "string",
desc: 'Install dependencies from a "dist" directory (for development)',
})
.option("local", {
type: "boolean",
desc: "Use local state storage for generated Terraform.",
default: false,
})
.option("cdktf-version", {
type: "string",
desc: "The cdktf version to use while creating a new project.",
default: pkg.version,
})
.positional("source", {
describe: "Terraform Project to transform to CDK",
})
.positional("destination", {
describe: "Path to where the project should be created",
})
.showHelpOnFail(true);

public async handler({ language, destination, source, ...argv }: any) {
if (!source) {
throw new Error(
`Expected first positional argument to be the source terraform project`
);
}
if (!destination) {
throw new Error(
`Expected second positional argument to be the destination folder`
);
}

await displayVersionMessage();
console.warn(
"This command does not import your state, it only generates CDKTF source code"
);
const absolutePath = path.resolve(destination);
checkForEmptyDirectory(absolutePath);

await runInit({ ...argv, destination });

await convertProject(getProjectTerraformFiles(source), absolutePath, {
language,
});
}
}

module.exports = new Command();
13 changes: 1 addition & 12 deletions packages/cdktf-cli/bin/cmds/init.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import yargs from "yargs";
import * as fs from "fs-extra";
import * as path from "path";
import { terraformCheck } from "./terraform-check";
import { displayVersionMessage } from "./version-check";
import { checkForEmptyDirectory, runInit } from "./helper/init";

const templatesDir = path.join(__dirname, "..", "..", "templates");
const availableTemplates = fs
.readdirSync(templatesDir)
.filter((x) => !x.startsWith("."));
const templates: string[] = [];
for (const template of availableTemplates) {
templates.push(template);
}
import { checkForEmptyDirectory, runInit, templates } from "./helper/init";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require("../../package.json");
Expand Down

0 comments on commit f22c21a

Please sign in to comment.