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 { assignCommands, ServerInfo } from '@shared/extra-utils'
6 import { getAccessToken } from '@shared/extra-utils/users/login'
7 import { getMyUserInformation } from '@shared/extra-utils/users/users'
8 import { User, UserRole } from '@shared/models'
9 import { root } from '../../shared/extra-utils/miscs/miscs'
10 import { VideoPrivacy } from '../../shared/models/videos'
11 import { getAppNumber, isTestInstance } from '../helpers/core-utils'
13 let configName = 'PeerTube/CLI'
14 if (isTestInstance()) configName += `-${getAppNumber()}`
16 const config = require('application-config')(configName)
18 const version = require('../../../package.json').version
20 async function getAdminTokenOrDie (url: string, username: string, password: string) {
21 const accessToken = await getAccessToken(url, username, password)
22 const resMe = await getMyUserInformation(url, accessToken)
23 const me: User = resMe.body
25 if (me.role !== UserRole.ADMINISTRATOR) {
26 console.error('You must be an administrator.')
33 async function getAccessTokenOrDie (url: string, username: string, password: string) {
34 return getAccessToken(url, username, password)
42 async function getSettings (): Promise<Settings> {
43 const defaultSettings = {
48 const data = await config.read()
50 return Object.keys(data).length === 0
55 async function getNetrc () {
56 const Netrc = require('netrc-parser').Netrc
58 const netrc = isTestInstance()
59 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
67 function writeSettings (settings: Settings) {
68 return config.write(settings)
71 function deleteSettings () {
75 function getRemoteObjectOrDie (
79 ): { url: string, username: string, password: string } {
80 const options = program.opts()
82 if (!options.url || !options.username || !options.password) {
83 // No remote and we don't have program parameters: quit
84 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
85 if (!options.url) console.error('--url field is required.')
86 if (!options.username) console.error('--username field is required.')
87 if (!options.password) console.error('--password field is required.')
89 return process.exit(-1)
92 let url: string = options.url
93 let username: string = options.username
94 let password: string = options.password
96 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
98 const machine = netrc.machines[url]
100 if (!username && machine) username = machine.login
101 if (!password && machine) password = machine.password
103 return { url, username, password }
108 username: options.username,
109 password: options.password
113 function buildCommonVideoOptions (command: Command) {
114 function list (val) {
115 return val.split(',')
119 .option('-n, --video-name <name>', 'Video name')
120 .option('-c, --category <category_number>', 'Category number')
121 .option('-l, --licence <licence_number>', 'Licence number')
122 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
123 .option('-t, --tags <tags>', 'Video tags', list)
124 .option('-N, --nsfw', 'Video is Not Safe For Work')
125 .option('-d, --video-description <description>', 'Video description')
126 .option('-P, --privacy <privacy_number>', 'Privacy')
127 .option('-C, --channel-name <channel_name>', 'Channel name')
128 .option('--no-comments-enabled', 'Disable video comments')
129 .option('-s, --support <support>', 'Video support text')
130 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
131 .option('--no-download-enabled', 'Disable video download')
132 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
135 async function buildVideoAttributesFromCommander (server: ServerInfo, command: Command, defaultAttributes: any = {}) {
136 const options = command.opts()
138 const defaultBooleanAttributes = {
140 commentsEnabled: true,
141 downloadEnabled: true,
142 waitTranscoding: true
145 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
147 for (const key of Object.keys(defaultBooleanAttributes)) {
148 if (options[key] !== undefined) {
149 booleanAttributes[key] = options[key]
150 } else if (defaultAttributes[key] !== undefined) {
151 booleanAttributes[key] = defaultAttributes[key]
153 booleanAttributes[key] = defaultBooleanAttributes[key]
157 const videoAttributes = {
158 name: options.videoName || defaultAttributes.name,
159 category: options.category || defaultAttributes.category || undefined,
160 licence: options.licence || defaultAttributes.licence || undefined,
161 language: options.language || defaultAttributes.language || undefined,
162 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
163 support: options.support || defaultAttributes.support || undefined,
164 description: options.videoDescription || defaultAttributes.description || undefined,
165 tags: options.tags || defaultAttributes.tags || undefined
168 Object.assign(videoAttributes, booleanAttributes)
170 if (options.channelName) {
171 const videoChannel = await server.channelsCommand.get({ channelName: options.channelName })
173 Object.assign(videoAttributes, { channelId: videoChannel.id })
175 if (!videoAttributes.support && videoChannel.support) {
176 Object.assign(videoAttributes, { support: videoChannel.support })
180 return videoAttributes
183 function getServerCredentials (program: Command) {
184 return Promise.all([ getSettings(), getNetrc() ])
185 .then(([ settings, netrc ]) => {
186 return getRemoteObjectOrDie(program, settings, netrc)
190 function buildServer (url: string, accessToken?: string): ServerInfo {
191 const server = { url, accessToken, internalServerNumber: undefined }
192 assignCommands(server)
197 function getLogger (logLevel = 'info') {
211 const logger = createLogger({
213 format: format.combine(
218 new (transports.Console)({
227 // ---------------------------------------------------------------------------
234 getRemoteObjectOrDie,
238 getServerCredentials,
240 buildCommonVideoOptions,
241 buildVideoAttributesFromCommander,