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 { CommanderStatic } from 'commander'
7 import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
8 import { createLogger, format, transports } from 'winston'
9 import { getMyUserInformation } from '@shared/extra-utils/users/users'
10 import { User, UserRole } from '@shared/models'
11 import { getAccessToken } from '@shared/extra-utils/users/login'
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.')
38 async function getSettings (): Promise<Settings> {
39 const defaultSettings = {
44 const data = await config.read()
46 return Object.keys(data).length === 0
51 async function getNetrc () {
52 const Netrc = require('netrc-parser').Netrc
54 const netrc = isTestInstance()
55 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
63 function writeSettings (settings: Settings) {
64 return config.write(settings)
67 function deleteSettings () {
71 function getRemoteObjectOrDie (
72 program: CommanderStatic,
75 ): { url: string, username: string, password: string } {
76 const options = program.opts()
78 if (!options.url || !options.username || !options.password) {
79 // No remote and we don't have program parameters: quit
80 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
81 if (!options.url) console.error('--url field is required.')
82 if (!options.username) console.error('--username field is required.')
83 if (!options.password) console.error('--password field is required.')
85 return process.exit(-1)
88 let url: string = options.url
89 let username: string = options.username
90 let password: string = options.password
92 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
94 const machine = netrc.machines[url]
96 if (!username && machine) username = machine.login
97 if (!password && machine) password = machine.password
99 return { url, username, password }
104 username: options.username,
105 password: options.password
109 function buildCommonVideoOptions (command: CommanderStatic) {
110 function list (val) {
111 return val.split(',')
115 .option('-n, --video-name <name>', 'Video name')
116 .option('-c, --category <category_number>', 'Category number')
117 .option('-l, --licence <licence_number>', 'Licence number')
118 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
119 .option('-t, --tags <tags>', 'Video tags', list)
120 .option('-N, --nsfw', 'Video is Not Safe For Work')
121 .option('-d, --video-description <description>', 'Video description')
122 .option('-P, --privacy <privacy_number>', 'Privacy')
123 .option('-C, --channel-name <channel_name>', 'Channel name')
124 .option('--no-comments-enabled', 'Disable video comments')
125 .option('-s, --support <support>', 'Video support text')
126 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
127 .option('--no-download-enabled', 'Disable video download')
128 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
131 async function buildVideoAttributesFromCommander (url: string, command: CommanderStatic, defaultAttributes: any = {}) {
132 const options = command.opts()
134 const defaultBooleanAttributes = {
136 commentsEnabled: true,
137 downloadEnabled: true,
138 waitTranscoding: true
141 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
143 for (const key of Object.keys(defaultBooleanAttributes)) {
144 if (options[key] !== undefined) {
145 booleanAttributes[key] = options[key]
146 } else if (defaultAttributes[key] !== undefined) {
147 booleanAttributes[key] = defaultAttributes[key]
149 booleanAttributes[key] = defaultBooleanAttributes[key]
153 const videoAttributes = {
154 name: options.videoName || defaultAttributes.name,
155 category: options.category || defaultAttributes.category || undefined,
156 licence: options.licence || defaultAttributes.licence || undefined,
157 language: options.language || defaultAttributes.language || undefined,
158 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
159 support: options.support || defaultAttributes.support || undefined,
160 description: options.videoDescription || defaultAttributes.description || undefined,
161 tags: options.tags || defaultAttributes.tags || undefined
164 Object.assign(videoAttributes, booleanAttributes)
166 if (options.channelName) {
167 const res = await getVideoChannel(url, options.channelName)
168 const videoChannel: VideoChannel = res.body
170 Object.assign(videoAttributes, { channelId: videoChannel.id })
172 if (!videoAttributes.support && videoChannel.support) {
173 Object.assign(videoAttributes, { support: videoChannel.support })
177 return videoAttributes
180 function getServerCredentials (program: CommanderStatic) {
181 return Promise.all([ getSettings(), getNetrc() ])
182 .then(([ settings, netrc ]) => {
183 return getRemoteObjectOrDie(program, settings, netrc)
187 function getLogger (logLevel = 'info') {
201 const logger = createLogger({
203 format: format.combine(
208 new (transports.Console)({
217 // ---------------------------------------------------------------------------
224 getRemoteObjectOrDie,
228 getServerCredentials,
230 buildCommonVideoOptions,
231 buildVideoAttributesFromCommander,