Skip to content

Commit

Permalink
showcase class
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Jan 17, 2025
1 parent 3eebc41 commit 362d546
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
12 changes: 7 additions & 5 deletions commands/account/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { getAccessToken } from '@hubspot/local-dev-lib/personalAccessKey';
import { addConfigOptions } from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import { getTableContents } from '../../lib/ui/table';
import { CommonArgs } from '../../types/Yargs';
import { CommonArgs, ConfigOptions } from '../../types/Yargs';

const i18nKey = 'commands.account.subcommands.info';
export const describe = i18n(`${i18nKey}.describe`);

export const describe = i18n(`${i18nKey}.describe`);
export const command = 'info [account]';

type AccountInfoArgs = CommonArgs & ConfigOptions;

export async function handler(
args: ArgumentsCamelCase<CommonArgs>
args: ArgumentsCamelCase<AccountInfoArgs>
): Promise<void> {
const { derivedAccountId } = args;
const config = getAccountConfig(derivedAccountId);
Expand Down Expand Up @@ -43,7 +45,7 @@ export async function handler(
}
}

export function builder(yargs: Argv): Argv<CommonArgs> {
export function builder(yargs: Argv): Argv<AccountInfoArgs> {
addConfigOptions(yargs);

yargs.example([
Expand All @@ -52,5 +54,5 @@ export function builder(yargs: Argv): Argv<CommonArgs> {
['$0 accounts info 1234567', i18n(`${i18nKey}.examples.idBased`)],
]);

return yargs as Argv<CommonArgs>;
return yargs as Argv<AccountInfoArgs>;
}
63 changes: 63 additions & 0 deletions commands/account/infoClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Argv, ArgumentsCamelCase, CommandModule } from 'yargs';
import { logger } from '@hubspot/local-dev-lib/logger';
import { getAccountConfig } from '@hubspot/local-dev-lib/config';
import { getAccessToken } from '@hubspot/local-dev-lib/personalAccessKey';
import { addConfigOptions } from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import { getTableContents } from '../../lib/ui/table';
import { CommonArgs, ConfigOptions } from '../../types/Yargs';

const i18nKey = 'commands.account.subcommands.info';

type AccountInfoArgs = CommonArgs & ConfigOptions;

class AccountInfo implements CommandModule<CommonArgs, AccountInfoArgs> {
public command = 'info [account]';
public describe = i18n(`${i18nKey}.describe`);

public builder(yargs: Argv): Argv<AccountInfoArgs> {
addConfigOptions(yargs);

yargs.example([
['$0 accounts info', i18n(`${i18nKey}.examples.default`)],
['$0 accounts info MyAccount', i18n(`${i18nKey}.examples.nameBased`)],
['$0 accounts info 1234567', i18n(`${i18nKey}.examples.idBased`)],
]);

return yargs as Argv<AccountInfoArgs>;
}

public async handler(
args: ArgumentsCamelCase<AccountInfoArgs>
): Promise<void> {
const { derivedAccountId } = args;
const config = getAccountConfig(derivedAccountId);
// check if the provided account is using a personal access key, if not, show an error
if (config && config.authType === 'personalaccesskey') {
const { name, personalAccessKey, env } = config;
let scopeGroups: string[][] = [];

if (personalAccessKey) {
const response = await getAccessToken(
personalAccessKey,
env,
derivedAccountId
);

scopeGroups = response.scopeGroups.map(s => [s]);
}

if (name) {
logger.log(i18n(`${i18nKey}.name`, { name }));
}
logger.log(i18n(`${i18nKey}.accountId`, { accountId: derivedAccountId }));
logger.log(i18n(`${i18nKey}.scopeGroups`));
logger.log(getTableContents(scopeGroups, { border: { bodyLeft: ' ' } }));
} else {
logger.log(i18n(`${i18nKey}.errors.notUsingPersonalAccessKey`));
}
}
}

export default AccountInfo;
module.exports = AccountInfo;
7 changes: 4 additions & 3 deletions lib/commonOpts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Argv, Arguments } from 'yargs';
import {
LOG_LEVEL,
setLogLevel as setLoggerLogLevel,
Expand All @@ -13,7 +14,7 @@ import {
getAndLoadConfigIfNeeded,
} from '@hubspot/local-dev-lib/config';
import { i18n } from './lang';
import { Argv, Arguments } from 'yargs';
import { ConfigOptions, StringOptionType } from '../types/Yargs';

const i18nKey = 'lib.commonOpts';

Expand All @@ -36,8 +37,8 @@ export function addAccountOptions(yargs: Argv): Argv {
});
}

export function addConfigOptions(yargs: Argv): Argv<{ config?: string }> {
return yargs.option('config', {
export function addConfigOptions(yargs: Argv): Argv<ConfigOptions> {
return yargs.option<keyof ConfigOptions, StringOptionType>('config', {
alias: 'c',
describe: i18n(`${i18nKey}.options.config.describe`),
type: 'string',
Expand Down
11 changes: 11 additions & 0 deletions types/Yargs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Options } from 'yargs';

export type CommonArgs = {
derivedAccountId: number;
providedAccountId?: number;
d: boolean;
debug: boolean;
};

export type ConfigOptions = {
c?: string;
config?: string;
};

export type StringOptionType = Options & {
type: 'string';
};

0 comments on commit 362d546

Please sign in to comment.