terrac
Version:
A minimal private module registry for Terraform and OpenTofu
108 lines (107 loc) • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = require("node:path");
const core_1 = require("@oclif/core");
const prompts_1 = require("@inquirer/prompts");
const utils_1 = require("../utils");
class Init extends core_1.Command {
async run() {
const { flags } = await this.parse(Init);
const workDir = flags['work-directory'];
// ask for backend type
const backendType = await (0, prompts_1.select)({
message: 'Select a backend type',
choices: [
{
name: 'AWS S3',
value: 's3',
},
{
name: 'GCP Stroage',
value: 'gcp',
},
],
});
const config = {
backend: await this.askForBackendConfig(backendType),
};
const isModule = await (0, prompts_1.confirm)({ message: 'Is this a module to publish?' });
if (isModule) {
config.module = await this.askForModuleConfig();
}
await (0, utils_1.saveConfig)(workDir, config);
this.log('Initialization is completed.');
}
async askForModuleConfig() {
const name = await (0, prompts_1.input)({
message: 'Module name',
default: (0, node_path_1.basename)(process.cwd()),
});
const version = await (0, prompts_1.input)({
message: 'Module version',
default: '0.0.0',
});
return { name, version };
}
async askForBackendConfig(type) {
switch (type) {
case 's3':
return this.askForS3Config();
case 'gcp':
return this.askForGCPConfig();
case 'azure':
return this.askForAzureConfig();
default:
throw new Error(`Backend type "${type}" is not supported.`);
}
}
async askForS3Config() {
const bucket = await this.askForInput('Bucket');
const region = await this.askForInput('Region');
const keyPrefix = await this.askForInput('Object key prefix', true);
return {
type: 's3',
bucket,
region,
keyPrefix,
};
}
async askForGCPConfig() {
const bucket = await this.askForInput('Bucket');
const projectId = await this.askForInput('Project');
const pathPrefix = await this.askForInput('Object path prefix', true);
return {
type: 'gcp',
bucket,
projectId,
pathPrefix,
};
}
async askForAzureConfig() {
const account = await this.askForInput('Account');
const container = await this.askForInput('Container');
const fileNamePrefix = await this.askForInput('File name prefix', true);
return {
type: 'azure',
account,
container,
fileNamePrefix,
};
}
async askForInput(message, optional = false) {
const inputMessage = optional ? `[Optional] ${message}` : message;
return (0, prompts_1.input)({ message: inputMessage });
}
}
exports.default = Init;
Init.description = 'Initialize terrac configuration in a directory.';
Init.examples = [
'<%= config.bin %> <%= command.id %>',
];
Init.flags = {
'work-directory': core_1.Flags.string({
summary: 'Root directory of the module project',
default: '.',
}),
};
Init.args = {};