cli.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // dev accounts are automatically loaded, no need to add explicitly to keyring using loadIdentity(api)
  59. return dev.init(api)
  60. },
  61. // Checks that the setup done by dev-init command was successful
  62. 'dev-check': async (api) => {
  63. // dev accounts are automatically loaded, no need to add explicitly to keyring using loadIdentity(api)
  64. return dev.check(api)
  65. },
  66. // Runs the versioned store initialization with given SURI of content working group lead
  67. 'vstore-init': async (api, suri: string) => {
  68. return dev.vstoreInit(api, suri)
  69. },
  70. // Uploads the file to the system. Registers new data object in the runtime, obtains proper colossus instance URL.
  71. upload: async (
  72. api: any,
  73. filePath: string,
  74. dataObjectTypeId: string,
  75. keyFile: string,
  76. passPhrase: string,
  77. memberId: string
  78. ) => {
  79. const uploadCmd = new UploadCommand(api, filePath, dataObjectTypeId, keyFile, passPhrase, memberId)
  80. await uploadCmd.run()
  81. },
  82. // needs to be updated to take a content id and resolve it a potential set
  83. // of providers that has it, and select one (possibly try more than one provider)
  84. // to fetch it from the get api url of a provider..
  85. download: async (api: any, url: string, contentId: string, filePath: string) => {
  86. const downloadCmd = new DownloadCommand(api, url, contentId, filePath)
  87. await downloadCmd.run()
  88. },
  89. // Shows asset information derived from response headers.
  90. // Accepts colossus URL and content ID.
  91. head: async (api: any, storageNodeUrl: string, contentId: string) => {
  92. const headCmd = new HeadCommand(api, storageNodeUrl, contentId)
  93. await headCmd.run()
  94. },
  95. }
  96. // Entry point.
  97. export async function main() {
  98. const api = await RuntimeApi.create()
  99. // Simple CLI commands
  100. const command = cli.input[0]
  101. if (!command) {
  102. showUsageAndExit('Enter the command, please.')
  103. }
  104. if (Object.prototype.hasOwnProperty.call(commands, command)) {
  105. // Command recognized
  106. const args = _.clone(cli.input).slice(1)
  107. await commands[command](api, ...args)
  108. } else {
  109. showUsageAndExit(`Command "${command}" not recognized.`)
  110. }
  111. }