Skip to content

Commit 1e3e682

Browse files
authored
Introduce save flag for saving to context on creation (#114)
1 parent c6144af commit 1e3e682

5 files changed

Lines changed: 31 additions & 2 deletions

File tree

docs/imodel/create.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Create an empty iModel within a specified iTwin.
3030
- **`latitude`**: `number`
3131
- **`longitude`**: `number`
3232

33+
- **`--save`**
34+
Save the iTwin id to the [context](./../environment.md).
35+
**Type:** `flag` **Required:** No
36+
3337
## Examples
3438

3539
```bash

docs/itwin/create.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Create a new iTwin with specified properties.
5151
Optional parent iTwin Id. Defaults to user's Account iTwin.
5252
**Type:** `string` **Required:** No
5353

54+
- **`--save`**
55+
Save the iTwin id to the [context](./../environment.md).
56+
**Type:** `flag` **Required:** No
57+
5458
## Examples
5559

5660
```bash

src/commands/imodel/create.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export default class CreateIModel extends BaseCommand {
5252
helpValue: '<string>',
5353
required: true,
5454
}),
55+
save: Flags.boolean({
56+
description: 'Save the iModel id to the context.',
57+
required: false,
58+
}),
5559
};
5660

5761
async run() {
@@ -71,7 +75,9 @@ export default class CreateIModel extends BaseCommand {
7175
name: flags.name,
7276
},
7377
});
74-
78+
79+
this.setContext(iModel.iTwinId, iModel.id);
80+
7581
return this.logAndReturnResult(iModel);
7682
}
7783
}

src/commands/itwin/create.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export default class CreateITwin extends BaseCommand {
7373
helpValue: '<string>',
7474
required: false,
7575
}),
76+
save: Flags.boolean({
77+
description: 'Save the iTwin id to the context.',
78+
required: false,
79+
}),
7680
status: Flags.string({
7781
description: 'Status of the iTwin. Defaults to Active.',
7882
helpValue: '<string>',
@@ -116,6 +120,15 @@ export default class CreateITwin extends BaseCommand {
116120
{
117121
this.error(JSON.stringify(creatediTwin.error, null, 2));
118122
}
123+
124+
if (flags.save) {
125+
if(creatediTwin.data?.id === undefined){
126+
this.log("iTwin Id not found in response. Cannot save to context.");
127+
}
128+
else {
129+
this.setContext(creatediTwin.data.id);
130+
}
131+
}
119132

120133
return this.logAndReturnResult(creatediTwin.data);
121134
}

src/extensions/base-command.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export default abstract class BaseCommand extends Command {
263263
return this.config.runCommand<T>(command, mergedArgs);
264264
}
265265

266-
protected async setContext(iTwinId: string, iModelId: string | undefined) {
266+
protected async setContext(iTwinId: string, iModelId?: string) {
267267
const contextPath = this.config.cacheDir + '/context.json';
268268

269269
const context: UserContext = {
@@ -277,6 +277,8 @@ export default abstract class BaseCommand extends Command {
277277

278278
fs.writeFileSync(contextPath, JSON.stringify(context, null, 2), 'utf8');
279279

280+
this.log(`Context set to iTwinId: ${iTwinId}, iModelId: ${iModelId}`);
281+
280282
return context;
281283
}
282284
}

0 commit comments

Comments
 (0)