update-bag.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { flags } from '@oclif/command'
  2. import { updateStorageBucketsForBag } from '../../services/runtime/extrinsics'
  3. import ApiCommandBase from '../../command-base/ApiCommandBase'
  4. import logger from '../../services/logger'
  5. import ExitCodes from '../../command-base/ExitCodes'
  6. import _ from 'lodash'
  7. import { CLIError } from '@oclif/errors'
  8. // Custom 'integer array' oclif flag.
  9. const integerArrFlags = {
  10. integerArr: flags.build({
  11. parse: (value: string) => {
  12. const arr: number[] = value.split(',').map((v) => {
  13. if (!/^-?\d+$/.test(v)) {
  14. throw new CLIError(`Expected comma-separated integers, but received: ${value}`, {
  15. exit: ExitCodes.InvalidIntegerArray,
  16. })
  17. }
  18. return parseInt(v)
  19. })
  20. return arr
  21. },
  22. }),
  23. }
  24. /**
  25. * CLI command:
  26. * Updates bags-to-buckets relationships.
  27. *
  28. * @remarks
  29. * Storage working group leader command. Requires storage WG leader priviliges.
  30. * Shell command: "leader:update-bag"
  31. */
  32. export default class LeaderUpdateBag extends ApiCommandBase {
  33. static description = 'Add/remove a storage bucket from a bag (adds by default).'
  34. static flags = {
  35. add: integerArrFlags.integerArr({
  36. char: 'a',
  37. description: 'ID of a bucket to add to bag',
  38. default: [],
  39. }),
  40. remove: integerArrFlags.integerArr({
  41. char: 'r',
  42. description: 'ID of a bucket to remove from bag',
  43. default: [],
  44. }),
  45. bagId: ApiCommandBase.extraFlags.bagId({
  46. char: 'i',
  47. required: true,
  48. }),
  49. ...ApiCommandBase.flags,
  50. }
  51. async run(): Promise<void> {
  52. const { flags } = this.parse(LeaderUpdateBag)
  53. logger.info('Updating the bag...')
  54. if (flags.dev) {
  55. await this.ensureDevelopmentChain()
  56. }
  57. if (_.isEmpty(flags.add) && _.isEmpty(flags.remove)) {
  58. logger.error('No bucket ID provided.')
  59. this.exit(ExitCodes.InvalidParameters)
  60. }
  61. const account = this.getAccount(flags)
  62. const api = await this.getApi()
  63. const success = await updateStorageBucketsForBag(api, flags.bagId, account, flags.add, flags.remove)
  64. this.exitAfterRuntimeCall(success)
  65. }
  66. }