2
1

peertube-auth.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as program from 'commander'
  2. import * as prompt from 'prompt'
  3. import { getSettings, writeSettings, getNetrc } from './cli'
  4. import { isHostValid } from '../helpers/custom-validators/servers'
  5. import { isUserUsernameValid } from '../helpers/custom-validators/users'
  6. const Table = require('cli-table')
  7. async function delInstance (url: string) {
  8. const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
  9. const index = settings.remotes.indexOf(url)
  10. settings.remotes.splice(index)
  11. if (settings.default === index) settings.default = -1
  12. await writeSettings(settings)
  13. delete netrc.machines[url]
  14. await netrc.save()
  15. }
  16. async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
  17. const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
  18. if (settings.remotes.indexOf(url) === -1) {
  19. settings.remotes.push(url)
  20. }
  21. if (isDefault || settings.remotes.length === 1) {
  22. settings.default = settings.remotes.length - 1
  23. }
  24. await writeSettings(settings)
  25. netrc.machines[url] = { login: username, password }
  26. await netrc.save()
  27. }
  28. function isURLaPeerTubeInstance (url: string) {
  29. return isHostValid(url) || (url.includes('localhost'))
  30. }
  31. program
  32. .name('auth')
  33. .usage('[command] [options]')
  34. program
  35. .command('add')
  36. .description('remember your accounts on remote instances for easier use')
  37. .option('-u, --url <url>', 'Server url')
  38. .option('-U, --username <username>', 'Username')
  39. .option('-p, --password <token>', 'Password')
  40. .option('--default', 'add the entry as the new default')
  41. .action(options => {
  42. prompt.override = options
  43. prompt.start()
  44. prompt.get({
  45. properties: {
  46. url: {
  47. description: 'instance url',
  48. conform: (value) => isURLaPeerTubeInstance(value),
  49. required: true
  50. },
  51. username: {
  52. conform: (value) => isUserUsernameValid(value),
  53. message: 'Name must be only letters, spaces, or dashes',
  54. required: true
  55. },
  56. password: {
  57. hidden: true,
  58. replace: '*',
  59. required: true
  60. }
  61. }
  62. }, async (_, result) => {
  63. await setInstance(result.url, result.username, result.password, program['default'])
  64. process.exit(0)
  65. })
  66. })
  67. program
  68. .command('del <url>')
  69. .description('unregisters a remote instance')
  70. .action(async url => {
  71. await delInstance(url)
  72. process.exit(0)
  73. })
  74. program
  75. .command('list')
  76. .description('lists registered remote instances')
  77. .action(async () => {
  78. const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
  79. const table = new Table({
  80. head: ['instance', 'login'],
  81. colWidths: [30, 30]
  82. })
  83. settings.remotes.forEach(element => {
  84. if (!netrc.machines[element]) return
  85. table.push([
  86. element,
  87. netrc.machines[element].login
  88. ])
  89. })
  90. console.log(table.toString())
  91. process.exit(0)
  92. })
  93. program
  94. .command('set-default <url>')
  95. .description('set an existing entry as default')
  96. .action(async url => {
  97. const settings = await getSettings()
  98. const instanceExists = settings.remotes.indexOf(url) !== -1
  99. if (instanceExists) {
  100. settings.default = settings.remotes.indexOf(url)
  101. await writeSettings(settings)
  102. process.exit(0)
  103. } else {
  104. console.log('<url> is not a registered instance.')
  105. process.exit(-1)
  106. }
  107. })
  108. program.on('--help', function () {
  109. console.log(' Examples:')
  110. console.log()
  111. console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
  112. console.log(' $ peertube add -u peertube.cpy.re -U root')
  113. console.log(' $ peertube list')
  114. console.log(' $ peertube del peertube.cpy.re')
  115. console.log()
  116. })
  117. if (!process.argv.slice(2).length) {
  118. program.outputHelp()
  119. }
  120. program.parse(process.argv)