1 import { Command } from 'commander'
2 import { Netrc } from 'netrc-parser'
3 import { join } from 'path'
4 import { createLogger, format, transports } from 'winston'
5 import { loadLanguages } from '@server/initializers/constants'
6 import { root } from '@shared/core-utils'
7 import { UserRole } from '@shared/models'
8 import { PeerTubeServer } from '@shared/server-commands'
9 import { VideoPrivacy } from '../../shared/models/videos'
10 import { getAppNumber, isTestInstance } from '../helpers/core-utils'
12 let configName = 'PeerTube/CLI'
13 if (isTestInstance()) configName += `-${getAppNumber()}`
15 const config = require('application-config')(configName)
17 const version = require(join(root(), 'package.json')).version
19 async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
20 const token = await server.login.getAccessToken(username, password)
21 const me = await server.users.getMyInfo({ token })
23 if (me.role !== UserRole.ADMINISTRATOR) {
24 console.error('You must be an administrator.')
36 async function getSettings (): Promise<Settings> {
37 const defaultSettings = {
42 const data = await config.read()
44 return Object.keys(data).length === 0
49 async function getNetrc () {
50 const Netrc = require('netrc-parser').Netrc
52 const netrc = isTestInstance()
53 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
61 function writeSettings (settings: Settings) {
62 return config.write(settings)
65 function deleteSettings () {
69 function getRemoteObjectOrDie (
73 ): { url: string, username: string, password: string } {
74 const options = program.opts()
76 if (!options.url || !options.username || !options.password) {
77 // No remote and we don't have program parameters: quit
78 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
79 if (!options.url) console.error('--url field is required.')
80 if (!options.username) console.error('--username field is required.')
81 if (!options.password) console.error('--password field is required.')
83 return process.exit(-1)
86 let url: string = options.url
87 let username: string = options.username
88 let password: string = options.password
90 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
92 const machine = netrc.machines[url]
94 if (!username && machine) username = machine.login
95 if (!password && machine) password = machine.password
97 return { url, username, password }
102 username: options.username,
103 password: options.password
107 function buildCommonVideoOptions (command: Command) {
108 function list (val) {
109 return val.split(',')
113 .option('-n, --video-name <name>', 'Video name')
114 .option('-c, --category <category_number>', 'Category number')
115 .option('-l, --licence <licence_number>', 'Licence number')
116 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
117 .option('-t, --tags <tags>', 'Video tags', list)
118 .option('-N, --nsfw', 'Video is Not Safe For Work')
119 .option('-d, --video-description <description>', 'Video description')
120 .option('-P, --privacy <privacy_number>', 'Privacy')
121 .option('-C, --channel-name <channel_name>', 'Channel name')
122 .option('--no-comments-enabled', 'Disable video comments')
123 .option('-s, --support <support>', 'Video support text')
124 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
125 .option('--no-download-enabled', 'Disable video download')
126 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
129 async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
130 const options = command.opts()
132 const defaultBooleanAttributes = {
134 commentsEnabled: true,
135 downloadEnabled: true,
136 waitTranscoding: true
139 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
141 for (const key of Object.keys(defaultBooleanAttributes)) {
142 if (options[key] !== undefined) {
143 booleanAttributes[key] = options[key]
144 } else if (defaultAttributes[key] !== undefined) {
145 booleanAttributes[key] = defaultAttributes[key]
147 booleanAttributes[key] = defaultBooleanAttributes[key]
151 const videoAttributes = {
152 name: options.videoName || defaultAttributes.name,
153 category: options.category || defaultAttributes.category || undefined,
154 licence: options.licence || defaultAttributes.licence || undefined,
155 language: options.language || defaultAttributes.language || undefined,
156 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
157 support: options.support || defaultAttributes.support || undefined,
158 description: options.videoDescription || defaultAttributes.description || undefined,
159 tags: options.tags || defaultAttributes.tags || undefined
162 Object.assign(videoAttributes, booleanAttributes)
164 if (options.channelName) {
165 const videoChannel = await server.channels.get({ channelName: options.channelName })
167 Object.assign(videoAttributes, { channelId: videoChannel.id })
169 if (!videoAttributes.support && videoChannel.support) {
170 Object.assign(videoAttributes, { support: videoChannel.support })
174 return videoAttributes
177 function getServerCredentials (program: Command) {
178 return Promise.all([ getSettings(), getNetrc() ])
179 .then(([ settings, netrc ]) => {
180 return getRemoteObjectOrDie(program, settings, netrc)
184 function buildServer (url: string) {
186 return new PeerTubeServer({ url })
189 async function assignToken (server: PeerTubeServer, username: string, password: string) {
190 const bodyClient = await server.login.getClient()
191 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
193 const body = await server.login.login({ client, user: { username, password } })
195 server.accessToken = body.access_token
198 function getLogger (logLevel = 'info') {
212 const logger = createLogger({
214 format: format.combine(
219 new (transports.Console)({
228 // ---------------------------------------------------------------------------
235 getRemoteObjectOrDie,
239 getServerCredentials,
241 buildCommonVideoOptions,
242 buildVideoAttributesFromCommander,