cli.ts 6.3 KB

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