@contextual-io/cli 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +456 -1
  2. package/dist/commands/base.d.ts +41 -0
  3. package/dist/commands/base.js +164 -0
  4. package/dist/commands/config/add.d.ts +13 -0
  5. package/dist/commands/config/add.js +34 -0
  6. package/dist/commands/config/current.d.ts +10 -0
  7. package/dist/commands/config/current.js +15 -0
  8. package/dist/commands/config/delete.d.ts +10 -0
  9. package/dist/commands/config/delete.js +17 -0
  10. package/dist/commands/config/get.d.ts +12 -0
  11. package/dist/commands/config/get.js +25 -0
  12. package/dist/commands/config/list.d.ts +13 -0
  13. package/dist/commands/config/list.js +33 -0
  14. package/dist/commands/config/login.d.ts +8 -0
  15. package/dist/commands/config/login.js +21 -0
  16. package/dist/commands/config/use.d.ts +10 -0
  17. package/dist/commands/config/use.js +17 -0
  18. package/dist/commands/records/add.d.ts +14 -0
  19. package/dist/commands/records/add.js +73 -0
  20. package/dist/commands/records/delete.d.ts +13 -0
  21. package/dist/commands/records/delete.js +55 -0
  22. package/dist/commands/records/get.d.ts +13 -0
  23. package/dist/commands/records/get.js +53 -0
  24. package/dist/commands/records/list.d.ts +22 -0
  25. package/dist/commands/records/list.js +145 -0
  26. package/dist/commands/records/query.d.ts +19 -0
  27. package/dist/commands/records/query.js +134 -0
  28. package/dist/commands/records/replace.d.ts +14 -0
  29. package/dist/commands/records/replace.js +69 -0
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +1 -0
  32. package/dist/models/cli-configuration.d.ts +9 -0
  33. package/dist/models/cli-configuration.js +8 -0
  34. package/dist/models/errors.d.ts +5 -0
  35. package/dist/models/errors.js +14 -0
  36. package/dist/models/silo.d.ts +7 -0
  37. package/dist/models/silo.js +2 -0
  38. package/dist/models/uri.d.ts +7 -0
  39. package/dist/models/uri.js +7 -0
  40. package/dist/models/url-safe-id.d.ts +3 -0
  41. package/dist/models/url-safe-id.js +4 -0
  42. package/dist/models/user-config.d.ts +54 -0
  43. package/dist/models/user-config.js +24 -0
  44. package/dist/utils/auth.d.ts +6 -0
  45. package/dist/utils/auth.js +101 -0
  46. package/dist/utils/cli-configuration.d.ts +3 -0
  47. package/dist/utils/cli-configuration.js +19 -0
  48. package/dist/utils/endpoints.d.ts +3 -0
  49. package/dist/utils/endpoints.js +2 -0
  50. package/dist/utils/stream.d.ts +3 -0
  51. package/dist/utils/stream.js +21 -0
  52. package/dist/utils/time.d.ts +2 -0
  53. package/dist/utils/time.js +5 -0
  54. package/oclif.manifest.json +740 -2
  55. package/package.json +1 -1
package/README.md CHANGED
@@ -20,7 +20,7 @@ $ npm install -g @contextual-io/cli
20
20
  $ ctxl COMMAND
21
21
  running command...
22
22
  $ ctxl (--version)
23
- @contextual-io/cli/0.1.1 linux-x64 node-v25.2.1
23
+ @contextual-io/cli/0.1.3 linux-x64 node-v25.2.1
24
24
  $ ctxl --help [COMMAND]
25
25
  USAGE
26
26
  $ ctxl COMMAND
@@ -29,7 +29,17 @@ USAGE
29
29
  <!-- usagestop -->
30
30
  # Commands
31
31
  <!-- commands -->
32
+ * [`ctxl base`](#ctxl-base)
33
+ * [`ctxl config add CONFIG-ID`](#ctxl-config-add-config-id)
34
+ * [`ctxl config current`](#ctxl-config-current)
35
+ * [`ctxl config delete CONFIG-ID`](#ctxl-config-delete-config-id)
36
+ * [`ctxl config get [CONFIG-ID]`](#ctxl-config-get-config-id)
37
+ * [`ctxl config list`](#ctxl-config-list)
38
+ * [`ctxl config login`](#ctxl-config-login)
39
+ * [`ctxl config use CONFIG-ID`](#ctxl-config-use-config-id)
40
+ * [`ctxl create [URI]`](#ctxl-create-uri)
32
41
  * [`ctxl help [COMMAND]`](#ctxl-help-command)
42
+ * [`ctxl import [URI]`](#ctxl-import-uri)
33
43
  * [`ctxl plugins`](#ctxl-plugins)
34
44
  * [`ctxl plugins add PLUGIN`](#ctxl-plugins-add-plugin)
35
45
  * [`ctxl plugins:inspect PLUGIN...`](#ctxl-pluginsinspect-plugin)
@@ -40,6 +50,202 @@ USAGE
40
50
  * [`ctxl plugins uninstall [PLUGIN]`](#ctxl-plugins-uninstall-plugin)
41
51
  * [`ctxl plugins unlink [PLUGIN]`](#ctxl-plugins-unlink-plugin)
42
52
  * [`ctxl plugins update`](#ctxl-plugins-update)
53
+ * [`ctxl records add [URI]`](#ctxl-records-add-uri)
54
+ * [`ctxl records delete [URI]`](#ctxl-records-delete-uri)
55
+ * [`ctxl records get [URI]`](#ctxl-records-get-uri)
56
+ * [`ctxl records list [URI]`](#ctxl-records-list-uri)
57
+ * [`ctxl records query [URI]`](#ctxl-records-query-uri)
58
+ * [`ctxl records replace [URI]`](#ctxl-records-replace-uri)
59
+
60
+ ## `ctxl base`
61
+
62
+ ```
63
+ USAGE
64
+ $ ctxl base [-C <value>]
65
+
66
+ GLOBAL FLAGS
67
+ -C, --config-id=<value> Specify config id to use for call.
68
+ ```
69
+
70
+ _See code: [src/commands/base.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/base.ts)_
71
+
72
+ ## `ctxl config add CONFIG-ID`
73
+
74
+ add a new config
75
+
76
+ ```
77
+ USAGE
78
+ $ ctxl config add CONFIG-ID [-s prod|qa|dev] [-t <value>]
79
+
80
+ ARGUMENTS
81
+ CONFIG-ID id of config
82
+
83
+ FLAGS
84
+ -s, --silo=<option> [default: prod] silo
85
+ <options: prod|qa|dev>
86
+ -t, --tenant-id=<value> tenant id (if different from config id)
87
+
88
+ DESCRIPTION
89
+ add a new config
90
+
91
+ EXAMPLES
92
+ $ ctxl config add some-config-id --tenant-id my-tenant
93
+ ```
94
+
95
+ _See code: [src/commands/config/add.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/add.ts)_
96
+
97
+ ## `ctxl config current`
98
+
99
+ get the current config
100
+
101
+ ```
102
+ USAGE
103
+ $ ctxl config current [--json]
104
+
105
+ GLOBAL FLAGS
106
+ --json Format output as json.
107
+
108
+ DESCRIPTION
109
+ get the current config
110
+
111
+ EXAMPLES
112
+ $ ctxl config current
113
+ ```
114
+
115
+ _See code: [src/commands/config/current.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/current.ts)_
116
+
117
+ ## `ctxl config delete CONFIG-ID`
118
+
119
+ delete a config
120
+
121
+ ```
122
+ USAGE
123
+ $ ctxl config delete CONFIG-ID
124
+
125
+ ARGUMENTS
126
+ CONFIG-ID id of config
127
+
128
+ DESCRIPTION
129
+ delete a config
130
+
131
+ EXAMPLES
132
+ $ ctxl config delete some-config-id
133
+ ```
134
+
135
+ _See code: [src/commands/config/delete.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/delete.ts)_
136
+
137
+ ## `ctxl config get [CONFIG-ID]`
138
+
139
+ get a specific config
140
+
141
+ ```
142
+ USAGE
143
+ $ ctxl config get [CONFIG-ID] [--json]
144
+
145
+ ARGUMENTS
146
+ [CONFIG-ID] id of config
147
+
148
+ GLOBAL FLAGS
149
+ --json Format output as json.
150
+
151
+ DESCRIPTION
152
+ get a specific config
153
+
154
+ EXAMPLES
155
+ $ ctxl config get
156
+
157
+ $ ctxl config get some-config-id
158
+ ```
159
+
160
+ _See code: [src/commands/config/get.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/get.ts)_
161
+
162
+ ## `ctxl config list`
163
+
164
+ list all available configs
165
+
166
+ ```
167
+ USAGE
168
+ $ ctxl config list [--json]
169
+
170
+ GLOBAL FLAGS
171
+ --json Format output as json.
172
+
173
+ DESCRIPTION
174
+ list all available configs
175
+
176
+ EXAMPLES
177
+ $ ctxl config list
178
+ ```
179
+
180
+ _See code: [src/commands/config/list.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/list.ts)_
181
+
182
+ ## `ctxl config login`
183
+
184
+ login using the current config
185
+
186
+ ```
187
+ USAGE
188
+ $ ctxl config login
189
+
190
+ DESCRIPTION
191
+ login using the current config
192
+
193
+ EXAMPLES
194
+ $ ctxl config login
195
+ ```
196
+
197
+ _See code: [src/commands/config/login.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/login.ts)_
198
+
199
+ ## `ctxl config use CONFIG-ID`
200
+
201
+ use a config
202
+
203
+ ```
204
+ USAGE
205
+ $ ctxl config use CONFIG-ID
206
+
207
+ ARGUMENTS
208
+ CONFIG-ID id of config
209
+
210
+ DESCRIPTION
211
+ use a config
212
+
213
+ EXAMPLES
214
+ $ ctxl config use some-config-id
215
+ ```
216
+
217
+ _See code: [src/commands/config/use.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/config/use.ts)_
218
+
219
+ ## `ctxl create [URI]`
220
+
221
+ create record(s)
222
+
223
+ ```
224
+ USAGE
225
+ $ ctxl create [URI] [-C <value>] [-i <value>] [-T <value>]
226
+
227
+ ARGUMENTS
228
+ [URI] uri of type
229
+
230
+ FLAGS
231
+ -T, --type=<value> type
232
+ -i, --input-file=<value> [default: -] file to read. can read stdin if value is '-'
233
+
234
+ GLOBAL FLAGS
235
+ -C, --config-id=<value> Specify config id to use for call.
236
+
237
+ DESCRIPTION
238
+ create record(s)
239
+
240
+ ALIASES
241
+ $ ctxl create
242
+ $ ctxl import
243
+
244
+ EXAMPLES
245
+ $ ctxl create native-object:my-type
246
+
247
+ $ ctxl create --type my-type --input-file records.json
248
+ ```
43
249
 
44
250
  ## `ctxl help [COMMAND]`
45
251
 
@@ -61,6 +267,37 @@ DESCRIPTION
61
267
 
62
268
  _See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v6.2.36/src/commands/help.ts)_
63
269
 
270
+ ## `ctxl import [URI]`
271
+
272
+ create record(s)
273
+
274
+ ```
275
+ USAGE
276
+ $ ctxl import [URI] [-C <value>] [-i <value>] [-T <value>]
277
+
278
+ ARGUMENTS
279
+ [URI] uri of type
280
+
281
+ FLAGS
282
+ -T, --type=<value> type
283
+ -i, --input-file=<value> [default: -] file to read. can read stdin if value is '-'
284
+
285
+ GLOBAL FLAGS
286
+ -C, --config-id=<value> Specify config id to use for call.
287
+
288
+ DESCRIPTION
289
+ create record(s)
290
+
291
+ ALIASES
292
+ $ ctxl create
293
+ $ ctxl import
294
+
295
+ EXAMPLES
296
+ $ ctxl import native-object:my-type
297
+
298
+ $ ctxl import --type my-type --input-file records.json
299
+ ```
300
+
64
301
  ## `ctxl plugins`
65
302
 
66
303
  List installed plugins.
@@ -350,4 +587,222 @@ DESCRIPTION
350
587
  ```
351
588
 
352
589
  _See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.4.54/src/commands/plugins/update.ts)_
590
+
591
+ ## `ctxl records add [URI]`
592
+
593
+ create record(s)
594
+
595
+ ```
596
+ USAGE
597
+ $ ctxl records add [URI] [-C <value>] [-i <value>] [-T <value>]
598
+
599
+ ARGUMENTS
600
+ [URI] uri of type
601
+
602
+ FLAGS
603
+ -T, --type=<value> type
604
+ -i, --input-file=<value> [default: -] file to read. can read stdin if value is '-'
605
+
606
+ GLOBAL FLAGS
607
+ -C, --config-id=<value> Specify config id to use for call.
608
+
609
+ DESCRIPTION
610
+ create record(s)
611
+
612
+ ALIASES
613
+ $ ctxl create
614
+ $ ctxl import
615
+
616
+ EXAMPLES
617
+ $ ctxl records add native-object:my-type
618
+
619
+ $ ctxl records add --type my-type --input-file records.json
620
+ ```
621
+
622
+ _See code: [src/commands/records/add.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/add.ts)_
623
+
624
+ ## `ctxl records delete [URI]`
625
+
626
+ delete record(s)
627
+
628
+ ```
629
+ USAGE
630
+ $ ctxl records delete [URI] [-C <value>] [-i <value>...] [-T <value>]
631
+
632
+ ARGUMENTS
633
+ [URI] uri of record
634
+
635
+ FLAGS
636
+ -T, --type=<value> type
637
+ -i, --id=<value>... id(s)
638
+
639
+ GLOBAL FLAGS
640
+ -C, --config-id=<value> Specify config id to use for call.
641
+
642
+ DESCRIPTION
643
+ delete record(s)
644
+
645
+ EXAMPLES
646
+ $ ctxl records delete native-object:my-type/instance-1
647
+
648
+ $ ctxl records delete native-object:my-type --id instance-1
649
+
650
+ $ ctxl records delete native-object:my-type --id instance-1 --id instance-2
651
+
652
+ $ ctxl records delete --type my-type --id instance-1
653
+
654
+ $ ctxl records delete --type my-type --id instance-1 --id instance-2
655
+ ```
656
+
657
+ _See code: [src/commands/records/delete.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/delete.ts)_
658
+
659
+ ## `ctxl records get [URI]`
660
+
661
+ get a record
662
+
663
+ ```
664
+ USAGE
665
+ $ ctxl records get [URI] [-C <value>] [-i <value>...] [-T <value>]
666
+
667
+ ARGUMENTS
668
+ [URI] uri of record
669
+
670
+ FLAGS
671
+ -T, --type=<value> type
672
+ -i, --id=<value>... id(s)
673
+
674
+ GLOBAL FLAGS
675
+ -C, --config-id=<value> Specify config id to use for call.
676
+
677
+ DESCRIPTION
678
+ get a record
679
+
680
+ EXAMPLES
681
+ $ ctxl records get native-object:my-type/instance-1
682
+
683
+ $ ctxl records get native-object:my-type --id instance-1
684
+
685
+ $ ctxl records get native-object:my-type --id instance-1 --id instance-2
686
+
687
+ $ ctxl records get --type my-type --id instance-1
688
+
689
+ $ ctxl records get --type my-type --id instance-1 --id instance-2
690
+ ```
691
+
692
+ _See code: [src/commands/records/get.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/get.ts)_
693
+
694
+ ## `ctxl records list [URI]`
695
+
696
+ list records
697
+
698
+ ```
699
+ USAGE
700
+ $ ctxl records list [URI] [-C <value>] [-S <value>...] [-e] [-f <value>...] [--include-total] [-o <value>...]
701
+ [--page-size <value>] [--page-token <value>] [-p] [-s <value>...] [-t <value>...] [-T <value>]
702
+
703
+ ARGUMENTS
704
+ [URI] uri of type
705
+
706
+ FLAGS
707
+ -S, --exact-search=<value>... search fields exactly
708
+ -T, --type=<value> type
709
+ -e, --export export data as JSONL
710
+ -f, --from=<value>... search from
711
+ -o, --order-by=<value>... order fields
712
+ -p, --progress show progress during export
713
+ -s, --search=<value>... search fields
714
+ -t, --to=<value>... search to
715
+ --include-total include total count
716
+ --page-size=<value> number of records per page
717
+ --page-token=<value> page token to fetch
718
+
719
+ GLOBAL FLAGS
720
+ -C, --config-id=<value> Specify config id to use for call.
721
+
722
+ DESCRIPTION
723
+ list records
724
+
725
+ EXAMPLES
726
+ $ ctxl records list native-object:my-type
727
+
728
+ $ ctxl records list --type my-type
729
+
730
+ $ ctxl records list --type my-type --order-by field1:desc
731
+
732
+ $ ctxl records list --type my-type -s field1=value1 -s field2=value2 --include-total
733
+ ```
734
+
735
+ _See code: [src/commands/records/list.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/list.ts)_
736
+
737
+ ## `ctxl records query [URI]`
738
+
739
+ query records
740
+
741
+ ```
742
+ USAGE
743
+ $ ctxl records query [URI] [-C <value>] [-e] [--include-total] [-o <value>...] [--page-size <value>]
744
+ [--page-token <value>] [-p] [-q <value>] [-T <value>]
745
+
746
+ ARGUMENTS
747
+ [URI] uri of type
748
+
749
+ FLAGS
750
+ -T, --type=<value> type
751
+ -e, --export export data as JSONL
752
+ -o, --order-by=<value>... order fields
753
+ -p, --progress show progress during export
754
+ -q, --query-file=<value> [default: -] file to read. can read stdin if value is '-'
755
+ --include-total include total count
756
+ --page-size=<value> number of records per page
757
+ --page-token=<value> page token to fetch
758
+
759
+ GLOBAL FLAGS
760
+ -C, --config-id=<value> Specify config id to use for call.
761
+
762
+ DESCRIPTION
763
+ query records
764
+
765
+ EXAMPLES
766
+ $ ctxl records query native-object:my-type
767
+
768
+ $ ctxl records query --type my-type --query-file query.json
769
+
770
+ $ ctxl records query --type my-type --order-by field1:desc --query-file query.json
771
+
772
+ $ ctxl records query --type my-type --include-total --query-file query.json
773
+ ```
774
+
775
+ _See code: [src/commands/records/query.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/query.ts)_
776
+
777
+ ## `ctxl records replace [URI]`
778
+
779
+ replace a record
780
+
781
+ ```
782
+ USAGE
783
+ $ ctxl records replace [URI] [-C <value>] [-i <value>] [-i <value>] [-T <value>]
784
+
785
+ ARGUMENTS
786
+ [URI] uri of record
787
+
788
+ FLAGS
789
+ -T, --type=<value> type
790
+ -i, --id=<value> id
791
+ -i, --input-file=<value> [default: -] file to read. can read stdin if value is '-'
792
+
793
+ GLOBAL FLAGS
794
+ -C, --config-id=<value> Specify config id to use for call.
795
+
796
+ DESCRIPTION
797
+ replace a record
798
+
799
+ EXAMPLES
800
+ $ ctxl records replace native-object:my-type/instance-1 --input-file record.json
801
+
802
+ $ ctxl records replace native-object:my-type --id instance-1 --input-file record.json
803
+
804
+ $ ctxl records replace --type my-type --id instance-1 --input-file record.json
805
+ ```
806
+
807
+ _See code: [src/commands/records/replace.ts](https://github.com/ContextualIO/contextual-cli/blob/v0.1.3/src/commands/records/replace.ts)_
353
808
  <!-- commandsstop -->
@@ -0,0 +1,41 @@
1
+ import { Command, Interfaces } from "@oclif/core";
2
+ import fetch from "cross-fetch";
3
+ import { UrlSafeId } from "../models/url-safe-id.js";
4
+ import { CleanConfig, Config, UserConfig } from "../models/user-config.js";
5
+ export type Flags<T extends typeof Command> = Interfaces.InferredFlags<T["flags"] & typeof BaseConfigCommand["baseFlags"]>;
6
+ export type Args<T extends typeof Command> = Interfaces.InferredArgs<T["args"]>;
7
+ declare class CommandConfig {
8
+ private readonly configDir;
9
+ constructor(configDir: string);
10
+ clean(configId: UrlSafeId, { silo, tenantId }: Config): CleanConfig;
11
+ get(configId?: UrlSafeId): Promise<{
12
+ config: Config;
13
+ lookup: string;
14
+ }>;
15
+ read(): UserConfig;
16
+ remove(configId: UrlSafeId): void;
17
+ upsert(configId: UrlSafeId, config: Config): Config;
18
+ use(configId: UrlSafeId): Promise<Config>;
19
+ private writeUserConfig;
20
+ }
21
+ export declare abstract class BaseConfigCommand<T extends typeof Command> extends Command {
22
+ static baseFlags: {};
23
+ protected args: Args<T>;
24
+ protected flags: Flags<T>;
25
+ protected userConfig: CommandConfig;
26
+ protected catch(err: Error & {
27
+ exitCode?: number;
28
+ }): Promise<unknown>;
29
+ protected finally(_: Error | undefined): Promise<unknown>;
30
+ init(): Promise<void>;
31
+ }
32
+ export declare abstract class BaseCommand<T extends typeof Command> extends BaseConfigCommand<T> {
33
+ static baseFlags: {
34
+ "config-id": Interfaces.OptionFlag<string | undefined, Interfaces.CustomOptions>;
35
+ };
36
+ protected currentConfig(): Promise<Config & {
37
+ bearerToken: string;
38
+ }>;
39
+ protected fetch(getUrl: (config: Config) => string, init?: Parameters<typeof fetch>[1]): Promise<any>;
40
+ }
41
+ export {};
@@ -0,0 +1,164 @@
1
+ import { Command, Flags } from "@oclif/core";
2
+ import fetch from "cross-fetch";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { HttpError } from "../models/errors.js";
6
+ import { UserConfig } from "../models/user-config.js";
7
+ import { rehydrateToken } from "../utils/auth.js";
8
+ class CommandConfig {
9
+ configDir;
10
+ constructor(configDir) {
11
+ this.configDir = configDir;
12
+ }
13
+ clean(configId, { silo, tenantId }) {
14
+ return {
15
+ configId,
16
+ silo,
17
+ tenantId,
18
+ };
19
+ }
20
+ async get(configId) {
21
+ const userConfig = this.read();
22
+ const lookup = configId ?? userConfig.current;
23
+ let config = lookup ? userConfig.configs[lookup] : undefined;
24
+ if (!lookup) {
25
+ throw new Error("No config id found");
26
+ }
27
+ if (!config) {
28
+ throw new Error(`No config with id '${lookup}' found`);
29
+ }
30
+ if (config.refreshToken && config.expiresAt) {
31
+ const expired = new Date();
32
+ expired.setUTCMinutes(expired.getUTCMinutes() + 5);
33
+ if (config.expiresAt <= expired.toISOString()) {
34
+ const auth = await rehydrateToken(config.auth, config.refreshToken);
35
+ config = this.upsert(lookup, {
36
+ ...config,
37
+ ...auth,
38
+ refreshToken: auth.refreshToken ?? config.refreshToken,
39
+ });
40
+ }
41
+ }
42
+ return { config, lookup };
43
+ }
44
+ read() {
45
+ const { configDir } = this;
46
+ this.writeUserConfig({ configs: {} }, false);
47
+ const configPath = path.join(configDir, "config.json");
48
+ return UserConfig.parse(JSON.parse(fs.readFileSync(configPath, "utf8")));
49
+ }
50
+ remove(configId) {
51
+ const userConfig = this.read();
52
+ if (!userConfig.configs[configId]) {
53
+ throw new Error(`No config with id '${configId}' found`);
54
+ }
55
+ delete userConfig.configs[configId];
56
+ if (userConfig.current === configId)
57
+ userConfig.current = undefined;
58
+ this.writeUserConfig(userConfig);
59
+ }
60
+ upsert(configId, config) {
61
+ const userConfig = this.read();
62
+ userConfig.configs[configId] = config;
63
+ userConfig.current ??= configId;
64
+ this.writeUserConfig(userConfig);
65
+ return config;
66
+ }
67
+ async use(configId) {
68
+ const userConfig = this.read();
69
+ const { config } = await this.get(configId);
70
+ userConfig.current = configId;
71
+ this.writeUserConfig(userConfig);
72
+ return config;
73
+ }
74
+ writeUserConfig(userConfig, overwrite = true) {
75
+ const { configDir } = this;
76
+ if (!fs.existsSync(configDir)) {
77
+ fs.mkdirSync(configDir);
78
+ }
79
+ const configPath = path.join(configDir, "config.json");
80
+ if (overwrite || !fs.existsSync(configPath)) {
81
+ fs.writeFileSync(configPath, JSON.stringify(userConfig, null, 2), "utf8");
82
+ }
83
+ return userConfig;
84
+ }
85
+ }
86
+ export class BaseConfigCommand extends Command {
87
+ // define flags that can be inherited by any command that extends BaseCommand
88
+ static baseFlags = {
89
+ /* "log-level": Flags.option({
90
+ default: "info",
91
+ helpGroup: "GLOBAL",
92
+ options: ["debug", "warn", "error", "info", "trace"] as const,
93
+ summary: "Specify level for logging.",
94
+ })(), */
95
+ };
96
+ args;
97
+ flags;
98
+ userConfig;
99
+ async catch(err) {
100
+ // add any custom logic to handle errors from the command
101
+ // or simply return the parent class error handling
102
+ return super.catch(err);
103
+ }
104
+ async finally(_) {
105
+ // called after run and catch regardless of whether or not the command errored
106
+ return super.finally(_);
107
+ }
108
+ async init() {
109
+ await super.init();
110
+ const { args, flags } = await this.parse({
111
+ args: this.ctor.args,
112
+ baseFlags: super.ctor.baseFlags,
113
+ enableJsonFlag: this.ctor.enableJsonFlag,
114
+ flags: this.ctor.flags,
115
+ strict: this.ctor.strict,
116
+ });
117
+ this.flags = flags;
118
+ this.args = args;
119
+ this.userConfig = new CommandConfig(this.config.configDir);
120
+ }
121
+ }
122
+ export class BaseCommand extends BaseConfigCommand {
123
+ // define flags that can be inherited by any command that extends BaseCommand
124
+ static baseFlags = {
125
+ "config-id": Flags.string({
126
+ char: "C",
127
+ helpGroup: "GLOBAL",
128
+ summary: "Specify config id to use for call.",
129
+ }),
130
+ };
131
+ async currentConfig() {
132
+ const { config, lookup } = await this.userConfig.get(this.flags["config-id"]);
133
+ const { bearerToken } = config;
134
+ if (!bearerToken) {
135
+ throw new Error(`Config '${lookup}' not logged in`);
136
+ }
137
+ return {
138
+ ...config,
139
+ bearerToken,
140
+ };
141
+ }
142
+ async fetch(getUrl, init) {
143
+ const config = await this.currentConfig();
144
+ const url = getUrl(config);
145
+ const rsp = await fetch(url, {
146
+ ...init,
147
+ headers: {
148
+ ...init?.headers,
149
+ authorization: `Bearer ${config.bearerToken}`,
150
+ "x-org-id": config.tenantId,
151
+ },
152
+ });
153
+ const rspContentType = rsp.headers.get("content-type");
154
+ if (!rsp.ok) {
155
+ const body = rspContentType?.startsWith("application/json")
156
+ ? await rsp.json()
157
+ : await rsp.text();
158
+ throw new HttpError(`${init?.method ?? "GET"} ${url}`, body, rsp.status);
159
+ }
160
+ if (rspContentType?.startsWith("application/json"))
161
+ return rsp.json();
162
+ return rsp.text();
163
+ }
164
+ }
@@ -0,0 +1,13 @@
1
+ import { BaseConfigCommand } from "../base.js";
2
+ export default class ConfigAdd extends BaseConfigCommand<typeof ConfigAdd> {
3
+ static args: {
4
+ "config-id": import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
5
+ };
6
+ static description: string;
7
+ static examples: string[];
8
+ static flags: {
9
+ silo: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
10
+ "tenant-id": import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
+ };
12
+ run(): Promise<void>;
13
+ }