Skip to content

Commit

Permalink
fix: v2 templte support report
Browse files Browse the repository at this point in the history
Signed-off-by: xsahxl <xsahxl@126.com>
  • Loading branch information
xsahxl authored and yuankun.yk committed Dec 1, 2023
1 parent b00737c commit b47830a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
13 changes: 3 additions & 10 deletions src/command/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from 'commander';
import chalk from 'chalk';
import { emoji, isJson, writeOutput } from '@/utils';
import { emoji, getUid, isJson, writeOutput } from '@/utils';
import v1 from './v1';
import * as utils from '@serverless-devs/utils';
import loadComponent from '@serverless-devs/load-component';
Expand Down Expand Up @@ -92,14 +92,7 @@ const doAction = async () => {
};
if (instance[command]) {
const argv = process.argv.slice(2);
const getUid = async () => {
// 尝试获取uid
try {
const res = await new Credential({ logger: componentLogger }).get(access);
return get(res, 'credential.AccountID');
} catch (error) { }
}
const reportData = { uid: await getUid(), argv, command, component: componentName, userAgent: getUserAgent({ component: componentName }) };
const reportData = { uid: await getUid(access), argv, command, component: componentName, userAgent: getUserAgent({ component: componentName }) };
try {
const res = await instance[command](inputs);
const showOutput = () => {
Expand All @@ -116,7 +109,7 @@ const doAction = async () => {
handleError(new utils.DevsError(error.message, {
stack: error.stack,
exitCode: 101,
trackerType: 'runtimeException'
trackerType: utils.ETrackerType.runtimeException,
}), reportData);
return;
}
Expand Down
22 changes: 15 additions & 7 deletions src/command/cli/v1.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { Command } from 'commander';
import chalk from 'chalk';
import * as core from '@serverless-devs/core';
import * as utils from '@serverless-devs/utils';
import { emoji, writeOutput } from '@/utils';
import { emoji, getUid, writeOutput } from '@/utils';
import { includes, isEmpty, isPlainObject, isString } from 'lodash';
import logger from '@/logger';
import path from 'path';
import execDaemon from '@/exec-daemon';
import { EReportType } from '@/type';
import { DevsError, ETrackerType, getUserAgent, parseArgv } from '@serverless-devs/utils';
import handleError from '@/error';

const v1 = (program: Command) => {
program.action(async options => {
await doAction(options);
});

const doAction = async options => {
const argvData = core.getGlobalArgs(process.argv.slice(2));
const argv = process.argv.slice(2)
const argvData = core.getGlobalArgs(argv);
const { _: rawData, access = 'default', help, silent } = argvData;
// s cli
if (rawData.length === 1 || (rawData.length === 1 && help)) {
Expand Down Expand Up @@ -59,28 +63,32 @@ const v1 = (program: Command) => {
configPath: process.cwd(),
},
};
const reportData = { uid: await getUid(access), argv, command: _method, component: componentName, userAgent: getUserAgent({ component: componentName }) };
try {
const res = await instance[_method](inputs);
if (isEmpty(res)) {
return logger.write(chalk.green(`End of method: ${_method}`));
}
const showOutput = () => {
const argv = utils.parseArgv(process.argv.slice(2));
const argv = parseArgv(process.argv.slice(2));
if (argv['output-file']) return;
logger.unsilent();
isString(res) ? (silent ? logger.write(res) : logger.write(chalk.green(res))) : logger.output(res);
if (silent) logger.silent();
};
showOutput();
writeOutput(res);
execDaemon('report.js', { ...reportData, type: EReportType.command });

} catch (error) {
throw new utils.DevsError(error.message, {
handleError(new DevsError(error.message, {
stack: error.stack,
exitCode: 101,
});
trackerType: ETrackerType.runtimeException,
}), reportData);
return;
}
}

// s cli fc -h
if (rawData.length === 2) {
if (instance['index']) {
Expand Down
67 changes: 46 additions & 21 deletions src/command/custom/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { Command } from 'commander';
import { loadComponent, makeUnderLine, publishHelp } from '@serverless-devs/core';
import path from 'path';
import { getYamlContent } from '@serverless-devs/utils';
import { get, isPlainObject, values, first, isEmpty, find, each } from 'lodash';
import { DevsError, ETrackerType, getUserAgent, getYamlContent } from '@serverless-devs/utils';
import { get, isPlainObject, values, first, isEmpty, find, each, join } from 'lodash';
import chalk from 'chalk';
import { emoji, writeOutput } from '@/utils';
import { emoji, getUid, writeOutput } from '@/utils';
import { parseArgv } from '@serverless-devs/utils';
import * as core from '@serverless-devs/core';
import { ISpec } from '../types';
import { EReportType } from '@/type';
import execDaemon from '@/exec-daemon';
import handleError from '@/error';

class V1 {
private customProgram: Command;
constructor(
private program: Command,
private spec = {} as ISpec,
) {}
) { }
async init() {
const argv = process.argv.slice(2);
const { _: raw, help } = parseArgv(argv);
Expand All @@ -38,23 +41,45 @@ class V1 {
async doExecCommand() {
const argv = process.argv.slice(2);
const { template, help, access, debug, output, ...rest } = parseArgv(argv);
const res = await core.execCommand({
syaml: template,
serverName: this.spec.projectName,
method: this.spec.command,
args: process.argv.slice(2),
globalArgs: {
access,
skipActions: rest['skip-actions'],
debug,
help,
output,
},
env: {
serverless_devs_temp_argv: JSON.stringify(process.argv.slice(2)),
},
});
writeOutput(res);
const reportComponent = await this.getReportComponent();
const reportData = { uid: await getUid(access), argv, command: this.spec.command, component: reportComponent, userAgent: getUserAgent({ component: reportComponent }) };
try {
const res = await core.execCommand({
syaml: template,
serverName: this.spec.projectName,
method: this.spec.command,
args: process.argv.slice(2),
globalArgs: {
access,
skipActions: rest['skip-actions'],
debug,
help,
output,
},
env: {
serverless_devs_temp_argv: JSON.stringify(process.argv.slice(2)),
},
});
writeOutput(res);
execDaemon('report.js', { ...reportData, type: EReportType.command });
} catch (error) {
handleError(new DevsError(error.message, {
stack: error.stack,
trackerType: ETrackerType.runtimeException,
}), reportData)
}
}

async getReportComponent() {
const reportComponentList = [];
const components = get(this.spec, 'components');
for (const name of components) {
const instance = await loadComponent(name);
const publishData = getYamlContent(path.join(instance.__path, 'publish.yaml'));
const version = get(publishData, 'Version');
reportComponentList.push(version ? `${get(publishData, 'Name')}@${version}` : name);
}
return join(reportComponentList, ',');
}
// s -h
async showSimpleHelp() {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ export const runEnvComponent = async (args: IEnvArgs, access: any) => {
});
}
};

// 尝试获取uid
export const getUid = async (access: string) => {
try {
const res = await new Credential({ logger }).get(access);
return get(res, 'credential.AccountID');
} catch (error) { };
};

0 comments on commit b47830a

Please sign in to comment.