Skip to content

Commit

Permalink
Merge f005616 into 9dd6190
Browse files Browse the repository at this point in the history
  • Loading branch information
doublecompile committed Oct 4, 2021
2 parents 9dd6190 + f005616 commit aef91a6
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 132 deletions.
59 changes: 26 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
const DeployCommand = require("./lib/commands/deploy.js");
const ParamCommand = require("./lib/commands/param.js");
const SetupCommand = require("./lib/commands/setup.js");
const StatusCommand = require("./lib/commands/status.js");
const OpsCommand = require("./lib/command");
const DeployCommand = require("./lib/commands/deploy");
const ParamCommand = require("./lib/commands/param");
const SetupCommand = require("./lib/commands/setup");
const StatusCommand = require("./lib/commands/status");

/**
* I'm responsible for the infrastructure and governance side of Miles.
*/
class OpsPlugin {
/**
* A brand new me.
*/
constructor() {}
const plugin = async function opsPluginStart(builder) {
builder
.register("plugin.ops.command.deploy", () => new DeployCommand())
.register("plugin.ops.command.param", () => new ParamCommand())
.register(
"plugin.ops.command.setup",
async (c) => await SetupCommand.create(c)
)
.register("plugin.ops.command.status", () => new StatusCommand())
.register("plugin.ops.command", async (c) => await OpsCommand.create(c), [
"commander-visitor",
]);

/**
* When I wake up.
*/
async init(miles) {
this.miles = miles;
}
return {
name: "Ops",
version: "0.2.0",
description: "Control a Miles deployment",
author: "LibreWorks",
};
};

/**
* Set up commander.
*
* @param {commander.Command} program - The Commander instance.
*/
addCommands(program) {
let opsNestedCommand = program
.command("ops")
.description("Control a Miles deployment.");
const commands = [DeployCommand, ParamCommand, SetupCommand, StatusCommand];
commands
.map((clz) => new clz(this))
.forEach((cmd) => opsNestedCommand.addCommand(cmd.createCommand()));
}
}
plugin.MILES_PLUGIN_API = 1;

OpsPlugin.MILES_PLUGIN_API = 1;

module.exports = OpsPlugin;
module.exports = plugin;
56 changes: 56 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { Command } = require("commander");

const COMMANDS = Symbol("commands");

/**
* Handles the `miles ops` command.
*/
class OpsCommand {
/**
* @param {Object[]} commands - The nested commands.
*/
constructor(commands) {
this[COMMANDS] = commands;
}

/**
* Factory function.
*
* @param {container.Container} container - The dependency injection container.
* @return {OpsCommand} a new instance of this class.
*/
static async create(container) {
const commands = await container.getAll([
"plugin.ops.command.deploy",
"plugin.ops.command.param",
"plugin.ops.command.setup",
"plugin.ops.command.status",
]);
return new OpsCommand(commands);
}

/**
* Visits a Commander.js instance.
*
* @param {commander.Commander} program - The Commander.js instance.
*/
visitCommander(program) {
program.addCommand(this.createCommand());
}

/**
* Creates a Commander command.
*
* @return {commander.Command} the created Commander command.
*/
createCommand() {
const command = new Command("ops");
command.description("Control a Miles deployment.");
for (const nestedCommand of this[COMMANDS]) {
command.addCommand(nestedCommand.createCommand());
}
return command;
}
}

module.exports = OpsCommand;
6 changes: 1 addition & 5 deletions lib/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ const { Command } = require("commander");
class DeployCommand {
/**
* Creates a new DeployCommand.
*
* @param {OpsPlugin} opsPlugin - The OpsPlugin instance.
*/
constructor(opsPlugin) {
this.miles = opsPlugin.miles;
}
constructor() {}

/**
* Registers the commands with the nested Commander command.
Expand Down
6 changes: 1 addition & 5 deletions lib/commands/param.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ const { Command } = require("commander");
class ParamCommand {
/**
* Creates a new ParamCommand.
*
* @param {OpsPlugin} opsPlugin - The OpsPlugin instance.
*/
constructor(opsPlugin) {
this.miles = opsPlugin.miles;
}
constructor() {}

/**
* Registers the commands with the nested Commander command.
Expand Down
41 changes: 33 additions & 8 deletions lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const PATTERN_AWS_ACCOUNT_ID = /^\d{12}$/;
const PATTERN_ACCESS_KEY_ID = /(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])/;
const PATTERN_SECRET_ACCESS_KEY = /(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])/;
const PATTERN_REGION_CODE = /(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d/;
const CONFIG_SERVICE = Symbol("configService");
const SECRET_SERVICE = Symbol("secretService");
const INPUT = Symbol("input");

/**
* Handles the `miles ops setup` command.
Expand All @@ -13,11 +16,29 @@ class SetupCommand {
/**
* Creates a new SetupCommand.
*
* @param {OpsPlugin} opsPlugin - The OpsPlugin instance.
* @param {Input} input - The input utility.
* @param {ConfigService} configService - The Miles config service.
* @param {SecretService} secretService - The Miles secret service.
*/
constructor(opsPlugin) {
this.input = opsPlugin.miles.input;
this.config = opsPlugin.miles.config;
constructor(input, configService, secretService) {
this[INPUT] = input;
this[CONFIG_SERVICE] = configService;
this[SECRET_SERVICE] = secretService;
}

/**
* Factory function.
*
* @param {container.Container} container - The dependency injection container.
* @return {SetupCommand} a new instance of this class.
*/
static async create(container) {
const [input, configService, secretService] = await container.getAll([
"core.input",
"configService",
"secretService",
]);
return new SetupCommand(input, configService, secretService);
}

/**
Expand Down Expand Up @@ -45,7 +66,7 @@ class SetupCommand {
* @return {string} The validated AWS account ID.
*/
async getAccountId(options) {
return await this.input.getOptionOrPrompt(
return await this[INPUT].getOptionOrPrompt(
options,
"awsAccountId",
"Enter your AWS account ID: ",
Expand All @@ -65,7 +86,7 @@ class SetupCommand {
* @return {string} The validated AWS access key ID.
*/
async getAccessKeyId(options) {
return await this.input.getOptionOrPrompt(
return await this[INPUT].getOptionOrPrompt(
options,
"accessKeyId",
"Enter your IAM access key ID: ",
Expand All @@ -84,7 +105,7 @@ class SetupCommand {
* @return {string} The validated AWS secret access key.
*/
async getSecretAccessKey(options) {
return await this.input.getOptionOrPrompt(
return await this[INPUT].getOptionOrPrompt(
options,
"secretAccessKey",
"Enter your IAM secret access key: ",
Expand All @@ -103,7 +124,7 @@ class SetupCommand {
* @return {string} The validated AWS region code.
*/
async getRegionCode(options) {
return await this.input.getOptionOrPrompt(
return await this[INPUT].getOptionOrPrompt(
options,
"regionCode",
"Enter your AWS region code: ",
Expand All @@ -124,6 +145,10 @@ class SetupCommand {
const regionCode = await this.getRegionCode(options);
const accessKeyId = await this.getAccessKeyId(options);
const secretAccessKey = await this.getSecretAccessKey(options);
this[CONFIG_SERVICE].set("ops", "awsAccountId", awsAccountId);
this[CONFIG_SERVICE].set("ops", "awsRegionCode", regionCode);
this[SECRET_SERVICE].set("ops", "awsAccessKeyId", accessKeyId);
this[SECRET_SERVICE].set("ops", "awsSecretAccessKey", secretAccessKey);
console.log({ awsAccountId, regionCode, accessKeyId, secretAccessKey });
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ const { Command } = require("commander");
class StatusCommand {
/**
* Creates a new StatusCommand.
*
* @param {OpsPlugin} opsPlugin - The OpsPlugin instance.
*/
constructor(opsPlugin) {
this.miles = opsPlugin.miles;
}
constructor() {}

/**
* Registers the commands with the nested Commander command.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"url": "https://github.com/libreworks/miles-plugin-ops.git"
},
"engines": {
"node": ">=8"
"node": ">=10.13.0"
},
"keywords": [
"miles"
"miles",
"miles-plugin"
],
"contributors": [
{
Expand Down
12 changes: 2 additions & 10 deletions test/commands/deploy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ const { Help, Command } = require("commander");
const DeployCommand = require("../../lib/commands/deploy");

describe("DeployCommand", () => {
describe("#constructor", () => {
it("should have the miles property", async () => {
const miles = {};
const opsPlugin = { miles };
const object = new DeployCommand(opsPlugin);
assert.strictEqual(object.miles, miles);
});
});
describe("#install", () => {
it("should throw", async () => {
const object = new DeployCommand({ miles: {} });
const object = new DeployCommand();
assert.throws(() => object.install(), {
name: "Error",
message: "This command has not been implemented yet.",
Expand All @@ -22,7 +14,7 @@ describe("DeployCommand", () => {
});
describe("#upgrade", () => {
it("should throw", async () => {
const object = new DeployCommand({ miles: {} });
const object = new DeployCommand();
assert.throws(() => object.upgrade(), {
name: "Error",
message: "This command has not been implemented yet.",
Expand Down
8 changes: 0 additions & 8 deletions test/commands/param.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ const { Help, Command } = require("commander");
const ParamCommand = require("../../lib/commands/param");

describe("ParamCommand", () => {
describe("#constructor", () => {
it("should have the miles property", async () => {
const miles = {};
const opsPlugin = { miles };
const object = new ParamCommand(opsPlugin);
assert.strictEqual(object.miles, miles);
});
});
describe("#get", () => {
it("should throw", async () => {
const object = new ParamCommand({ miles: {} });
Expand Down

0 comments on commit aef91a6

Please sign in to comment.