Skip to content

Commit

Permalink
feat: add a dry-run and verbose modes
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Nov 1, 2019
1 parent 81020c0 commit d54c652
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/commands/init.ts
Expand Up @@ -65,7 +65,9 @@ module.exports = async function(options?: HexaInitOptions) {
} catch (error) {
console.log(chalk.red(`✗ ${error.stack || error}`));
console.log(chalk.red(`✗ Abort.`));
process.exit(1);
if (!!process.env.HEXA_DRY_RUN) {
process.exit(1);
}
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/core/utils.ts
Expand Up @@ -7,7 +7,8 @@ const ora = require("ora");
const Configstore = require("configstore");
const dotenv = require("dotenv");
const packageJson = require("../../package.json");
const debug = require("debug")(`hexa`);
const debugCli = require("debug")(`hexa`);
const debug = require("debug")(`utils`);
const crypto = require("crypto");

// generate a Global UUID per execution.
Expand All @@ -22,7 +23,7 @@ export const uuid = () => {
};

export const sanitize = (name: string) =>
name
(name || "")
.replace(/[\W_]+/gim, "")
.trim()
.substr(0, 20);
Expand Down Expand Up @@ -57,7 +58,7 @@ export const FEATURES = [
short: "Kubernetes"
}
];
export const WORKSPACE_FILENAME = "hexa.json";
export const WORKSPACE_FILENAME = process.env.HEXA_DRY_RUN ? "hexa.test.json" : "hexa.json";
export const ENV_FILENAME = ".env";

const IS_DEBUG = !!process.env.DEBUG;
Expand All @@ -73,7 +74,17 @@ export async function runCmd(command: string, loadingMessage?: string, options?:
if (options && options.cwd) {
debug(`cwd=${chalk.cyan(options.cwd)}`);
}
debug(chalk.cyan(command));

debugCli(chalk.cyan(command));

if (process.env.HEXA_DRY_RUN) {
try {
spinner.succeed();
} catch (error) {
// don't catch errors here
}
return resolve("HEXA_DRY_RUN");
}

shell.exec(
command,
Expand Down
12 changes: 8 additions & 4 deletions src/features/container-registry/index.ts
Expand Up @@ -12,11 +12,15 @@ module.exports = async function() {
debug(`Using resource group ${chalk.green(resourceGroup.name)}`);

// https://docs.microsoft.com/en-us/cli/azure/acr?view=azure-cli-latest#az-acr-list
const acrList = await az<AzureContainerRegistry[]>(
let acrList = await az<AzureContainerRegistry[]>(
`acr list --resource-group "${resourceGroup.name}" --subscription "${subscription.id}" --query "[].{id:id, name:name, hostname:loginServer, tags:tags}"`,
`Checking Container Registry for project ${chalk.cyan(resourceGroup.name)}...`
);

if (!Array.isArray(acrList)) {
acrList = [];
}

let creationMode = process.env.HEXA_AUTO_MODE ? "AUTOMATIC" : "MANUAL";
let selectedAcrId: string | null = null;

Expand All @@ -26,7 +30,7 @@ module.exports = async function() {
}

if (creationMode === "AUTOMATIC") {
if (acrList.length === 1) {
if (Array.isArray(acrList) && acrList.length === 1) {
const acr = acrList[0];
debug(`found one container registry ${chalk.green(acr.name)}`);

Expand All @@ -40,11 +44,11 @@ module.exports = async function() {
// we founf one cluster but it was not created by Hexa, go ahead and automatically create one
return (await require(`./create`))("AUTOMATIC");
}
} else {
} else if (Array.isArray(acrList)) {
// we found many ACR accounts, let the user choose the right one
selectedAcrId = (await chooseAcrAccount(acrList)).registry as (string & CreationMode);
}
} else {
} else if (Array.isArray(acrList)) {
selectedAcrId = (await chooseAcrAccount(acrList)).registry as (string & CreationMode);
}

Expand Down
6 changes: 3 additions & 3 deletions src/features/kubernetes/index.ts
Expand Up @@ -36,11 +36,11 @@ module.exports = async function() {
// fallback to either a MANUAL or AUTOMATIC creation, depending on the global config
let creationMode = process.env.HEXA_AUTO_MODE ? "AUTOMATIC" : "MANUAL";

if (kubeClustersList.length === 0) {
if (Array.isArray(kubeClustersList) && kubeClustersList.length === 0) {
// no cluster found, create one using the selected creation mode
await createK8sClutster(creationMode);
k8s = Config.get("k8s") as AzureKubernetesCluster;
} else if (kubeClustersList.length === 1) {
} else if (Array.isArray(kubeClustersList) && kubeClustersList.length === 1) {
const cluster = kubeClustersList[0];
debug(`found one cluster ${chalk.green(cluster.name)}`);

Expand All @@ -56,7 +56,7 @@ module.exports = async function() {
// note: the user may wanna create a new clustor
k8s.id = (await chooseKubernetesCluster(kubeClustersList)).cluster as (string & CreationMode);
}
} else {
} else if (Array.isArray(kubeClustersList)) {
// we found many clusters, let the user choose the right one
// note: the user may wanna create a new clustor
k8s.id = (await chooseKubernetesCluster(kubeClustersList)).cluster as (string & CreationMode);
Expand Down
4 changes: 3 additions & 1 deletion src/features/resource-group/create.ts
Expand Up @@ -26,7 +26,9 @@ module.exports = async function(creationMode: CreationMode) {
isProjectExists = await az(`group exists --name ${name}`, `Checking for existing project ${chalk.cyan(name)}...`);
}

if (isProjectExists.message.includes("true")) {
debug(`isProjectExists=${chalk.green(JSON.stringify(isProjectExists))}`);

if (isProjectExists?.message.includes("true")) {
// https://docs.microsoft.com/en-us/cli/azure/group?view=azure-cli-latest#az-group-show
project = await az<AzureResourceGroup>(`group show -n workspacetest --query "{name:name, id:id, location:location}"`, `Bootstrapping project ${chalk.cyan(name)}...`);
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/features/storage/index.ts
Expand Up @@ -23,20 +23,22 @@ module.exports = async function() {
`Checking storage for project ${chalk.cyan(resourceGroup.name)}...`
);

debug(`storageAccountsList=${chalk.green(JSON.stringify(storageAccountsList))}`);

// In case we dont find any storage account that had been created by Hexa,
// fallback to either a MANUAL or AUTOMATIC creation, depending on the global config
let creationMode = process.env.HEXA_AUTO_MODE ? "AUTOMATIC" : "MANUAL";

if (storageAccountsList.length === 0) {
if (Array.isArray(storageAccountsList) && storageAccountsList.length === 0) {
// no storage account found, create one using the selected creation mode
await storageCreation(creationMode);
storage = Config.get("storage") as AzureStorage;
} else if (storageAccountsList.length === 1) {
} else if (Array.isArray(storageAccountsList) && storageAccountsList.length === 1) {
const storageAccount = storageAccountsList[0];
debug(`found one storage account ${chalk.green(storageAccount.name)}`);

// has the account been created with Hexa?
if (creationMode === "AUTOMATIC" && storageAccount && storageAccount.tags && storageAccount.tags["x-created-by"] === "hexa") {
if (creationMode === "AUTOMATIC" && storageAccount?.tags?.["x-created-by"] === "hexa") {
debug(`using storage account ${chalk.green(storageAccount.name)}`);

// use this storage account
Expand All @@ -47,7 +49,7 @@ module.exports = async function() {
// note: the user may wanna create a new storage account
storage.id = (await chooseAccountStorage(storageAccountsList)).storage as (string & CreationMode);
}
} else {
} else if(Array.isArray(storageAccountsList)) {
// we found many storage accounts, let the user choose the right one
// note: the user may wanna create a new storage account
storage.id = (await chooseAccountStorage(storageAccountsList)).storage as (string & CreationMode);
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Expand Up @@ -50,14 +50,19 @@ let debug: Function;
.option("-s, --sas", "use SAS token (only: storage and database)", false)
.option("-t, --token", "generate a Storage token into a .env file", false)
.option("-u, --use <builder>", "use a specific build system (e.g. tsc,bazel)", "tsc")
.option("-v, --verbose", "enable verbose mode", false)
.option("-y, --yes", "answer yes to all confirmations", false)
.option("--dry-run", "do not execute real commands.", false)
.option("--yolo", "enable all modes and all services", false)
.parse(process.argv);

// set confiuration
// WARNING: order matters

if (program.debug) {
process.env.DEBUG = "hexa";
}
if (program.verbose) {
process.env.DEBUG = "*";
}

Expand Down Expand Up @@ -94,6 +99,9 @@ let debug: Function;
if (program.use === "bazel") {
process.env.HEXA_USE_BAZEL = "1";
}
if (program.dryRun) {
process.env.HEXA_DRY_RUN = "1";
}

// use process.argv not program.argv
const commandName = process.argv[2];
Expand Down

0 comments on commit d54c652

Please sign in to comment.