Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/m365/cli/commands/app/app-reconsent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { Logger } from '../../../../cli/Logger.js';
Expand All @@ -12,6 +14,10 @@ import { browserUtil } from '../../../../utils/browserUtil.js';
import { entraApp } from '../../../../utils/entraApp.js';
import { entraServicePrincipal } from '../../../../utils/entraServicePrincipal.js';

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliAppReconsentCommand extends GraphCommand {
public get name(): string {
return commands.APP_RECONSENT;
Expand All @@ -21,6 +27,10 @@ class CliAppReconsentCommand extends GraphCommand {
return 'Reconsent all permission scopes used in CLI for Microsoft 365';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
try {
const appId = auth.connection.appId!;
Expand Down
10 changes: 10 additions & 0 deletions src/m365/cli/commands/completion/completion-clink-update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { z } from 'zod';
import { autocomplete } from '../../../../autocomplete.js';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import AnonymousCommand from '../../../base/AnonymousCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliCompletionClinkUpdateCommand extends AnonymousCommand {
public get name(): string {
return commands.COMPLETION_CLINK_UPDATE;
Expand All @@ -12,6 +18,10 @@ class CliCompletionClinkUpdateCommand extends AnonymousCommand {
return 'Updates command completion for Clink (cmder)';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
await logger.log(autocomplete.getClinkCompletion());
}
Expand Down
28 changes: 10 additions & 18 deletions src/m365/cli/commands/completion/completion-pwsh-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import url from 'url';
import { z } from 'zod';
import { autocomplete } from '../../../../autocomplete.js';
import { Logger } from '../../../../cli/Logger.js';
import {
CommandError
CommandError, globalOptionsZod
} from '../../../../Command.js';
import GlobalOptions from '../../../../GlobalOptions.js';
import AnonymousCommand from '../../../base/AnonymousCommand.js';
import commands from '../../commands.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export const options = z.strictObject({
...globalOptionsZod.shape,
profile: z.string().alias('p')
});
type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
profile: string;
}

class CliCompletionPwshSetupCommand extends AnonymousCommand {
public get name(): string {
return commands.COMPLETION_PWSH_SETUP;
Expand All @@ -30,18 +32,8 @@ class CliCompletionPwshSetupCommand extends AnonymousCommand {
return 'Sets up command completion for PowerShell';
}

constructor() {
super();

this.#initOptions();
}

#initOptions(): void {
this.options.unshift(
{
option: '-p, --profile <profile>'
}
);
public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
Expand Down
10 changes: 10 additions & 0 deletions src/m365/cli/commands/completion/completion-pwsh-update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { z } from 'zod';
import { autocomplete } from '../../../../autocomplete.js';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import AnonymousCommand from '../../../base/AnonymousCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliCompletionPwshUpdateCommand extends AnonymousCommand {
public get name(): string {
return commands.COMPLETION_PWSH_UPDATE;
Expand All @@ -12,6 +18,10 @@ class CliCompletionPwshUpdateCommand extends AnonymousCommand {
return 'Updates command completion for PowerShell';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
if (this.debug) {
await logger.logToStderr('Generating command completion...');
Expand Down
10 changes: 10 additions & 0 deletions src/m365/cli/commands/completion/completion-sh-setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { z } from 'zod';
import { autocomplete } from '../../../../autocomplete.js';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import AnonymousCommand from '../../../base/AnonymousCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliCompletionShSetupCommand extends AnonymousCommand {
public get name(): string {
return commands.COMPLETION_SH_SETUP;
Expand All @@ -12,6 +18,10 @@ class CliCompletionShSetupCommand extends AnonymousCommand {
return 'Sets up command completion for Zsh, Bash and Fish';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
if (this.debug) {
await logger.logToStderr('Generating command completion...');
Expand Down
10 changes: 10 additions & 0 deletions src/m365/cli/commands/completion/completion-sh-update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { z } from 'zod';
import { autocomplete } from '../../../../autocomplete.js';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import AnonymousCommand from '../../../base/AnonymousCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliCompletionShUpdateCommand extends AnonymousCommand {
public get name(): string {
return commands.COMPLETION_SH_UPDATE;
Expand All @@ -12,6 +18,10 @@ class CliCompletionShUpdateCommand extends AnonymousCommand {
return 'Updates command completion for Zsh, Bash and Fish';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
if (this.debug) {
await logger.logToStderr('Generating command completion...');
Expand Down
27 changes: 9 additions & 18 deletions src/m365/cli/commands/config/config-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './config-get.js';
import command, { options } from './config-get.js';

describe(commands.CONFIG_GET, () => {
let log: any[];
let logger: Logger;
let loggerSpy: sinon.SinonSpy;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;

before(() => {
commandInfo = cli.getCommandInfo(command);
commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options;
sinon.stub(telemetry, 'trackEvent').resolves();
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
Expand Down Expand Up @@ -70,24 +72,13 @@ describe(commands.CONFIG_GET, () => {
assert(loggerSpy.calledWith(undefined));
});

it('supports specifying key', () => {
const options = command.options;
let containsOptionKey = false;
options.forEach(o => {
if (o.option.indexOf('--key') > -1) {
containsOptionKey = true;
}
});
assert(containsOptionKey);
});

it('fails validation if specified key is invalid ', async () => {
const actual = await command.validate({ options: { key: 'invalid' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if specified key is invalid ', () => {
const actual = commandOptionsSchema.safeParse({ key: 'invalid' });
assert.strictEqual(actual.success, false);
});

it(`passes validation if setting is set to ${settingsNames.showHelpOnFailure}`, async () => {
const actual = await command.validate({ options: { key: settingsNames.showHelpOnFailure } }, commandInfo);
assert.strictEqual(actual, true);
it(`passes validation if setting is set to ${settingsNames.showHelpOnFailure}`, () => {
const actual = commandOptionsSchema.safeParse({ key: settingsNames.showHelpOnFailure });
assert.strictEqual(actual.success, true);
});
});
54 changes: 12 additions & 42 deletions src/m365/cli/commands/config/config-get.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { z } from 'zod';
import { cli } from "../../../../cli/cli.js";
import { Logger } from "../../../../cli/Logger.js";
import GlobalOptions from "../../../../GlobalOptions.js";
import { globalOptionsZod } from "../../../../Command.js";
import { settingsNames } from "../../../../settingsNames.js";
import AnonymousCommand from "../../../base/AnonymousCommand.js";
import commands from "../../commands.js";

const settingNameValues = Object.getOwnPropertyNames(settingsNames) as [string, ...string[]];

export const options = z.strictObject({
...globalOptionsZod.shape,
key: z.enum(settingNameValues).alias('k')
});
type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
key: string;
}

class CliConfigGetCommand extends AnonymousCommand {
private static readonly optionNames: string[] = Object.getOwnPropertyNames(settingsNames);

public get name(): string {
return commands.CONFIG_GET;
}
Expand All @@ -24,41 +27,8 @@ class CliConfigGetCommand extends AnonymousCommand {
return 'Gets value of a CLI for Microsoft 365 configuration option';
}

constructor() {
super();

this.#initTelemetry();
this.#initOptions();
this.#initValidators();
}

#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
key: args.options.key
});
});
}

#initOptions(): void {
this.options.unshift(
{
option: '-k, --key <key>',
autocomplete: CliConfigGetCommand.optionNames
}
);
}

#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => {
if (CliConfigGetCommand.optionNames.indexOf(args.options.key) < 0) {
return `${args.options.key} is not a valid setting. Allowed values: ${CliConfigGetCommand.optionNames.join(', ')}`;
}

return true;
}
);
public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
Expand Down
10 changes: 10 additions & 0 deletions src/m365/cli/commands/config/config-list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { z } from 'zod';
import { cli } from "../../../../cli/cli.js";
import { Logger } from "../../../../cli/Logger.js";
import { globalOptionsZod } from "../../../../Command.js";
import AnonymousCommand from "../../../base/AnonymousCommand.js";
import commands from "../../commands.js";

export const options = z.strictObject({
...globalOptionsZod.shape
});

class CliConfigListCommand extends AnonymousCommand {
public get name(): string {
return commands.CONFIG_LIST;
Expand All @@ -12,6 +18,10 @@ class CliConfigListCommand extends AnonymousCommand {
return 'List all self set CLI for Microsoft 365 configurations';
}

public get schema(): z.ZodType {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
await logger.log(cli.getConfig().all);
}
Expand Down
28 changes: 9 additions & 19 deletions src/m365/cli/commands/config/config-reset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { telemetry } from '../../../../telemetry.js';
import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import commands from '../../commands.js';
import command from './config-reset.js';
import command, { options } from './config-reset.js';

describe(commands.CONFIG_RESET, () => {
let log: any[];
let logger: Logger;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;

before(() => {
commandInfo = cli.getCommandInfo(command);
commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options;
sinon.stub(telemetry, 'trackEvent').resolves();
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
Expand Down Expand Up @@ -100,25 +102,13 @@ describe(commands.CONFIG_RESET, () => {
assert.strictEqual(showHelpOnFailureValue, undefined, 'Invalid value');
});

it('fails validation if specified key is invalid', async () => {
const actual = await command.validate({ options: { key: 'invalid' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if specified key is invalid', () => {
const actual = commandOptionsSchema.safeParse({ key: 'invalid' });
assert.strictEqual(actual.success, false);
});

it('passes validation if key is not specified', async () => {
const actual = await command.validate({ options: {} }, commandInfo);
assert.strictEqual(actual, true);
});

it('supports specifying key', () => {
const options = command.options;
let containsOptionKey = false;
options.forEach(o => {
if (o.option.indexOf('--key') > -1) {
containsOptionKey = true;
}
});

assert(containsOptionKey);
it('passes validation if key is not specified', () => {
const actual = commandOptionsSchema.safeParse({});
assert.strictEqual(actual.success, true);
});
});
Loading
Loading