|
| 1 | +import { VariableGroup } from "azure-devops-node-api/interfaces/ReleaseInterfaces"; |
| 2 | +import commander from "commander"; |
| 3 | +import fs from "fs"; |
| 4 | +import { readYaml } from "../../config"; |
| 5 | +import { IAzureDevOpsOpts } from "../../lib/git"; |
| 6 | +import { |
| 7 | + addVariableGroup, |
| 8 | + addVariableGroupWithKeyVaultMap |
| 9 | +} from "../../lib/pipelines/variableGroup"; |
| 10 | +import { logger } from "../../logger"; |
| 11 | +import { IVariableGroupData } from "../../types"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Adds the create command to the variable-group command object |
| 15 | + * |
| 16 | + * @param command Commander command object to decorate |
| 17 | + */ |
| 18 | +export const createCommandDecorator = (command: commander.Command): void => { |
| 19 | + command |
| 20 | + .command("create") |
| 21 | + .alias("c") |
| 22 | + .description("Add a new variable group in Azure DevOps project.") |
| 23 | + .option( |
| 24 | + "-f, --file <variable-group-manifest-file-path>", |
| 25 | + "Path to the yaml file that contains variable group manifest." |
| 26 | + ) |
| 27 | + .option( |
| 28 | + "-o, --org-name <organization-name>", |
| 29 | + "Azure DevOps organization name; falls back to azure_devops.org in spk config" |
| 30 | + ) |
| 31 | + .option( |
| 32 | + "-p, --project <project>", |
| 33 | + "Azure DevOps project name; falls back to azure_devops.project in spk config" |
| 34 | + ) |
| 35 | + .option( |
| 36 | + "-t, --personal-access-token <personal-access-token>", |
| 37 | + "Personal access token associated with the Azure DevOps org; falls back to azure_devops.access_token in spk config" |
| 38 | + ) |
| 39 | + .action(async opts => { |
| 40 | + try { |
| 41 | + if (!opts.file) { |
| 42 | + logger.error( |
| 43 | + "You need to specify a file with variable group manifest" |
| 44 | + ); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const { file, orgName, project, personalAccessToken } = opts; |
| 49 | + |
| 50 | + logger.debug( |
| 51 | + `opts: ${file}, ${orgName}, ${project}, ${personalAccessToken}` |
| 52 | + ); |
| 53 | + |
| 54 | + // type check |
| 55 | + if (typeof orgName !== "undefined" && typeof orgName !== "string") { |
| 56 | + throw Error( |
| 57 | + `--org-name must be of type 'string', ${typeof orgName} specified.` |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + if (typeof project !== "undefined" && typeof project !== "string") { |
| 62 | + throw Error( |
| 63 | + `--project must be of type 'string', ${typeof project} specified.` |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + typeof personalAccessToken !== "undefined" && |
| 69 | + typeof personalAccessToken !== "string" |
| 70 | + ) { |
| 71 | + throw Error( |
| 72 | + `--personal-access-token must be of type 'string', ${typeof personalAccessToken} specified.` |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const accessOpts: IAzureDevOpsOpts = { |
| 77 | + orgName, |
| 78 | + personalAccessToken, |
| 79 | + project |
| 80 | + }; |
| 81 | + logger.debug(`access options: ${JSON.stringify(accessOpts)}`); |
| 82 | + |
| 83 | + await create(opts.file, accessOpts); |
| 84 | + |
| 85 | + logger.info( |
| 86 | + "Successfully added a variable group in Azure DevOps project!" |
| 87 | + ); |
| 88 | + } catch (err) { |
| 89 | + logger.error(`Error occurred while creating variable group`); |
| 90 | + logger.error(err); |
| 91 | + } |
| 92 | + }); |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Loads varible group manifest from a given filename |
| 97 | + * |
| 98 | + * @param filepath file to read manifest |
| 99 | + * @param accessOpts Azure DevOps access options from command options to override spk config |
| 100 | + */ |
| 101 | +export const create = async ( |
| 102 | + filepath: string, |
| 103 | + accessOpts: IAzureDevOpsOpts |
| 104 | +) => { |
| 105 | + logger.info("Creating variale group"); |
| 106 | + try { |
| 107 | + fs.statSync(filepath); |
| 108 | + const data = readYaml<IVariableGroupData>(filepath); |
| 109 | + logger.debug(`Varible Group Yaml data: ${JSON.stringify(data)}`); |
| 110 | + |
| 111 | + // validate variable group type |
| 112 | + |
| 113 | + let variableGroup: VariableGroup; |
| 114 | + |
| 115 | + if (data.type === "AzureKeyVault") { |
| 116 | + variableGroup = await addVariableGroupWithKeyVaultMap(data, accessOpts); |
| 117 | + } else if (data.type === "Vsts") { |
| 118 | + variableGroup = await addVariableGroup(data, accessOpts); |
| 119 | + } else { |
| 120 | + throw new Error( |
| 121 | + `Varible Group type "${data.type}" is not supported. Only "Vsts" and "AzureKeyVault" are valid types and case sensitive.` |
| 122 | + ); |
| 123 | + } |
| 124 | + } catch (err) { |
| 125 | + throw err; |
| 126 | + } |
| 127 | +}; |
0 commit comments