Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

[FEATURE] Error code and exception chain for hld init command #478

Merged
merged 6 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/commands/hld/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { checkoutCommitPushCreatePRLink } from "../../lib/gitutils";
import { hasValue } from "../../lib/validator";
import { logger } from "../../logger";
import decorator from "./init.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";

// values that we need to pull out from command operator
interface CommandOptions {
Expand Down Expand Up @@ -57,7 +59,10 @@ export const execute = async (
): Promise<void> => {
try {
if (!hasValue(hldRepoPath)) {
throw new Error("project path is not provided");
throw buildError(
errorStatusCode.VALIDATION_ERR,
"hld-init-cmd-project-path-missing"
);
}
await initialize(
hldRepoPath,
Expand All @@ -68,10 +73,9 @@ export const execute = async (
);
await exitFn(0);
} catch (err) {
logger.error(
`Error occurred while initializing hld repository ${hldRepoPath}`
logError(
buildError(errorStatusCode.CMD_EXE_ERR, "hld-init-cmd-failed", err)
);
logger.error(err);
await exitFn(1);
}
};
Expand Down
140 changes: 82 additions & 58 deletions src/lib/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,37 +623,44 @@ const manifestGenerationPipelineYaml = (): string => {
export const generateHldAzurePipelinesYaml = (
targetDirectory: string
): void => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating hld manifest-generation in ${absTargetPath}`);

const azurePipelinesYamlPath = path.join(
absTargetPath,
RENDER_HLD_PIPELINE_FILENAME
);
try {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating hld manifest-generation in ${absTargetPath}`);

if (fs.existsSync(azurePipelinesYamlPath)) {
logger.warn(
`Existing ${RENDER_HLD_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
const azurePipelinesYamlPath = path.join(
absTargetPath,
RENDER_HLD_PIPELINE_FILENAME
);

return;
}
const hldYaml = manifestGenerationPipelineYaml();
logger.info(
`Writing ${RENDER_HLD_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
);
if (fs.existsSync(azurePipelinesYamlPath)) {
logger.warn(
`Existing ${RENDER_HLD_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
);
return;
}
const hldYaml = manifestGenerationPipelineYaml();
logger.info(
`Writing ${RENDER_HLD_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
);

const requiredPipelineVariables = [
`'MANIFEST_REPO' (Repository for your kubernetes manifests in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/materialized')`,
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
].join(", ");
const requiredPipelineVariables = [
`'MANIFEST_REPO' (Repository for your kubernetes manifests in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/materialized')`,
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
].join(", ");

logger.info(
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
);
logger.info(
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
);

writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"fileutils-generate-hld-pipeline-yaml",
err
);
}
};

/**
Expand Down Expand Up @@ -688,34 +695,41 @@ export const generateDefaultHldComponentYaml = (
componentName: string,
componentPath: string
): void => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating component.yaml in ${absTargetPath}`);

const fabrikateComponentPath = path.join(absTargetPath, "component.yaml");
try {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating component.yaml in ${absTargetPath}`);

const fabrikateComponentPath = path.join(absTargetPath, "component.yaml");

if (fs.existsSync(fabrikateComponentPath)) {
logger.warn(
`Existing component.yaml found at ${fabrikateComponentPath}, skipping generation.`
);
return;
}

const componentYaml = defaultComponentYaml(
componentGit,
componentName,
componentPath
);

if (fs.existsSync(fabrikateComponentPath)) {
logger.warn(
`Existing component.yaml found at ${fabrikateComponentPath}, skipping generation.`
logger.info(
`Writing ${HLD_COMPONENT_FILENAME} file to ${fabrikateComponentPath}`
);

return;
fs.writeFileSync(
fabrikateComponentPath,
yaml.safeDump(componentYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"fileutils-generate-default-hld-component-yaml",
err
);
}

const componentYaml = defaultComponentYaml(
componentGit,
componentName,
componentPath
);

logger.info(
`Writing ${HLD_COMPONENT_FILENAME} file to ${fabrikateComponentPath}`
);

fs.writeFileSync(
fabrikateComponentPath,
yaml.safeDump(componentYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
};

const hldLifecyclePipelineYaml = (): string => {
Expand Down Expand Up @@ -896,18 +910,28 @@ export const generateGitIgnoreFile = (
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating starter .gitignore in ${absTargetPath}`);

const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");
try {
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");

if (fs.existsSync(gitIgnoreFilePath)) {
logger.warn(
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
);
if (fs.existsSync(gitIgnoreFilePath)) {
logger.warn(
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
);
return;
}

return;
logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
{
errorKey: "fileutils-generate-git-ignore-file",
values: [absTargetPath],
},
err
);
}

logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
};

/**
Expand Down
9 changes: 7 additions & 2 deletions src/lib/gitutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from "path";
import url from "url";
import { logger } from "../logger";
import { exec } from "./shell";
import { build as buildError } from "./errorBuilder";
import { errorStatusCode } from "./errorStatusCode";

/**
* For git urls that you may want to log only!
Expand Down Expand Up @@ -329,8 +331,11 @@ export const checkoutCommitPushCreatePRLink = async (
);
});
} catch (err) {
logger.error(err);
throw err;
throw buildError(
errorStatusCode.GIT_OPS_ERR,
"git-checkout-commit-push-create-PR-link",
err
);
}
};

Expand Down
10 changes: 9 additions & 1 deletion src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"storageKeVaultName": "Enter key vault name (have the value as empty and hit enter key to skip)"
},
"errors": {
"hld-init-cmd-failed": "Hld init command was not successfully executed.",
"hld-init-cmd-project-path-missing": "Value for project path was not provided. Provide it.",

"infra-scaffold-cmd-failed": "Infra scaffold Command was not successfully executed.",
"infra-scaffold-cmd-src-missing": "Value for source is required because it cannot be constructed with properties in spk-config.yaml. Provide value for source.",
"infra-scaffold-cmd-values-missing": "Values for name, version and/or 'template were missing. Provide value for values for them.",
Expand All @@ -37,6 +40,11 @@
"hld-append-var-group-cmd-failed": "HLD Append Variable Group Command was not successfully executed.",
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",

"fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format."
"fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format.",
"fileutils-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.",
"fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",
"fileutils-generate-git-ignore-file": "Could not generate .gitignore file in {0}.",

"git-checkout-commit-push-create-PR-link": "Could not checkout, commit, push or create pull request"
}
}