cli.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. import { RuntimeApi } from '@joystream/storage-runtime-api'
  20. import meow from 'meow'
  21. import _ from 'lodash'
  22. // Commands
  23. import * as dev from './commands/dev'
  24. import { HeadCommand } from './commands/head'
  25. import { DownloadCommand } from './commands/download'
  26. import { UploadCommand } from './commands/upload'
  27. // Parse CLI
  28. const FLAG_DEFINITIONS = {
  29. // TODO: current version of meow doesn't support subcommands. We should consider a migration to yargs or oclif.
  30. }
  31. const usage = `
  32. Usage:
  33. $ storage-cli command [arguments..]
  34. Commands:
  35. upload Upload a file to the Joystream Network. Requires a
  36. source file path to upload, data object ID, member ID and account key file with
  37. pass phrase to unlock it.
  38. download Retrieve a file. Requires a storage node URL and a content
  39. ID, as well as an output filename.
  40. head Send a HEAD request for a file, and print headers.
  41. Requires a storage node URL and a content ID.
  42. Dev Commands: Commands to run on a development chain.
  43. dev-init Setup chain with Alice as lead and storage provider.
  44. dev-check Check the chain is setup with Alice as lead and storage provider.
  45. vstore-init Initialize versioned store, Requires SURI of ContentWorking Lead.
  46. Type 'storage-cli command' for the exact command usage examples.
  47. `
  48. const cli = meow(usage, { flags: FLAG_DEFINITIONS })
  49. // Shows a message, CLI general usage and exits.
  50. function showUsageAndExit(message: string) {
  51. console.log(message)
  52. console.log(usage)
  53. process.exit(1)
  54. }
  55. const commands = {
  56. // add Alice well known account as storage provider
  57. 'dev-init': async (api) => {
  58. return dev.init(api)
  59. },
  60. // Checks that the setup done by dev-init command was successful
  61. 'dev-check': async (api) => {
  62. return dev.check(api)
  63. },
  64. // Uploads the file to the system. Registers new data object in the runtime, obtains proper colossus instance URL.
  65. upload: async (
  66. api: any,
  67. filePath: string,
  68. dataObjectTypeId: string,
  69. memberId: string,
  70. keyFile: string,
  71. passPhrase: string
  72. ) => {
  73. const uploadCmd = new UploadCommand(api, filePath, dataObjectTypeId, memberId, keyFile, passPhrase)
  74. await uploadCmd.run()
  75. },
  76. // needs to be updated to take a content id and resolve it a potential set
  77. // of providers that has it, and select one (possibly try more than one provider)
  78. // to fetch it from the get api url of a provider..
  79. download: async (api: any, url: string, contentId: string, filePath: string) => {
  80. const downloadCmd = new DownloadCommand(api, url, contentId, filePath)
  81. await downloadCmd.run()
  82. },
  83. // Shows asset information derived from response headers.
  84. // Accepts colossus URL and content ID.
  85. head: async (api: any, storageNodeUrl: string, contentId: string) => {
  86. const headCmd = new HeadCommand(api, storageNodeUrl, contentId)
  87. await headCmd.run()
  88. },
  89. }
  90. // Entry point.
  91. export async function main() {
  92. const api = await RuntimeApi.create({ retries: 3 })
  93. // Simple CLI commands
  94. const command = cli.input[0]
  95. if (!command) {
  96. showUsageAndExit('Enter the command, please.')
  97. }
  98. if (Object.prototype.hasOwnProperty.call(commands, command)) {
  99. // Command recognized
  100. const args = _.clone(cli.input).slice(1)
  101. try {
  102. await commands[command](api, ...args)
  103. } catch (err) {
  104. console.error('Command Failed:', err)
  105. process.exit(-1)
  106. }
  107. } else {
  108. showUsageAndExit(`Command "${command}" not recognized.`)
  109. }
  110. }