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 { PeerTubeServer } from '@shared/extra-utils'
6 import { UserRole } from '@shared/models'
7 import { VideoPrivacy } from '../../shared/models/videos'
8 import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
10 let configName = 'PeerTube/CLI'
11 if (isTestInstance()) configName += `-${getAppNumber()}`
13 const config = require('application-config')(configName)
15 const version = require('../../../package.json').version
17 async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
18 const token = await server.login.getAccessToken(username, password)
19 const me = await server.users.getMyInfo({ token })
21 if (me.role !== UserRole.ADMINISTRATOR) {
22 console.error('You must be an administrator.')
34 async function getSettings (): Promise<Settings> {
35 const defaultSettings = {
40 const data = await config.read()
42 return Object.keys(data).length === 0
47 async function getNetrc () {
48 const Netrc = require('netrc-parser').Netrc
50 const netrc = isTestInstance()
51 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
59 function writeSettings (settings: Settings) {
60 return config.write(settings)
63 function deleteSettings () {
67 function getRemoteObjectOrDie (
71 ): { url: string, username: string, password: string } {
72 const options = program.opts()
74 if (!options.url || !options.username || !options.password) {
75 // No remote and we don't have program parameters: quit
76 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
77 if (!options.url) console.error('--url field is required.')
78 if (!options.username) console.error('--username field is required.')
79 if (!options.password) console.error('--password field is required.')
81 return process.exit(-1)
84 let url: string = options.url
85 let username: string = options.username
86 let password: string = options.password
88 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
90 const machine = netrc.machines[url]
92 if (!username && machine) username = machine.login
93 if (!password && machine) password = machine.password
95 return { url, username, password }
100 username: options.username,
101 password: options.password
105 function buildCommonVideoOptions (command: Command) {
106 function list (val) {
107 return val.split(',')
111 .option('-n, --video-name <name>', 'Video name')
112 .option('-c, --category <category_number>', 'Category number')
113 .option('-l, --licence <licence_number>', 'Licence number')
114 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
115 .option('-t, --tags <tags>', 'Video tags', list)
116 .option('-N, --nsfw', 'Video is Not Safe For Work')
117 .option('-d, --video-description <description>', 'Video description')
118 .option('-P, --privacy <privacy_number>', 'Privacy')
119 .option('-C, --channel-name <channel_name>', 'Channel name')
120 .option('--no-comments-enabled', 'Disable video comments')
121 .option('-s, --support <support>', 'Video support text')
122 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
123 .option('--no-download-enabled', 'Disable video download')
124 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
127 async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
128 const options = command.opts()
130 const defaultBooleanAttributes = {
132 commentsEnabled: true,
133 downloadEnabled: true,
134 waitTranscoding: true
137 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
139 for (const key of Object.keys(defaultBooleanAttributes)) {
140 if (options[key] !== undefined) {
141 booleanAttributes[key] = options[key]
142 } else if (defaultAttributes[key] !== undefined) {
143 booleanAttributes[key] = defaultAttributes[key]
145 booleanAttributes[key] = defaultBooleanAttributes[key]
149 const videoAttributes = {
150 name: options.videoName || defaultAttributes.name,
151 category: options.category || defaultAttributes.category || undefined,
152 licence: options.licence || defaultAttributes.licence || undefined,
153 language: options.language || defaultAttributes.language || undefined,
154 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
155 support: options.support || defaultAttributes.support || undefined,
156 description: options.videoDescription || defaultAttributes.description || undefined,
157 tags: options.tags || defaultAttributes.tags || undefined
160 Object.assign(videoAttributes, booleanAttributes)
162 if (options.channelName) {
163 const videoChannel = await server.channels.get({ channelName: options.channelName })
165 Object.assign(videoAttributes, { channelId: videoChannel.id })
167 if (!videoAttributes.support && videoChannel.support) {
168 Object.assign(videoAttributes, { support: videoChannel.support })
172 return videoAttributes
175 function getServerCredentials (program: Command) {
176 return Promise.all([ getSettings(), getNetrc() ])
177 .then(([ settings, netrc ]) => {
178 return getRemoteObjectOrDie(program, settings, netrc)
182 function buildServer (url: string) {
183 return new PeerTubeServer({ url })
186 async function assignToken (server: PeerTubeServer, username: string, password: string) {
187 const bodyClient = await server.login.getClient()
188 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
190 const body = await server.login.login({ client, user: { username, password } })
192 server.accessToken = body.access_token
195 function getLogger (logLevel = 'info') {
209 const logger = createLogger({
211 format: format.combine(
216 new (transports.Console)({
225 // ---------------------------------------------------------------------------
232 getRemoteObjectOrDie,
236 getServerCredentials,
238 buildCommonVideoOptions,
239 buildVideoAttributesFromCommander,