Skip to content

Commit

Permalink
Filter by arbitrary attributes (#40)
Browse files Browse the repository at this point in the history
```
laconic cns record list \
  --type GeneralRecord \
  --value anything-goes-here \
  --category filter-by-this \
  --bond-id d0094c75e267abb709d631abd7bfaa8d610413f5766ddfc07db735822905d641 \
  --owner AB0A17A1EBF47DDCF6AB267CA8E07B7EA836E1AF \
  --all
```

Reviewed-on: https://git.vdb.to/cerc-io/laconic-registry-cli/pulls/40
Co-authored-by: Thomas E Lackey <telackey@bozemanpass.com>
Co-committed-by: Thomas E Lackey <telackey@bozemanpass.com>
  • Loading branch information
telackey authored and Thomas E Lackey committed Nov 28, 2023
1 parent 1fa32a3 commit 6e0829d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/cmds/cns-cmds/record-cmds/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const builder = {
'bond-id': {
type: 'string'
},
owner: {
type: 'string'
},
type: {
type: 'string'
},
Expand All @@ -27,14 +30,30 @@ export const builder = {
export const handler = async (argv: Arguments) => {
const { services: { cns: cnsConfig } } = getConfig(argv.config as string)
const { restEndpoint, gqlEndpoint, chainId } = getConnectionInfo(argv, cnsConfig);
const { type, name, bondId, all } = argv;
const { type, name, bondId, owner, all } = argv;
const filters: any = {};

const filterArgs = argv._.slice(3);
for (let i = 0; i < filterArgs.length-1; i+=2) {
filters[String(filterArgs[i]).replace(/^-+/,"")] = filterArgs[i+1];
}

assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(chainId, 'Invalid CNS Chain ID.');

const registry = new Registry(gqlEndpoint, restEndpoint, chainId);

const result = await registry.queryRecords({ bondId, type, name }, all as boolean);
queryOutput(result,argv.output)
let result = await registry.queryRecords({...filters, type, name}, all as boolean);

// Apply ex post filters.
if (bondId) {
result = result.filter(v => v.bondId === bondId);
}

if (owner) {
result = result.filter(v => v.owners?.find((e: string) => e === owner));
}

queryOutput(result, argv.output)
}
1 change: 1 addition & 0 deletions src/cmds/cns-cmds/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const desc = 'Record operations.';

exports.builder = (yargs: yargs.Argv) => {
return yargs.commandDir('record-cmds')
.parserConfiguration({'unknown-options-as-args': true})
.demandCommand()
}

0 comments on commit 6e0829d

Please sign in to comment.