Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 31 additions & 18 deletions src/commands/variable-group/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import { logger } from "../../logger";
import { VariableGroupData } from "../../types";
import decorator from "./create.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";

interface CommandOptions {
file: string | undefined;
Expand All @@ -31,16 +33,20 @@ interface CommandOptions {

export const validateValues = (opts: CommandOptions): void => {
if (!opts.file) {
throw Error("You need to specify a file with variable group manifest");
throw buildError(
errorStatusCode.VALIDATION_ERR,
"variable-group-create-cmd-err-file-missing"
);
}
const config = Config();
const azure = config.azure_devops;

if (!hasValue(opts.orgName) && !azure?.org) {
throw Error(
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "variable-group-create-cmd-err-org-missing",
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`value for ${getCmdOption(decorator, "org-name")!.arg} is missing`
);
values: [getCmdOption(decorator, "org-name")!.arg],
});
}

if (hasValue(opts.orgName)) {
Expand All @@ -49,10 +55,11 @@ export const validateValues = (opts: CommandOptions): void => {
}

if (!hasValue(opts.devopsProject) && !azure?.project) {
throw Error(
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "variable-group-create-cmd-err-project-missing",
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`value for ${getCmdOption(decorator, "devops-project")!.arg} is missing`
);
values: [getCmdOption(decorator, "devops-project")!.arg],
});
}

if (hasValue(opts.devopsProject)) {
Expand All @@ -61,12 +68,11 @@ export const validateValues = (opts: CommandOptions): void => {
}

if (!hasValue(opts.personalAccessToken) && !azure?.access_token) {
throw Error(
`value for ${
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
getCmdOption(decorator, "personal-access-token")!.arg
} is missing`
);
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "variable-group-create-cmd-err-access-token",
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
values: [getCmdOption(decorator, "personal-access-token")!.arg],
});
}
};

Expand Down Expand Up @@ -95,8 +101,13 @@ export const commandDecorator = (command: commander.Command): void => {
);
await exitCmd(logger, process.exit, 0);
} catch (err) {
logger.error(`Error occurred while creating variable group`);
logger.error(err);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
logError(
buildError(
errorStatusCode.CMD_EXE_ERR,
"variable-group-create-cmd-failed"
)
);
await exitCmd(logger, process.exit, 1);
}
});
Expand Down Expand Up @@ -126,8 +137,10 @@ export const create = async (
} else if (data.type === "Vsts") {
await addVariableGroup(data, accessOpts);
} else {
throw new Error(
`Variable Group type "${data.type}" is not supported. Only "Vsts" and "AzureKeyVault" are valid types and case sensitive.`
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw buildError(errorStatusCode.EXE_FLOW_ERR, {
errorKey: "variable-group-create-cmd-err-create",
values: [data.type],
});
}
};
7 changes: 7 additions & 0 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@
"ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.",
"ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}",

"variable-group-create-cmd-failed": "Error occurred while creating variable group.",
"variable-group-create-cmd-err-create": "Could not load variable group. The variable group type '{0}' is not supported. Only 'Vsts' and 'AzureKeyVault' are valid types and case sensitive.",
"variable-group-create-cmd-err-access-token": "Provide a value for {0}.",
"variable-group-create-cmd-err-project-missing": "Provide a value for {0}.",
"variable-group-create-cmd-err-org-missing": "Provide a value for {0}.",
"variable-group-create-cmd-err-file-missing": "Provide a file with the variable group manifest.",

"validation-err-org-name-missing": "Organization name was missing. Provide it.",
"validation-err-org-name": "Organization name must start with a letter or number, followed by letters, numbers or hyphens, and must end with a letter or number.",
"validation-err-password-missing": "Password was missing. Provide it.",
Expand Down