Skip to content

Commit

Permalink
feat: select or create new resource group
Browse files Browse the repository at this point in the history
THIS COMMIT INTRODUCES A LOT OF REFACTORING
  • Loading branch information
manekinekko committed Sep 1, 2019
1 parent b942c9b commit c212b57
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 76 deletions.
12 changes: 9 additions & 3 deletions src/commands/init.ts
@@ -1,4 +1,4 @@
import { isProjectFileExists, saveProjectConfigToDisk } from "../lib/utils";
import { isProjectFileExists, saveWorkspace, Config } from "../lib/utils";
import {
askIfOverrideProjectFile,
askForProjectDetails,
Expand All @@ -15,21 +15,27 @@ module.exports = async function() {
}

const project = await askForProjectDetails();


await (require(`./login`)());
await (require(`./resource-group-selection`)());

const { features } = await askForFeatures();
const featuresConfiguration: any = {};

for await (let feature of features) {
console.log(`Configuring ${chalk.green(feature)}:`);
try {
const featureImplementation = require(`./lib/features/${feature}/index`);
const featureImplementation = require(`../features/${feature}/index`);
const config = await featureImplementation();
featuresConfiguration[feature] = config;
Config.get(feature, config);
} catch (error) {
console.error(error.toString());
}
}

saveProjectConfigToDisk({
saveWorkspace({
project,
...featuresConfiguration
});
Expand Down
22 changes: 12 additions & 10 deletions src/commands/login.ts
@@ -1,23 +1,25 @@
import { az, saveProjectConfigToDisk } from "../lib/utils";
import { az, saveWorkspace, Config } from "../lib/utils";
import { chooseSubscription } from "../lib/prompt";

module.exports = async function() {
// console.log(chalk.green(`Fetching subscriptions...`));

// @todo save these subscriptions globally.
let subscriptions: string = await az(
let subscriptionsList = await az<AzureSubscription[]>(
`login --query '[].{name:name, state:state, id:id}'`,
`Loading your subscriptions...`
);

if (subscriptions.length) {
const subscriptionsList = JSON.parse(subscriptions) as AzureSubscription[];
let selectedSubscription = (await chooseSubscription(subscriptionsList))
Config.set("subscriptions", subscriptionsList);

if (subscriptionsList.length) {
let selectedSubscriptionId = (await chooseSubscription(subscriptionsList))
.subscription as string;
const { id, name } = subscriptionsList.find(
(sub: AzureSubscription) => sub.name === selectedSubscription
(subscription: AzureSubscription) =>
subscription.id === selectedSubscriptionId
) as AzureSubscription;
saveProjectConfigToDisk({

Config.set("subscription", { id, name });

saveWorkspace({
subscription: {
id,
name
Expand Down
12 changes: 12 additions & 0 deletions src/commands/push.ts
@@ -0,0 +1,12 @@
import { push } from "../features/hosting/command";
import { Config } from "../lib/utils";

module.exports = async function() {
const subscription = Config.get("subscription") as AzureSubscription;
const storage = Config.get("storage") as AzureStorage;

await push({
subscriptionId: subscription.id,
storageAccountName: storage.name
});
};
22 changes: 22 additions & 0 deletions src/commands/resource-group-create.ts
@@ -0,0 +1,22 @@
import { askForResourceGroupDetails } from "../lib/prompt";
import { az, saveWorkspace } from "../lib/utils";

module.exports = async function() {
let regionsList = await az<AzureRegion[]>(
`account list-locations --query '[].{name:name, id:id, displayName:displayName}'`,
`Loading your regions (this may take few minutes)...`
);

const { resource, region } = await askForResourceGroupDetails(regionsList);

let resourceGroup = await az<AzureResourceGroup>(
`group create -l ${region} -n ${resource} --tag cli=nitro --query '[].{name:name, id:id, location:location}'`,
`Creating your resource group...`
);

console.log("asdasdsdasdasd");

saveWorkspace({
resourceGroup
});
};
30 changes: 30 additions & 0 deletions src/commands/resource-group-selection.ts
@@ -0,0 +1,30 @@
import { chooseResourceGroup } from "../lib/prompt";
import { az, Config, saveWorkspace } from "../lib/utils";

module.exports = async function() {
let resourceGroupsList = await az<AzureResourceGroup[]>(
`group list --query '[].{name:name, id:id, location:location}'`,
`Loading your resource groups...`
);

if (resourceGroupsList.length) {
let selectedResourceId = (await chooseResourceGroup(resourceGroupsList)).resourceGroup as string;

if (selectedResourceId === "") {
// create a new resource group
return (await require(`./resource-group-create`))();
} else {
const { id, name, location } = resourceGroupsList.find(
(resourceGroup: AzureResourceGroup) => resourceGroup.id === selectedResourceId
) as AzureResourceGroup;

saveWorkspace({
resourceGroup: {
id,
location,
name
}
});
}
}
};
16 changes: 8 additions & 8 deletions src/features/hosting/command.ts
@@ -1,13 +1,13 @@
import { az } from "../../lib/utils";

export async function push() {
export async function push({ subscriptionId, storageAccountName }: { subscriptionId: string, storageAccountName: string }) {
await az(
`storage blob service-properties update --account-name <storage-account-name> --static-website --404-document <error-document-name> --index-document <index-document-name>`
);
await az(
`storage blob upload-batch -s <source-path> -d \$web --account-name <storage-account-name>`
);
await az(
`storage account show -n <storage-account-name> -g <resource-group-name> --query "primaryEndpoints.web"`
`storage blob service-properties update --account-name ${storageAccountName} --static-website --404-document 404.html --index-document index.html`
);
// await az(
// `storage blob upload-batch -s <source-path> -d \$web --account-name <storage-account-name>`
// );
// await az(
// `storage account show -n <storage-account-name> -g <resource-group-name> --query "primaryEndpoints.web"`
// );
}
1 change: 1 addition & 0 deletions src/features/hosting/index.ts
Expand Up @@ -12,6 +12,7 @@ module.exports = async function(): Promise<inquirer.Answers> {
default: "public",
validate: function(value: string) {
if (value && value.length) {
// TODO: copy template files if new created folder
return createDirectoryIfNotExists(value);
} else {
return "Please enter a public folder.";
Expand Down
7 changes: 7 additions & 0 deletions src/features/storage/commands.ts
@@ -0,0 +1,7 @@
import { az } from "../../lib/utils";

export async function create({ name }: AzureStorage) {
await az(
`storage account create -n ${name} -g MyResourceGroup -l westus --sku Standard_LRS`
);
}
12 changes: 0 additions & 12 deletions src/features/storage/index.ts
Expand Up @@ -15,18 +15,6 @@ module.exports = async function(): Promise<inquirer.Answers> {
return "Please enter a valid name.";
}
}
},
{
type: "input",
name: "sas",
message: "Enter your storage SAS token:",
validate: function(value: string) {
if (value.length) {
return true;
} else {
return "Please enter a valid SAS token.";
}
}
}
];
return inquirer.prompt(questions);
Expand Down
32 changes: 20 additions & 12 deletions src/index.ts
@@ -1,5 +1,7 @@
#!/usr/bin/env node

process.env.DEBUG = "*";

import chalk from "chalk";
import clear from "clear";
import figlet from "figlet";
Expand All @@ -16,25 +18,31 @@ console.log(
);

(async () => {
const runCommand = async (commandName: string) => {
try {
return (await require(`./commands/${commandName}`))();
} catch (error) {
console.error(chalk.red(`Command "${commandName}" not supported yet.`));
console.error(chalk.red(error));
program.outputHelp();
}
};
program
.name("nitro")
.usage("<command>")
.version(require("../package.json").version)
.option("--init", "initialise a new workspace")
.option("--login", "connect to your Azure")
.option("--push", "deploy the app to Azure")
.option("login, --login", "connect to your Azure")
.option("init, --init", "initialise a new workspace")
.option("push, --push", "deploy the app to Azure")
.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}
// use process.argv not program.argv
const commandName = process.argv[2];

const commandName = program.args[0];

try {
(await require(`./commands/${commandName}`))();
} catch (error) {
console.error(chalk.red(`Command ${commandName} not supported.`));
if (!process.argv.slice(2).length || !commandName) {
program.outputHelp();
process.exit(0);
}

runCommand(commandName.replace("--", ""));
})();

0 comments on commit c212b57

Please sign in to comment.