Skip to content

Commit 487e238

Browse files
committed
feat(keto-cli): add CreateRelationCommand
1 parent aad88ff commit 487e238

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

packages/keto-cli/src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Module } from '@nestjs/common';
66
import { ConfigModule, ConfigService } from '@nestjs/config';
77

88
import { CheckPermissionCommand } from './check-permission.command';
9+
import { CreateRelationCommand } from './create-relation.command';
910
import { OryKetoEnvironmentVariables, validate } from './environment-variables';
1011

1112
@Module({
@@ -33,6 +34,6 @@ import { OryKetoEnvironmentVariables, validate } from './environment-variables';
3334
}),
3435
}),
3536
],
36-
providers: [CheckPermissionCommand],
37+
providers: [CheckPermissionCommand, CreateRelationCommand],
3738
})
3839
export class AppModule {}

packages/keto-cli/src/app/check-permission.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class CheckPermissionCommand extends CommandRunner {
2525

2626
async run(passedParams: string[], options: CommandOptions): Promise<void> {
2727
const { tuple } = options;
28-
if (options.accessToken || options.basePath) {
28+
if (options.basePath) {
2929
this.oryPermissionsService.config = new Configuration({
3030
...this.oryPermissionsService.config,
3131
...options,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { OryRelationshipsService } from '@getlarge/keto-client-wrapper';
2+
import {
3+
createRelationQuery,
4+
parseRelationTuple,
5+
} from '@getlarge/keto-relations-parser';
6+
import { Logger } from '@nestjs/common';
7+
import { Configuration, type RelationQuery } from '@ory/client';
8+
import { Command, CommandRunner, Option } from 'nest-commander';
9+
10+
interface CommandOptions
11+
extends Pick<Configuration, 'basePath' | 'accessToken'> {
12+
tuple: RelationQuery;
13+
}
14+
15+
@Command({ name: 'create', description: 'Create relationship on Ory Keto' })
16+
export class CreateRelationCommand extends CommandRunner {
17+
readonly logger = new Logger(CreateRelationCommand.name);
18+
19+
constructor(
20+
private readonly oryRelationshipsService: OryRelationshipsService
21+
) {
22+
super();
23+
}
24+
25+
async run(passedParams: string[], options: CommandOptions): Promise<void> {
26+
console.log('options', options);
27+
const { tuple } = options;
28+
if (options.accessToken || options.basePath) {
29+
this.oryRelationshipsService.config = new Configuration({
30+
...this.oryRelationshipsService.config,
31+
...options,
32+
});
33+
}
34+
await this.oryRelationshipsService.createRelationship({
35+
createRelationshipBody: tuple,
36+
});
37+
this.logger.debug('Created relation');
38+
this.logger.log(tuple);
39+
}
40+
41+
@Option({
42+
flags: '-t, --tuple [string]',
43+
description: 'Relationship tuple to create, using Zanzibar notation',
44+
required: true,
45+
})
46+
parseRelationTuple(val: string): RelationQuery {
47+
const res = parseRelationTuple(val);
48+
if (res.hasError()) {
49+
throw res.error;
50+
}
51+
return createRelationQuery(res.value).unwrapOrThrow();
52+
}
53+
54+
@Option({
55+
flags: '-b, --basePath [string]',
56+
description: 'Ory Keto Admin URL',
57+
required: false,
58+
})
59+
parseBasePath(val: string): string | undefined {
60+
return val;
61+
}
62+
63+
@Option({
64+
flags: '-a, --accessToken [string]',
65+
description: 'Ory Keto Access Token',
66+
required: false,
67+
})
68+
parseAccessToken(val: string): string | undefined {
69+
return val ?? process.env['ORY_KETO_API_KEY'];
70+
}
71+
}

0 commit comments

Comments
 (0)