Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pfgrep instead of QShell grep when possible #2429

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ Thanks so much to everyone [who has contributed](https://github.com/codefori/vsc
* [@NicolasSchindler](https://github.com/NicolasSchindler)
* [@marcin-ogon](https://github.com/marcin-ogon)
* [@Detrytus59](https://github.com/Detrytus59)
* [@NattyNarwhal](https://github.com/NattyNarwhal)
3 changes: 2 additions & 1 deletion src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures
},
{
path: `/QOpenSys/pkgs/bin/`,
names: [`git`, `grep`, `tn5250`, `md5sum`, `bash`, `chsh`, `stat`, `sort`, `tar`, `ls`, `find`]
names: [`git`, `grep`, `tn5250`, `pfgrep`, `md5sum`, `bash`, `chsh`, `stat`, `sort`, `tar`, `ls`, `find`]
},
{
path: `/QSYS.LIB/`,
Expand Down Expand Up @@ -171,6 +171,7 @@ export default class IBMi {
this.remoteFeatures = {
git: undefined,
grep: undefined,
pfgrep: undefined,
tn5250: undefined,
setccsid: undefined,
md5sum: undefined,
Expand Down
25 changes: 20 additions & 5 deletions src/api/Search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { GetMemberInfo } from '../components/getMemberInfo';
import { IBMiMember, SearchHit, SearchResults } from '../typings';
import { IBMiMember, SearchHit, SearchResults, CommandResult } from '../typings';
import { GlobalConfiguration } from './Configuration';
import Instance from './Instance';
import { Tools } from './Tools';
Expand All @@ -15,6 +15,8 @@ export namespace Search {
let detailedMembers: IBMiMember[]|undefined;
let memberFilter: string|undefined;

const pfgrep = connection.remoteFeatures.pfgrep;

if (typeof members === `string`) {
memberFilter = connection.sysNameInAmerican(`${members}.MBR`);
} else
Expand Down Expand Up @@ -42,10 +44,23 @@ export namespace Search {
}

// Then search the members
const result = await connection.sendQsh({
command: `/usr/bin/grep -inHR -F "${sanitizeSearchTerm(searchTerm)}" ${memberFilter}`,
directory: connection.sysNameInAmerican(`${asp}/QSYS.LIB/${library}.LIB/${sourceFile}.FILE`)
});
var result: CommandResult | undefined = undefined;
if (pfgrep) {
// pfgrep vs. qshell grep difference: uses -r for recursion instead of -R
// (GNU/BSD grep treat them the same); we don't use recursion yet though...
// another difference: use -t to trim ending whitespace (pfgrep leaves intact)
const command = `${pfgrep} -inHrt -F "${sanitizeSearchTerm(searchTerm)}" ${memberFilter}`;
result = await connection.sendCommand({
command: command,
directory: connection.sysNameInAmerican(`${asp}/QSYS.LIB/${library}.LIB/${sourceFile}.FILE`)
});
} else {
const command = `/usr/bin/grep -inHR -F "${sanitizeSearchTerm(searchTerm)}" ${memberFilter}`;
result = await connection.sendQsh({
command: command,
directory: connection.sysNameInAmerican(`${asp}/QSYS.LIB/${library}.LIB/${sourceFile}.FILE`)
});
}

if (!result.stderr) {
let hits = parseGrepOutput(
Expand Down
15 changes: 15 additions & 0 deletions src/testing/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ export const SearchSuite: TestSuite = {
assert.ok(checkNames(result.hits.map(hit => hit.path.split("/").at(-1)!)));
assert.ok(result.hits.every(hit => !hit.path.endsWith(`MBR`)));
}
},
{
name: "pfgrep vs. qsh grep equivalency", test: async () => {
const pfgrep = getConnection().remoteFeatures.pfgrep;
// This test only needs to run if pfgrep is installed
if (pfgrep) {
const resultPfgrep = await Search.searchMembers(instance, "QSYSINC", "QRPGLESRC", "IBM", "CMRPG");
getConnection().remoteFeatures.pfgrep = undefined;
const resultQsh = await Search.searchMembers(instance, "QSYSINC", "QRPGLESRC", "IBM", "CMRPG");
getConnection().remoteFeatures.pfgrep = pfgrep;
assert.deepEqual(resultPfgrep, resultQsh);
} else {
assert.ok(true)
}
}
}
]
}
Expand Down