contensis-cli 1.0.0-beta.63 → 1.0.0-beta.65

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.
@@ -754,14 +754,19 @@ class ContensisCli {
754
754
 
755
755
  // print the key details to console
756
756
  console.log(
757
- ` - ${key.name}${
758
- key.description ? ` (${key.description})` : ''
759
- } [${key.dateModified.toString().substring(0, 10)} ${key.modifiedBy}]`
757
+ ` - ${chalk.bold(key.name)} [${key.dateModified
758
+ .toString()
759
+ .substring(0, 10)} ${key.modifiedBy}]`
760
760
  );
761
- console.log(` - id: ${key.id}`);
762
- console.log(` - sharedSecret: ${key.sharedSecret}`);
761
+ if (key.description)
762
+ console.log(` ${log.infoText(key.description)}`);
763
+ console.log(` ${chalk.bold.grey`id`}: ${key.id}`);
764
+ console.log(
765
+ ` ${chalk.bold.grey`sharedSecret`}: ${key.sharedSecret}`
766
+ );
767
+ console.log('');
768
+ log.help(messages.keys.tip());
763
769
  }
764
- console.log('');
765
770
 
766
771
  if (err) {
767
772
  log.error(messages.keys.failedCreate(currentEnv, name), err);
@@ -795,6 +800,9 @@ class ContensisCli {
795
800
 
796
801
  if (Array.isArray(roles)) {
797
802
  log.success(messages.roles.list(currentEnv));
803
+
804
+ if (!roles.length) log.help(messages.roles.noneExist());
805
+
798
806
  this.HandleFormattingAndOutput(roles, () => {
799
807
  // print the roles to console
800
808
  for (const {
@@ -807,9 +815,10 @@ class ContensisCli {
807
815
  } of roles) {
808
816
  const color = enabled ? (s: string) => s : log.infoText;
809
817
 
810
- console.log(color(` - ${name} ${log.infoText(id)}`));
818
+ console.log(color(` - ${chalk.bold(name)} ${log.infoText(id)}`));
811
819
  if (description) console.log(log.infoText(` ${description}`));
812
-
820
+ if (enabled === false)
821
+ console.log(` ${chalk.bold.grey('enabled')}: false`);
813
822
  if (assignments.groups?.length)
814
823
  console.log(
815
824
  ` ${chalk.bold.grey('groups')}: ${assignments.groups.join(
@@ -896,6 +905,33 @@ class ContensisCli {
896
905
  }
897
906
  };
898
907
 
908
+ CreateRole = async (role: Partial<Role>) => {
909
+ const { currentEnv, log, messages } = this;
910
+ const contensis = await this.ConnectContensis();
911
+
912
+ if (contensis) {
913
+ const [err, created] = await contensis.roles.CreateRole(role as Role);
914
+
915
+ if (created) {
916
+ log.success(
917
+ messages.roles.created(currentEnv, role.id || role.name || '')
918
+ );
919
+
920
+ this.HandleFormattingAndOutput(created, log.object);
921
+
922
+ log.help(messages.roles.tip());
923
+ return role.id;
924
+ }
925
+
926
+ if (err) {
927
+ log.error(
928
+ messages.roles.failedCreate(currentEnv, role.id || role.name || ''),
929
+ err
930
+ );
931
+ }
932
+ }
933
+ };
934
+
899
935
  UpdateRole = async (roleNameOrId: string, role: Partial<Role>) => {
900
936
  const { currentEnv, log, messages } = this;
901
937
  const contensis = await this.ConnectContensis();
@@ -913,13 +949,59 @@ class ContensisCli {
913
949
  r.name.toLowerCase() === roleNameOrId.toLowerCase()
914
950
  );
915
951
  if (existingRole) {
916
- const rolesService = contensis.roles.sourceRepo.roles;
952
+ log.info(messages.roles.setPayload());
953
+ log.object(role);
954
+ log.raw(``);
955
+ const [updateErr, updated] = await contensis.roles.UpdateRole(
956
+ existingRole.id,
957
+ role
958
+ );
959
+ if (updateErr)
960
+ log.error(messages.roles.failedSet(currentEnv, roleNameOrId));
961
+ else {
962
+ log.success(messages.roles.set());
963
+
964
+ this.HandleFormattingAndOutput(updated, log.object);
965
+ }
966
+ } else {
967
+ // Role does not exist
968
+ log.error(messages.roles.failedGet(currentEnv, roleNameOrId));
969
+ }
970
+ }
971
+
972
+ if (rolesErr) {
973
+ log.error(messages.roles.noList(currentEnv));
974
+ log.error(jsonFormatter(rolesErr));
975
+ }
976
+ }
977
+ };
978
+
979
+ RemoveRole = async (roleNameOrId: string) => {
980
+ const { currentEnv, log, messages } = this;
981
+ const contensis = await this.ConnectContensis();
982
+
983
+ if (contensis) {
984
+ // Retrieve roles list for env
985
+ const [rolesErr, roles] = await to(contensis.roles.GetRoles());
986
+
987
+ if (Array.isArray(roles)) {
988
+ log.success(messages.roles.list(currentEnv));
989
+
990
+ const existingRole = roles.find(
991
+ r =>
992
+ r.id === roleNameOrId ||
993
+ r.name.toLowerCase() === roleNameOrId.toLowerCase()
994
+ );
995
+ if (existingRole) {
996
+ const [deleteErr] = await contensis.roles.RemoveRole(existingRole.id);
917
997
 
918
- await rolesService.UpdateRole(existingRole.id, role);
998
+ if (deleteErr)
999
+ log.error(messages.roles.failedRemove(currentEnv, roleNameOrId));
1000
+ else log.success(messages.roles.removed(currentEnv, roleNameOrId));
919
1001
  } else {
920
- //Role not exist
1002
+ // Role does not exist
1003
+ log.error(messages.roles.failedGet(currentEnv, roleNameOrId));
921
1004
  }
922
- this.HandleFormattingAndOutput(role, log.object);
923
1005
  }
924
1006
 
925
1007
  if (rolesErr) {
@@ -1589,10 +1671,7 @@ class ContensisCli {
1589
1671
  }
1590
1672
  };
1591
1673
 
1592
- PrintWebhookSubscriptions = async (
1593
- subscriptionIds?: string[],
1594
- name?: string
1595
- ) => {
1674
+ PrintWebhookSubscriptions = async (subscriptionIdsOrNames?: string[]) => {
1596
1675
  const { currentEnv, log, messages } = this;
1597
1676
  const contensis = await this.ConnectContensis();
1598
1677
  if (contensis) {
@@ -1600,39 +1679,83 @@ class ContensisCli {
1600
1679
  const [webhooksErr, webhooks] =
1601
1680
  await contensis.subscriptions.webhooks.GetSubscriptions();
1602
1681
 
1603
- const filteredResults =
1604
- typeof name === 'string'
1605
- ? webhooks?.filter(w =>
1606
- w.name?.toLowerCase().includes(name.toLowerCase())
1607
- )
1608
- : Array.isArray(subscriptionIds)
1609
- ? webhooks?.filter(w => subscriptionIds?.some(id => id === w.id))
1610
- : webhooks;
1682
+ const filteredResults = subscriptionIdsOrNames?.length
1683
+ ? webhooks?.filter(
1684
+ w =>
1685
+ subscriptionIdsOrNames?.some(idname =>
1686
+ w.name?.toLowerCase().includes(idname.toLowerCase())
1687
+ ) ||
1688
+ subscriptionIdsOrNames?.some(
1689
+ id => id.toLowerCase() === w.id.toLowerCase()
1690
+ )
1691
+ )
1692
+ : webhooks;
1611
1693
 
1612
1694
  if (Array.isArray(filteredResults)) {
1613
- this.HandleFormattingAndOutput(filteredResults, () => {
1614
- // print the keys to console
1615
- log.success(messages.webhooks.list(currentEnv));
1616
- for (const {
1617
- id,
1618
- description,
1619
- method,
1620
- name,
1621
- version,
1622
- url,
1623
- } of filteredResults) {
1624
- console.log(
1625
- ` - ${name}${
1626
- description ? ` (${description})` : ''
1627
- } [${version.modified.toString().substring(0, 10)} ${
1628
- version.modifiedBy
1629
- }]`
1630
- );
1631
- console.log(` ${id}`);
1632
- console.log(` [${method}] ${url}`);
1633
- }
1634
- console.log('');
1635
- });
1695
+ log.success(messages.webhooks.list(currentEnv));
1696
+ if (!webhooks?.length) log.warning(messages.webhooks.noneExist());
1697
+ else {
1698
+ this.HandleFormattingAndOutput(filteredResults, () => {
1699
+ // print the keys to console
1700
+ for (const {
1701
+ id,
1702
+ description,
1703
+ method,
1704
+ name,
1705
+ version,
1706
+ url,
1707
+ enabled,
1708
+ topics,
1709
+ templates,
1710
+ headers,
1711
+ } of filteredResults) {
1712
+ console.log(
1713
+ log.infoText(
1714
+ ` ${chalk.bold.white`- ${name}`} ${id} [${(
1715
+ version.modified || version.created
1716
+ )
1717
+ .toString()
1718
+ .substring(0, 10)} ${
1719
+ version.modifiedBy || version.createdBy
1720
+ }]`
1721
+ )
1722
+ );
1723
+ if (description) console.log(log.infoText` ${description}`);
1724
+ console.log(` ${log.infoText`[${method}]`} ${url}`);
1725
+ if (headers && Object.keys(headers).length) {
1726
+ console.log(` ${log.infoText`headers`}:`);
1727
+
1728
+ for (const [key, { value, secret }] of Object.entries(headers))
1729
+ console.log(
1730
+ ` ${chalk.bold.gray(key)}: ${secret ? '🤐' : value}`
1731
+ );
1732
+ }
1733
+ if (topics?.length)
1734
+ if (topics?.length === 1)
1735
+ console.log(
1736
+ ` ${log.infoText`topics`}: ${topics
1737
+ .map(t => JSON.stringify(t))
1738
+ .join(' ')
1739
+ .replaceAll('"', '')
1740
+ .replaceAll(',', ' ')
1741
+ .replaceAll('{', '')
1742
+ .replaceAll('}', '')}`
1743
+ );
1744
+ else {
1745
+ console.log(` ${log.infoText`topics`}:`);
1746
+ log.objectRecurse(topics, 1, ' ');
1747
+ }
1748
+ if (templates && Object.keys(templates).length)
1749
+ console.log(
1750
+ ` ${log.infoText`templates`}: ${Object.keys(
1751
+ templates
1752
+ ).join(' ')}`
1753
+ );
1754
+ if (enabled === false)
1755
+ console.log(` ${log.infoText`enabled`}: ${enabled}`);
1756
+ }
1757
+ });
1758
+ }
1636
1759
  }
1637
1760
 
1638
1761
  if (webhooksErr) {
package/src/shell.ts CHANGED
@@ -135,6 +135,7 @@ class ContensisShell {
135
135
  availableCommands.push('login', 'list projects', 'set project');
136
136
  if (userId)
137
137
  availableCommands.push(
138
+ 'create key',
138
139
  'create project',
139
140
  'create role',
140
141
  'diff models',
@@ -158,16 +159,19 @@ class ContensisShell {
158
159
  'list models',
159
160
  'list roles',
160
161
  'list webhooks',
161
- 'create key',
162
162
  'push block',
163
163
  'release block',
164
+ 'remove components',
165
+ 'remove contenttypes',
164
166
  'remove key',
165
167
  'remove entries',
166
- 'remove contenttypes',
167
- 'remove components',
168
+ 'remove role',
168
169
  'set project name',
169
170
  'set project description',
171
+ 'set role name',
172
+ 'set role description',
170
173
  'set role assignments',
174
+ 'set role enabled',
171
175
  'set role permissions'
172
176
  );
173
177
 
package/src/util/index.ts CHANGED
@@ -27,6 +27,9 @@ export const tryStringify = (obj: any) => {
27
27
  }
28
28
  };
29
29
 
30
+ export const isSysError = (error: any): error is Error =>
31
+ error?.message !== undefined && error.stack;
32
+
30
33
  export const isUuid = (str: string) => {
31
34
  // Regular expression to check if string is a valid UUID
32
35
  const regexExp =
@@ -2,8 +2,8 @@
2
2
  import chalk from 'chalk';
3
3
  import dateFormat from 'dateformat';
4
4
  import deepCleaner from 'deep-cleaner';
5
- import ProgressBar from 'progress';
6
- import { tryStringify } from '.';
5
+ // import ProgressBar from 'progress';
6
+ import { isSysError, tryStringify } from '.';
7
7
 
8
8
  type LogMethod = (content: string) => void;
9
9
  type LogErrorMethod = (content: string, err?: any, newline?: string) => void;
@@ -40,7 +40,11 @@ export class Logger {
40
40
  static error: LogErrorMethod = (content, err, newline = '\n') => {
41
41
  const message = `${Logger.getPrefix()} ${Logger.errorText(
42
42
  `${Logger.isUserTerminal ? '❌' : '[ERROR]'} ${content}${
43
- err ? `\n\n${JSON.stringify(err, null, 2)}` : ''
43
+ err
44
+ ? `\n\n${
45
+ isSysError(err) ? err.toString() : JSON.stringify(err, null, 2)
46
+ }`
47
+ : ''
44
48
  }`
45
49
  )}${newline}`;
46
50
  if (progress.active) progress.current.interrupt(message);
@@ -178,14 +182,18 @@ export class Logger {
178
182
  else Logger.objectRecurse(item, depth + 1, `${indent} `);
179
183
  } else Logger.raw(`${indent}${item}`);
180
184
  }
181
- } else
185
+ } else {
186
+ let pos = 0;
182
187
  for (const [key, value] of Object.entries(content)) {
188
+ const thisIndent =
189
+ pos === 0 ? `${indent.substring(0, indent.length - 2)}- ` : indent;
183
190
  if (Array.isArray(value)) {
191
+ if (value.length) Logger.raw(`${thisIndent}${chalk.bold.grey(key)}:`);
184
192
  for (const item of value) {
185
193
  if (item && typeof item === 'object') {
186
194
  if (Array.isArray(item) && depth > 3)
187
195
  Logger.raw(chalk.grey(`${indent} [${item.join(', ')}]`));
188
- else Logger.objectRecurse(item, depth + 1, `${indent}`);
196
+ else Logger.objectRecurse(item, depth + 1, `${indent} `);
189
197
  } else {
190
198
  Logger.raw(`${indent} ${item}`);
191
199
  }
@@ -196,9 +204,11 @@ export class Logger {
196
204
  Logger.objectRecurse(value, depth + 1, `${indent} `);
197
205
  // console.table(value);
198
206
  } else if (typeof value !== 'undefined' && value !== null) {
199
- Logger.raw(`${indent}${chalk.bold.grey(key)}: ${value}`);
207
+ Logger.raw(`${thisIndent}${chalk.bold.grey(key)}: ${value}`);
200
208
  }
209
+ pos++;
201
210
  }
211
+ }
202
212
  };
203
213
  static raw: LogMethod = (content: string) => {
204
214
  if (progress.active) progress.current.interrupt(content);
@@ -222,13 +232,14 @@ export const logError: LogErrorFunc = (
222
232
  };
223
233
 
224
234
  export const progress = {
235
+ current: { interrupt: (x: string) => {} },
225
236
  active: false,
226
- done: () => new ProgressBar('', 0),
227
- colours: { green: '\u001b[42m \u001b[0m', red: '\u001b[41m \u001b[0m' },
228
- current: new ProgressBar(`:bar`, {
229
- complete: '=',
230
- incomplete: ' ',
231
- width: 20,
232
- total: 100,
233
- }),
237
+ // done: () => new ProgressBar('', 0),
238
+ // colours: { green: '\u001b[42m \u001b[0m', red: '\u001b[41m \u001b[0m' },
239
+ // current: new ProgressBar(`:bar`, {
240
+ // complete: '=',
241
+ // incomplete: ' ',
242
+ // width: 20,
243
+ // total: 100,
244
+ // }),
234
245
  };
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const LIB_VERSION = "1.0.0-beta.63";
1
+ export const LIB_VERSION = "1.0.0-beta.65";