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 { Command } from 'commander'
7 import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
8 import { createLogger, format, transports } from 'winston'
9 import { getAccessToken, getMyUserInformation } from '@shared/extra-utils'
10 import { User, UserRole } from '@shared/models'
12 let configName = 'PeerTube/CLI'
13 if (isTestInstance()) configName += `-${getAppNumber()}`
15 const config = require('application-config')(configName)
17 const version = require('../../../package.json').version
19 async function getAdminTokenOrDie (url: string, username: string, password: string) {
20 const accessToken = await getAccessToken(url, username, password)
21 const resMe = await getMyUserInformation(url, accessToken)
22 const me: User = resMe.body
24 if (me.role !== UserRole.ADMINISTRATOR) {
25 console.error('You must be an administrator.')
37 function getSettings () {
38 return new Promise<Settings>((res, rej) => {
39 const defaultSettings = {
44 config.read((err, data) => {
45 if (err) return rej(err)
47 return res(Object.keys(data).length === 0 ? defaultSettings : data)
52 async function getNetrc () {
53 const Netrc = require('netrc-parser').Netrc
55 const netrc = isTestInstance()
56 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
64 function writeSettings (settings) {
65 return new Promise((res, rej) => {
66 config.write(settings, err => {
67 if (err) return rej(err)
74 function deleteSettings () {
75 return new Promise((res, rej) => {
76 config.trash((err) => {
77 if (err) return rej(err)
84 function getRemoteObjectOrDie (
88 ): { url: string, username: string, password: string } {
89 if (!program['url'] || !program['username'] || !program['password']) {
90 // No remote and we don't have program parameters: quit
91 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
92 if (!program[ 'url' ]) console.error('--url field is required.')
93 if (!program[ 'username' ]) console.error('--username field is required.')
94 if (!program[ 'password' ]) console.error('--password field is required.')
96 return process.exit(-1)
99 let url: string = program['url']
100 let username: string = program['username']
101 let password: string = program['password']
103 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
105 const machine = netrc.machines[url]
107 if (!username && machine) username = machine.login
108 if (!password && machine) password = machine.password
110 return { url, username, password }
114 url: program[ 'url' ],
115 username: program[ 'username' ],
116 password: program[ 'password' ]
120 function buildCommonVideoOptions (command: Command) {
121 function list (val) {
122 return val.split(',')
126 .option('-n, --video-name <name>', 'Video name')
127 .option('-c, --category <category_number>', 'Category number')
128 .option('-l, --licence <licence_number>', 'Licence number')
129 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
130 .option('-t, --tags <tags>', 'Video tags', list)
131 .option('-N, --nsfw', 'Video is Not Safe For Work')
132 .option('-d, --video-description <description>', 'Video description')
133 .option('-P, --privacy <privacy_number>', 'Privacy')
134 .option('-C, --channel-name <channel_name>', 'Channel name')
135 .option('-m, --comments-enabled', 'Enable comments')
136 .option('-s, --support <support>', 'Video support text')
137 .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video')
138 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
141 async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) {
142 const defaultBooleanAttributes = {
144 commentsEnabled: true,
145 downloadEnabled: true,
146 waitTranscoding: true
149 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
151 for (const key of Object.keys(defaultBooleanAttributes)) {
152 if (command[ key ] !== undefined) {
153 booleanAttributes[key] = command[ key ]
154 } else if (defaultAttributes[key] !== undefined) {
155 booleanAttributes[key] = defaultAttributes[key]
157 booleanAttributes[key] = defaultBooleanAttributes[key]
161 const videoAttributes = {
162 name: command[ 'videoName' ] || defaultAttributes.name,
163 category: command[ 'category' ] || defaultAttributes.category || undefined,
164 licence: command[ 'licence' ] || defaultAttributes.licence || undefined,
165 language: command[ 'language' ] || defaultAttributes.language || undefined,
166 privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
167 support: command[ 'support' ] || defaultAttributes.support || undefined,
168 description: command[ 'videoDescription' ] || defaultAttributes.description || undefined,
169 tags: command[ 'tags' ] || defaultAttributes.tags || undefined
172 Object.assign(videoAttributes, booleanAttributes)
174 if (command[ 'channelName' ]) {
175 const res = await getVideoChannel(url, command['channelName'])
176 const videoChannel: VideoChannel = res.body
178 Object.assign(videoAttributes, { channelId: videoChannel.id })
180 if (!videoAttributes.support && videoChannel.support) {
181 Object.assign(videoAttributes, { support: videoChannel.support })
185 return videoAttributes
188 function getServerCredentials (program: any) {
189 return Promise.all([ getSettings(), getNetrc() ])
190 .then(([ settings, netrc ]) => {
191 return getRemoteObjectOrDie(program, settings, netrc)
195 function getLogger (logLevel = 'info') {
209 const logger = createLogger({
211 format: format.combine(
216 new (transports.Console)({
225 // ---------------------------------------------------------------------------
233 getRemoteObjectOrDie,
237 getServerCredentials,
239 buildCommonVideoOptions,
240 buildVideoAttributesFromCommander,