Skip to content

Commit

Permalink
0.2.2 (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: greenkeeper[bot] <23040076+greenkeeper[bot]@users.noreply.github.com>
Co-authored-by: snyk-bot <snyk-bot@snyk.io>
Co-authored-by: Adam Osiecki <osiecki.adam@outlook.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Snyk bot <github+bot@snyk.io>
Co-authored-by: Adam Osiecki <adam.osiecki@rb.com>
  • Loading branch information
6 people committed Feb 22, 2021
1 parent 9fe671b commit d0d7673
Show file tree
Hide file tree
Showing 6 changed files with 1,557 additions and 1,110 deletions.
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ sfdx plugins:link
## Commands

<!-- commands -->
* [`sfdx oa:apex:log:delete [-c] [-a] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-oaapexlogdelete--c--a--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx oa:apex:log:delete [-c] [-a] [-s <array>] [-n <array>] [-m] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-oaapexlogdelete--c--a--s-array--n-array--m--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx oa:data:soql:sel -f <string> [-w <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-oadatasoqlsel--f-string--w-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

## `sfdx oa:apex:log:delete [-c] [-a] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
## `sfdx oa:apex:log:delete [-c] [-a] [-s <array>] [-n <array>] [-m] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

delete ApexLog entries from Your org

```
delete ApexLog entries from Your org
USAGE
$ sfdx oa:apex:log:delete [-c] [-a] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
$ sfdx oa:apex:log:delete [-c] [-a] [-s <array>] [-n <array>] [-m] [-u <string>] [--apiversion <string>] [--json]
[--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
-a, --async do not wait for successful
Expand All @@ -80,6 +82,14 @@ OPTIONS
number of debug logs without
deleting them
-m, --my delete only my logs
-n, --name=name delete only logs created by
specified user names
-s, --status=status delete only logs with specified
statuses
-u, --targetusername=targetusername username or alias for the target
org; overrides default target org
Expand Down Expand Up @@ -117,13 +127,24 @@ EXAMPLES
"jobID": "7501w000002WnWcAAK"
}
}
sfdx oa:apex:log:delete -s "Internal Salesforce.com Error,success"
Query: SELECT Id FROM ApexLog WHERE Status IN ('Internal Salesforce.com Error','success') does not return any
record
sfdx oa:apex:log:delete -m
Query: SELECT Id FROM ApexLog WHERE LogUserId = '0054J000005OInNQAW' does not return any record
oa:apex:log:delete -c -n test1@user.org,test2@user.org
Query: SELECT Id FROM ApexLog WHERE LogUser.Username IN ('test1@user.org','test2@user.org') does not return any
record
```

_See code: [lib\commands\oa\apex\log\delete.js](https://github.com/osieckiAdam/osiecki-sfdx-plugins/blob/v0.2.0/lib\commands\oa\apex\log\delete.js)_
_See code: [src/commands/oa/apex/log/delete.ts](https://github.com/osieckiAdam/osiecki-sfdx-plugins/blob/v0.2.1/src/commands/oa/apex/log/delete.ts)_

## `sfdx oa:data:soql:sel -f <string> [-w <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

generate query for all fields from SObject (SEL * FROM SObject)
generate query string for all fields from SObject (SEL * FROM SObject)

```
USAGE
Expand Down Expand Up @@ -168,5 +189,4 @@ EXAMPLES
SystemModstamp, StartTime, Location FROM ApexLog WHERE Where LogLength > 100
```

_See code: [lib\commands\oa\data\soql\sel.js](https://github.com/osieckiAdam/osiecki-sfdx-plugins/blob/v0.2.0/lib\commands\oa\data\soql\sel.js)_
<!-- commandsstop -->
_See code: [src/commands/oa/data/soql/sel.ts](https://github.com/osieckiAdam/osiecki-sfdx-plugins/blob/v0.2.1/src/commands/oa/data/soql/sel.ts)_
6 changes: 5 additions & 1 deletion messages/delete.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"deleteCommandDescription": "delete ApexLog entries from Your org",
"checkOnlyFlagDescription": "use this parameter to only check number of debug logs without deleting them",
"asyncFlagDescription": "do not wait for successful completion of the job. If ommited will poll for number of processed records until complete",
"statusFlagDescription": "delete only logs with specified statuses",
"nameFlagDescription": "delete only logs created by specified user names",
"myFlagDescription": "delete only my logs",
"recordsLenghtMessage": "Number of ApexLog records to be deleted: %i",
"jobStartedMessage": "Delete job is started. Id of the job: %s",
"jobStartedMessageAsync": "To poll status of the job, run command 'sfdx force:data:bulk:status -i %s -u %s",
"jobFinishedMessage": "\nJob is finished",
"failedRecordsMessage": "Number of failed records: %i",
"successMessage": "All records were deleted sucessfully",
"noLogsMessage": "There are no Apex logs on Your org!"
"query": "Query: ",
"doesNotReturnAnyRecord": "does not return any record"
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
"author": "Adam Osiecki @osieckiAdam",
"bugs": "https://github.com/osieckiAdam/osiecki-sfdx-plugins/issues",
"dependencies": {
"@oclif/command": "^1",
"@oclif/command": "^1.6.1",
"@oclif/config": "^1",
"@oclif/errors": "^1",
"@salesforce/command": "^2",
"@salesforce/core": "^2",
"@salesforce/core": "2.15.0",
"@salesforce/ts-sinon": "^1.3.0",
"@types/copy-paste": "^1.1.30",
"@types/jsforce": "^1.9.12",
"@types/jsforce": "^1.9.26",
"clipboardy": "^2.1.0",
"copy-paste": "^1.3.0",
"tslib": "^1"
"tslib": "^1.13.0"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
Expand All @@ -31,7 +32,8 @@
"rimraf": "^3.0.0",
"testdouble": "^3.12.4",
"ts-node": "^8.6.2",
"tslint": "^5"
"tslint": "^5",
"typescript": "^4.0.0"
},
"engines": {
"node": ">=8.0.0"
Expand Down
73 changes: 61 additions & 12 deletions src/commands/oa/apex/log/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,36 @@ sfdx oa:apex:log:delete --json'
"numberOfQueriedLogs": 3,
"jobID": "7501w000002WnWcAAK"
}
}
}`,
`
sfdx oa:apex:log:delete -s "Internal Salesforce.com Error,success"
Query: SELECT Id FROM ApexLog WHERE Status IN ('Internal Salesforce.com Error','success') does not return any record`,
`
sfdx oa:apex:log:delete -m
Query: SELECT Id FROM ApexLog WHERE LogUserId = '0054J000005OInNQAW' does not return any record`,
`
oa:apex:log:delete -c -n test1@user.org,test2@user.org
Query: SELECT Id FROM ApexLog WHERE LogUser.Username IN ('test1@user.org','test2@user.org') does not return any record`
];

protected static requiresUsername = true;
protected static flagsConfig = {
checkonly: flags.boolean({ char: 'c', description: messages.getMessage('checkOnlyFlagDescription') }),
async: flags.boolean({ char: 'a', description: messages.getMessage('asyncFlagDescription') })
async: flags.boolean({ char: 'a', description: messages.getMessage('asyncFlagDescription') }),
status: flags.array({ char: 's', description: messages.getMessage('statusFlagDescription') }),
name: flags.array({ char: 'n', description: messages.getMessage('nameFlagDescription') }),
my: flags.boolean({ char: 'm', description: messages.getMessage('myFlagDescription') })
};
private queryString: string;

public async run(): Promise<AnyJson> {

let response: AnyJson = ({ numberOfQueriedLogs: 0 });
const conn = this.org.getConnection();
const apexLogRecords = await this.getAllApexLogs();

if (apexLogRecords.length > 0) {
const apexLogRecords = await this.getApexLogs(conn);

if (apexLogRecords && apexLogRecords.length > 0) {
this.ux.log(messages.getMessage('recordsLenghtMessage', [apexLogRecords.length]));
if (this.flags.checkonly) {
response = ({ numberOfQueriedLogs: apexLogRecords.length });
Expand All @@ -57,23 +70,59 @@ sfdx oa:apex:log:delete --json'
response = await this.trackBatchProgress(batch, apexLogRecords.length);
}
} else {
this.ux.log(messages.getMessage('noLogsMessage'));
this.ux.log(messages.getMessage('query') + this.queryString + ' ' + messages.getMessage('doesNotReturnAnyRecord'));
response = ({ numberOfQueriedLogs: 0 });
}
return response;
}

public async getAllApexLogs() {
const response = this.org.getConnection()
public async getApexLogs(conn: core.Connection) {

let query = this.org.getConnection()
.sobject('ApexLog')
.find({}, ['Id'])
.execute({ autoFetch: true, maxFetch: 10000 }, async (err, records) => {
return records;
});
.find({}, ['Id']);

const whereClause = await this.buildWhereClause(conn);
if (whereClause) {
query = query.where(whereClause);
}
this.queryString = await query.toSOQL((err, soql) => {
return soql;
});

const response = query.execute({ autoFetch: true, maxFetch: 20000 }, async (err, records) => {
return records;
});
return response;
}

public createBatch(conn: core.Connection, records: Array<{ Id?: string; }>): Batch {
private async buildWhereClause(conn: core.Connection): Promise<string> {

const whereClause: string[] = new Array<string>();
if (this.flags.my) {
const identitystr = (await conn.identity()).user_id;
whereClause.push("LogUserId = '" + identitystr + "'");
} else if (this.flags.name) {
const formatedNames = this.flags.name.reduce((acc, currval) => {
return acc + '\'' + currval + '\',';
}, '').slice(0, -1);
whereClause.push(`LogUser.Username IN (${formatedNames})`);
}

if (this.flags.status) {
const formatedStatuses = this.flags.status.reduce((acc, currval) => {
return acc + '\'' + currval + '\',';
}, '').slice(0, -1);
whereClause.push(`Status IN (${formatedStatuses})`);
}

if (whereClause.length) {
return whereClause.join(' AND ');
}
return '';
}

private createBatch(conn: core.Connection, records: Array<{ Id?: string; }>): Batch {
return conn.bulk.createJob('ApexLog', 'delete').createBatch().execute(records);
}

Expand Down
Loading

0 comments on commit d0d7673

Please sign in to comment.