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 const manualOptionMode = options.url || options.username || options.password
78 // Check parameters validity
79 if (manualOptionMode || settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
82 for (const key of [ 'url', 'username', 'password' ]) {
84 console.error(`--${key} field is required`)
89 if (exit) process.exit(-1)
92 if (!manualOptionMode) {
93 let url: string = options.url
94 let username: string = options.username
95 let password: string = options.password
97 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
99 const machine = netrc.machines[url]
101 if (!username && machine) username = machine.login
102 if (!password && machine) password = machine.password
104 return { url, username, password }
109 username: options.username,
110 password: options.password
114 function buildCommonVideoOptions (command: Command) {
115 function list (val) {
116 return val.split(',')
120 .option('-n, --video-name <name>', 'Video name')
121 .option('-c, --category <category_number>', 'Category number')
122 .option('-l, --licence <licence_number>', 'Licence number')
123 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
124 .option('-t, --tags <tags>', 'Video tags', list)
125 .option('-N, --nsfw', 'Video is Not Safe For Work')
126 .option('-d, --video-description <description>', 'Video description')
127 .option('-P, --privacy <privacy_number>', 'Privacy')
128 .option('-C, --channel-name <channel_name>', 'Channel name')
129 .option('--no-comments-enabled', 'Disable video comments')
130 .option('-s, --support <support>', 'Video support text')
131 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
132 .option('--no-download-enabled', 'Disable video download')
133 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
136 async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
137 const options = command.opts()
139 const defaultBooleanAttributes = {
141 commentsEnabled: true,
142 downloadEnabled: true,
143 waitTranscoding: true
146 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
148 for (const key of Object.keys(defaultBooleanAttributes)) {
149 if (options[key] !== undefined) {
150 booleanAttributes[key] = options[key]
151 } else if (defaultAttributes[key] !== undefined) {
152 booleanAttributes[key] = defaultAttributes[key]
154 booleanAttributes[key] = defaultBooleanAttributes[key]
158 const videoAttributes = {
159 name: options.videoName || defaultAttributes.name,
160 category: options.category || defaultAttributes.category || undefined,
161 licence: options.licence || defaultAttributes.licence || undefined,
162 language: options.language || defaultAttributes.language || undefined,
163 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
164 support: options.support || defaultAttributes.support || undefined,
165 description: options.videoDescription || defaultAttributes.description || undefined,
166 tags: options.tags || defaultAttributes.tags || undefined
169 Object.assign(videoAttributes, booleanAttributes)
171 if (options.channelName) {
172 const videoChannel = await server.channels.get({ channelName: options.channelName })
174 Object.assign(videoAttributes, { channelId: videoChannel.id })
176 if (!videoAttributes.support && videoChannel.support) {
177 Object.assign(videoAttributes, { support: videoChannel.support })
181 return videoAttributes
184 function getServerCredentials (program: Command) {
185 return Promise.all([ getSettings(), getNetrc() ])
186 .then(([ settings, netrc ]) => {
187 return getRemoteObjectOrDie(program, settings, netrc)
191 function buildServer (url: string) {
193 return new PeerTubeServer({ url })
196 async function assignToken (server: PeerTubeServer, username: string, password: string) {
197 const bodyClient = await server.login.getClient()
198 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
200 const body = await server.login.login({ client, user: { username, password } })
202 server.accessToken = body.access_token
205 function getLogger (logLevel = 'info') {
219 const logger = createLogger({
221 format: format.combine(
226 new (transports.Console)({
235 // ---------------------------------------------------------------------------
242 getRemoteObjectOrDie,
246 getServerCredentials,
248 buildCommonVideoOptions,
249 buildVideoAttributesFromCommander,