cli.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { Netrc } from 'netrc-parser'
  2. import { getAppNumber, isTestInstance } from '../helpers/core-utils'
  3. import { join } from 'path'
  4. import { root } from '../../shared/extra-utils/miscs/miscs'
  5. import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
  6. import { CommanderStatic } from 'commander'
  7. import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
  8. import { createLogger, format, transports } from 'winston'
  9. import { getMyUserInformation } from '@shared/extra-utils/users/users'
  10. import { User, UserRole } from '@shared/models'
  11. import { getAccessToken } from '@shared/extra-utils/users/login'
  12. let configName = 'PeerTube/CLI'
  13. if (isTestInstance()) configName += `-${getAppNumber()}`
  14. const config = require('application-config')(configName)
  15. const version = require('../../../package.json').version
  16. async function getAdminTokenOrDie (url: string, username: string, password: string) {
  17. const accessToken = await getAccessToken(url, username, password)
  18. const resMe = await getMyUserInformation(url, accessToken)
  19. const me: User = resMe.body
  20. if (me.role !== UserRole.ADMINISTRATOR) {
  21. console.error('You must be an administrator.')
  22. process.exit(-1)
  23. }
  24. return accessToken
  25. }
  26. interface Settings {
  27. remotes: any[]
  28. default: number
  29. }
  30. async function getSettings (): Promise<Settings> {
  31. const defaultSettings = {
  32. remotes: [],
  33. default: -1
  34. }
  35. const data = await config.read()
  36. return Object.keys(data).length === 0
  37. ? defaultSettings
  38. : data
  39. }
  40. async function getNetrc () {
  41. const Netrc = require('netrc-parser').Netrc
  42. const netrc = isTestInstance()
  43. ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
  44. : new Netrc()
  45. await netrc.load()
  46. return netrc
  47. }
  48. function writeSettings (settings: Settings) {
  49. return config.write(settings)
  50. }
  51. function deleteSettings () {
  52. return config.trash()
  53. }
  54. function getRemoteObjectOrDie (
  55. program: any,
  56. settings: Settings,
  57. netrc: Netrc
  58. ): { url: string, username: string, password: string } {
  59. if (!program['url'] || !program['username'] || !program['password']) {
  60. // No remote and we don't have program parameters: quit
  61. if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
  62. if (!program['url']) console.error('--url field is required.')
  63. if (!program['username']) console.error('--username field is required.')
  64. if (!program['password']) console.error('--password field is required.')
  65. return process.exit(-1)
  66. }
  67. let url: string = program['url']
  68. let username: string = program['username']
  69. let password: string = program['password']
  70. if (!url && settings.default !== -1) url = settings.remotes[settings.default]
  71. const machine = netrc.machines[url]
  72. if (!username && machine) username = machine.login
  73. if (!password && machine) password = machine.password
  74. return { url, username, password }
  75. }
  76. return {
  77. url: program['url'],
  78. username: program['username'],
  79. password: program['password']
  80. }
  81. }
  82. function buildCommonVideoOptions (command: CommanderStatic) {
  83. function list (val) {
  84. return val.split(',')
  85. }
  86. return command
  87. .option('-n, --video-name <name>', 'Video name')
  88. .option('-c, --category <category_number>', 'Category number')
  89. .option('-l, --licence <licence_number>', 'Licence number')
  90. .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
  91. .option('-t, --tags <tags>', 'Video tags', list)
  92. .option('-N, --nsfw', 'Video is Not Safe For Work')
  93. .option('-d, --video-description <description>', 'Video description')
  94. .option('-P, --privacy <privacy_number>', 'Privacy')
  95. .option('-C, --channel-name <channel_name>', 'Channel name')
  96. .option('--no-comments-enabled', 'Disable video comments')
  97. .option('-s, --support <support>', 'Video support text')
  98. .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
  99. .option('--no-download-enabled', 'Disable video download')
  100. .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
  101. }
  102. async function buildVideoAttributesFromCommander (url: string, command: CommanderStatic, defaultAttributes: any = {}) {
  103. const defaultBooleanAttributes = {
  104. nsfw: false,
  105. commentsEnabled: true,
  106. downloadEnabled: true,
  107. waitTranscoding: true
  108. }
  109. const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
  110. for (const key of Object.keys(defaultBooleanAttributes)) {
  111. if (command[key] !== undefined) {
  112. booleanAttributes[key] = command[key]
  113. } else if (defaultAttributes[key] !== undefined) {
  114. booleanAttributes[key] = defaultAttributes[key]
  115. } else {
  116. booleanAttributes[key] = defaultBooleanAttributes[key]
  117. }
  118. }
  119. const videoAttributes = {
  120. name: command['videoName'] || defaultAttributes.name,
  121. category: command['category'] || defaultAttributes.category || undefined,
  122. licence: command['licence'] || defaultAttributes.licence || undefined,
  123. language: command['language'] || defaultAttributes.language || undefined,
  124. privacy: command['privacy'] || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
  125. support: command['support'] || defaultAttributes.support || undefined,
  126. description: command['videoDescription'] || defaultAttributes.description || undefined,
  127. tags: command['tags'] || defaultAttributes.tags || undefined
  128. }
  129. Object.assign(videoAttributes, booleanAttributes)
  130. if (command['channelName']) {
  131. const res = await getVideoChannel(url, command['channelName'])
  132. const videoChannel: VideoChannel = res.body
  133. Object.assign(videoAttributes, { channelId: videoChannel.id })
  134. if (!videoAttributes.support && videoChannel.support) {
  135. Object.assign(videoAttributes, { support: videoChannel.support })
  136. }
  137. }
  138. return videoAttributes
  139. }
  140. function getServerCredentials (program: any) {
  141. return Promise.all([ getSettings(), getNetrc() ])
  142. .then(([ settings, netrc ]) => {
  143. return getRemoteObjectOrDie(program, settings, netrc)
  144. })
  145. }
  146. function getLogger (logLevel = 'info') {
  147. const logLevels = {
  148. 0: 0,
  149. error: 0,
  150. 1: 1,
  151. warn: 1,
  152. 2: 2,
  153. info: 2,
  154. 3: 3,
  155. verbose: 3,
  156. 4: 4,
  157. debug: 4
  158. }
  159. const logger = createLogger({
  160. levels: logLevels,
  161. format: format.combine(
  162. format.splat(),
  163. format.simple()
  164. ),
  165. transports: [
  166. new (transports.Console)({
  167. level: logLevel
  168. })
  169. ]
  170. })
  171. return logger
  172. }
  173. // ---------------------------------------------------------------------------
  174. export {
  175. version,
  176. getLogger,
  177. getSettings,
  178. getNetrc,
  179. getRemoteObjectOrDie,
  180. writeSettings,
  181. deleteSettings,
  182. getServerCredentials,
  183. buildCommonVideoOptions,
  184. buildVideoAttributesFromCommander,
  185. getAdminTokenOrDie
  186. }