Skip to content

Commit

Permalink
fix(cli): polish user interaction by switching libraries
Browse files Browse the repository at this point in the history
Fixes #788
  • Loading branch information
DanielMSchmidt committed Jul 28, 2021
1 parent d262eb3 commit 15b090e
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 84 deletions.
113 changes: 59 additions & 54 deletions packages/cdktf-cli/bin/cmds/helper/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs-extra";
import * as chalk from "chalk";
import * as readlineSync from "readline-sync";
import * as inquirer from "inquirer";
import extract from "extract-zip";
import { TerraformLogin } from "../helper/terraform-login";

Expand Down Expand Up @@ -245,33 +245,31 @@ async function gatherInfo(
projectName?: string,
projectDescription?: string
): Promise<Project> {
if (!projectName && !projectDescription) {
console.log(chalkColour`\nWe will now set up the project. Please enter the details for your project.
If you want to exit, press {magenta ^C}.
`);
}

if (!projectName) {
// Current working directory
const currentDirectory = path.basename(process.cwd());
projectName = readlineSync.question(
chalkColour`{greenBright Project Name:} (default: '${currentDirectory}') `,
{ defaultInput: currentDirectory }
);
}

if (!projectDescription) {
const projectDescriptionDefault =
"A simple getting started project for cdktf.";
projectDescription = readlineSync.question(
chalkColour`{greenBright Project Description:} (default: '${projectDescriptionDefault}') `,
{ defaultInput: projectDescriptionDefault }
);
}
const currentDirectory = path.basename(process.cwd());
const projectDescriptionDefault =
"A simple getting started project for cdktf.";
const questions = [
...(!projectName
? [{ name: "projectName", default: currentDirectory }]
: []),
...(!projectDescription
? [
{
name: "projectDescription",
default: projectDescriptionDefault,
},
]
: []),
];

const answers: {
projectName?: string;
projectDescription?: string;
} = questions.length > 0 ? await inquirer.prompt(questions) : {};

const project: Project = {
Name: projectName,
Description: projectDescription,
Name: projectName || answers.projectName || "",
Description: projectDescription || answers.projectDescription || "",
OrganizationName: "",
WorkspaceName: "",
};
Expand All @@ -291,23 +289,27 @@ If you want to exit, press {magenta ^C}.
}

// todo: add validation for the organization name and workspace. add error handling
const organizationSelect = readlineSync.keyInSelect(
organizationOptions,
chalkColour`{blueBright Terraform Cloud Organization Name:} `
);
if (organizationSelect == -1) {
process.exit(0);
}
const { organization: organizationSelect } = await inquirer.prompt([
{
type: "list",
name: "organization",
message: "Terraform Cloud Organization Name",
choices: organizationOptions,
},
]);

console.log(
chalkColour`\nWe are going to create a new {blueBright Terraform Cloud Workspace} for your project.\n`
);

const workspaceName = readlineSync.question(
chalkColour`{blueBright Terraform Cloud Workspace Name:} (default: '${templateName}') `,
{ defaultInput: templateName }
);
project.OrganizationName = organizationOptions[organizationSelect];
const { workspace: workspaceName } = await inquirer.prompt([
{
name: "workspace",
message: "Terraform Cloud Workspace Name",
default: templateName,
},
]);
project.OrganizationName = organizationSelect;
project.WorkspaceName = workspaceName;
}

Expand All @@ -323,23 +325,26 @@ async function getTemplate(templateName: string): Promise<Template> {
const templateOptionRemote = "<remote zip file>";
const options = [...templates, templateOptionRemote];
// Prompt for template
const selection = readlineSync.keyInSelect(
options,
chalkColour`{whiteBright What template you want to use?}`
);
if (selection == -1) {
process.exit(0);
}
if (selection === options.indexOf(templateOptionRemote)) {
templateName = readlineSync.question(
"Please enter an URL pointing to the template zip file you want to use: "
);
if (templateName == "") {
console.log("No URL was given (received empty string). Aborted.");
process.exit(1);
}
const { template: selection } = await inquirer.prompt([
{
type: "list",
name: "template",
message: "What template you want to use?",
choices: options,
},
]);

if (selection === templateOptionRemote) {
const { templateName: remoteTemplateName } = await inquirer.prompt([
{
name: "templateName",
message:
"Please enter an URL pointing to the template zip file you want to use:",
},
]);
templateName = remoteTemplateName;
} else {
templateName = options[selection];
templateName = selection;
console.log(
chalkColour`\n{whiteBright Initializing a project using the {greenBright ${templateName}} template.}`
);
Expand Down
33 changes: 21 additions & 12 deletions packages/cdktf-cli/bin/cmds/helper/terraform-login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "fs";
import * as readlineSync from "readline-sync";
import * as inquirer from "inquirer";
import * as open from "open";
import * as chalk from "chalk";

Expand All @@ -21,7 +21,7 @@ export interface TerraformCredentialsFile {
}

export class TerraformLogin {
public askToContinue(): boolean {
public async askToContinue(): Promise<boolean> {
// Describe the command
console.log(chalkColour`{greenBright Welcome to CDK for Terraform!}
Expand All @@ -37,11 +37,16 @@ the following file for use by subsequent Terraform commands:

let isLogin = false;

const c = readlineSync.question(
chalkColour`{whiteBright Do you want to continue with Terraform Cloud remote state management (yes/no)?} `,
{ defaultInput: "yes" }
);
if (c == "yes" || c == "y" || c == "\n") {
const { tfCloud } = await inquirer.prompt([
{
name: "tfCloud",
type: "confirm",
message:
"Do you want to continue with Terraform Cloud remote state management?",
},
]);

if (tfCloud) {
isLogin = true;
this.openBrowser();
}
Expand All @@ -57,10 +62,14 @@ the following file for use by subsequent Terraform commands:
}

public async askForToken() {
return readlineSync.question(
chalkColour`Token for {bold app.terraform.io}: 🔑 `,
{ hideEchoBack: true, mask: "" }
);
const { token } = await inquirer.prompt([
{
name: "token",
message: "Token for app.terraform.io 🔑",
type: "password",
},
]);
return token;
}

public async saveTerraformCredentials(token: string) {
Expand Down Expand Up @@ -105,7 +114,7 @@ the following file for use by subsequent Terraform commands:
const checkToken = await this.checkIfTerraformCredentialsExist();
// user login if not already
if (!checkToken) {
const shouldContinue = this.askToContinue();
const shouldContinue = await this.askToContinue();
if (shouldContinue) {
const token = await this.askForToken();
if (token == "") {
Expand Down
13 changes: 9 additions & 4 deletions packages/cdktf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"license": "MPL-2.0",
"dependencies": {
"@cdktf/hcl2json": "0.0.0",
"@cdktf/hcl2cdk": "0.0.0",
"@cdktf/hcl2json": "0.0.0",
"@skorfmann/ink-confirm-input": "^3.0.0",
"@skorfmann/terraform-cloud": "^1.10.1",
"@types/node": "^14.0.26",
Expand All @@ -43,11 +43,11 @@
"indent-string": "^4.0.0",
"ink": "^3.0.8",
"ink-spinner": "^4.0.1",
"inquirer": "^8.1.2",
"jsii-srcmak": "^0.1.272",
"log4js": "^6.3.0",
"open": "^7.0.4",
"react": "<17.0.0",
"readline-sync": "^1.4.10",
"semver": "^7.3.2",
"sscaff": "^1.2.0",
"stream-buffers": "^3.0.2",
Expand Down Expand Up @@ -82,7 +82,12 @@
"@typescript-eslint/no-var-requires": 0,
"react/no-unescaped-entities": 0,
"no-sequences": "error",
"no-irregular-whitespace": ["error", { "skipTemplates": true }]
"no-irregular-whitespace": [
"error",
{
"skipTemplates": true
}
]
},
"ignorePatterns": [
"node_modules",
Expand All @@ -96,11 +101,11 @@
"@types/fs-extra": "^8.1.0",
"@types/ink": "^2.0.3",
"@types/ink-spinner": "^3.0.0",
"@types/inquirer": "^7.3.3",
"@types/jest": "^25.1.2",
"@types/json-schema": "^7.0.4",
"@types/nock": "^11.1.0",
"@types/react": "^16.9.35",
"@types/readline-sync": "^1.4.3",
"@types/semver": "^7.1.0",
"@types/stream-buffers": "^3.0.3",
"@types/uuid": "^8.3.0",
Expand Down
Loading

0 comments on commit 15b090e

Please sign in to comment.