Skip to content

Commit

Permalink
feat: prevent use of clientId & secret and patToken together
Browse files Browse the repository at this point in the history
  • Loading branch information
mjacholke committed Jul 8, 2024
1 parent a8236f6 commit d0d0db6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 52 deletions.
120 changes: 71 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions src/commands/configure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Arguments, Argv } from 'yargs';
import yargs, { Arguments, Argv } from 'yargs';
import { readConfig } from '../cli';
import { CommandOptions } from '../interfaces/command-options.interface';
import fs from 'fs';
Expand Down Expand Up @@ -41,6 +41,14 @@ export const builder = (yargs: Argv): void => {
});
};

const getCommandLineArgs = (): Arguments => {
const rawArgs = process.argv.slice(2);
return yargs(rawArgs)
.option('clientId', { type: 'string' })
.option('clientSecret', { type: 'string' })
.option('patToken', { type: 'string' }).argv;
};

export type ConfigurationParameters = {
clientId?: string;
clientSecret?: string;
Expand All @@ -52,6 +60,16 @@ export type ConfigurationParameters = {
dstHubId?: string;
};

export type StoredConfigurationParameters = {
clientId?: string;
clientSecret?: string;
patToken?: string;
hubId?: string;
dstClientId?: string;
dstSecret?: string;
dstHubId?: string;
};

type ConfigArgument = {
config: string;
};
Expand Down Expand Up @@ -95,10 +113,21 @@ export const readConfigFile = (configFile: string, ignoreError?: boolean): objec

export const handler = (argv: Arguments<ConfigurationParameters & ConfigArgument>): void => {
const { clientId, clientSecret, hubId, patToken } = argv;
const storedConfig = readConfigFile(argv.config);

const storedConfig: StoredConfigurationParameters = readConfigFile(argv.config);
const newConfig: ConfigurationParameters = { clientId, clientSecret, hubId, patToken };
const commandLineArgs = getCommandLineArgs();

if ((commandLineArgs.clientId || commandLineArgs.clientSecret) && commandLineArgs.patToken) {
console.error('You cannot specify both clientId and clientSecret together with patToken.');
return;
}
if (commandLineArgs.patToken && (storedConfig.clientId || storedConfig.clientSecret)) {
delete newConfig.clientId;
delete newConfig.clientSecret;
}
if ((commandLineArgs.clientId || commandLineArgs.clientSecret) && storedConfig.patToken) {
delete newConfig.patToken;
}
if (argv.dstClientId) newConfig.dstClientId = argv.dstClientId;
if (argv.dstSecret) newConfig.dstSecret = argv.dstSecret;
if (argv.dstHubId) newConfig.dstHubId = argv.dstHubId;
Expand Down

0 comments on commit d0d0db6

Please sign in to comment.