]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/cli.ts
Translated using Weblate (Vietnamese)
[github/Chocobozzz/PeerTube.git] / server / tools / cli.ts
CommitLineData
1a12f66d 1import { Netrc } from 'netrc-parser'
1205823f 2import { getAppNumber, isTestInstance } from '../helpers/core-utils'
1a12f66d 3import { join } from 'path'
8d2be0ed
C
4import { root } from '../../shared/extra-utils/miscs/miscs'
5import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
ded739eb 6import { CommanderStatic } from 'commander'
1205823f 7import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
bda3b705 8import { createLogger, format, transports } from 'winston'
2f1756a0 9import { getMyUserInformation } from '@shared/extra-utils/users/users'
26fcf2ef 10import { User, UserRole } from '@shared/models'
2f1756a0 11import { getAccessToken } from '@shared/extra-utils/users/login'
1a12f66d
C
12
13let configName = 'PeerTube/CLI'
14if (isTestInstance()) configName += `-${getAppNumber()}`
15
16const config = require('application-config')(configName)
8704acf4 17
499d9015 18const version = require('../../../package.json').version
8704acf4 19
26fcf2ef
C
20async 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
24
25 if (me.role !== UserRole.ADMINISTRATOR) {
26 console.error('You must be an administrator.')
27 process.exit(-1)
28 }
29
30 return accessToken
31}
32
8704acf4 33interface Settings {
a1587156 34 remotes: any[]
8704acf4
RK
35 default: number
36}
37
3d3bb238
C
38async function getSettings (): Promise<Settings> {
39 const defaultSettings = {
40 remotes: [],
41 default: -1
42 }
2b4dd7e2 43
3d3bb238 44 const data = await config.read()
2b4dd7e2 45
3d3bb238
C
46 return Object.keys(data).length === 0
47 ? defaultSettings
48 : data
8704acf4
RK
49}
50
2b4dd7e2 51async function getNetrc () {
1a12f66d
C
52 const Netrc = require('netrc-parser').Netrc
53
54 const netrc = isTestInstance()
55 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
56 : new Netrc()
57
2b4dd7e2
C
58 await netrc.load()
59
60 return netrc
61}
62
3d3bb238
C
63function writeSettings (settings: Settings) {
64 return config.write(settings)
1a12f66d
C
65}
66
67function deleteSettings () {
3d3bb238 68 return config.trash()
8704acf4
RK
69}
70
8d2be0ed 71function getRemoteObjectOrDie (
ba5a8d89 72 program: CommanderStatic,
8d2be0ed
C
73 settings: Settings,
74 netrc: Netrc
75): { url: string, username: string, password: string } {
ba5a8d89
C
76 const options = program.opts()
77
78 if (!options.url || !options.username || !options.password) {
1a12f66d 79 // No remote and we don't have program parameters: quit
9f167f12 80 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
ba5a8d89
C
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.')
2b4dd7e2
C
84
85 return process.exit(-1)
86 }
87
ba5a8d89
C
88 let url: string = options.url
89 let username: string = options.username
90 let password: string = options.password
2b4dd7e2 91
1a12f66d 92 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
2b4dd7e2 93
9f167f12 94 const machine = netrc.machines[url]
1a12f66d
C
95
96 if (!username && machine) username = machine.login
97 if (!password && machine) password = machine.password
2b4dd7e2
C
98
99 return { url, username, password }
100 }
101
102 return {
ba5a8d89
C
103 url: options.url,
104 username: options.username,
105 password: options.password
2b4dd7e2
C
106 }
107}
8704acf4 108
ded739eb 109function buildCommonVideoOptions (command: CommanderStatic) {
1205823f
C
110 function list (val) {
111 return val.split(',')
112 }
113
114 return command
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')
bd65cf02 124 .option('--no-comments-enabled', 'Disable video comments')
1205823f 125 .option('-s, --support <support>', 'Video support text')
bd65cf02
C
126 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
127 .option('--no-download-enabled', 'Disable video download')
bda3b705 128 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
1205823f
C
129}
130
ded739eb 131async function buildVideoAttributesFromCommander (url: string, command: CommanderStatic, defaultAttributes: any = {}) {
ba5a8d89
C
132 const options = command.opts()
133
6b226c32
C
134 const defaultBooleanAttributes = {
135 nsfw: false,
136 commentsEnabled: true,
137 downloadEnabled: true,
138 waitTranscoding: true
139 }
140
141 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
1205823f 142
6b226c32 143 for (const key of Object.keys(defaultBooleanAttributes)) {
ba5a8d89
C
144 if (options[key] !== undefined) {
145 booleanAttributes[key] = options[key]
1205823f
C
146 } else if (defaultAttributes[key] !== undefined) {
147 booleanAttributes[key] = defaultAttributes[key]
148 } else {
6b226c32 149 booleanAttributes[key] = defaultBooleanAttributes[key]
1205823f
C
150 }
151 }
152
153 const videoAttributes = {
ba5a8d89
C
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
1205823f
C
162 }
163
164 Object.assign(videoAttributes, booleanAttributes)
165
ba5a8d89
C
166 if (options.channelName) {
167 const res = await getVideoChannel(url, options.channelName)
1205823f
C
168 const videoChannel: VideoChannel = res.body
169
170 Object.assign(videoAttributes, { channelId: videoChannel.id })
171
172 if (!videoAttributes.support && videoChannel.support) {
173 Object.assign(videoAttributes, { support: videoChannel.support })
174 }
175 }
176
177 return videoAttributes
178}
179
ba5a8d89 180function getServerCredentials (program: CommanderStatic) {
8d2be0ed 181 return Promise.all([ getSettings(), getNetrc() ])
a1587156
C
182 .then(([ settings, netrc ]) => {
183 return getRemoteObjectOrDie(program, settings, netrc)
184 })
8d2be0ed
C
185}
186
bda3b705
FL
187function getLogger (logLevel = 'info') {
188 const logLevels = {
189 0: 0,
190 error: 0,
191 1: 1,
192 warn: 1,
193 2: 2,
194 info: 2,
195 3: 3,
196 verbose: 3,
197 4: 4,
198 debug: 4
199 }
200
201 const logger = createLogger({
202 levels: logLevels,
203 format: format.combine(
204 format.splat(),
205 format.simple()
206 ),
207 transports: [
208 new (transports.Console)({
209 level: logLevel
210 })
211 ]
212 })
213
214 return logger
215}
216
8704acf4
RK
217// ---------------------------------------------------------------------------
218
219export {
220 version,
bda3b705 221 getLogger,
8704acf4 222 getSettings,
2b4dd7e2
C
223 getNetrc,
224 getRemoteObjectOrDie,
1a12f66d 225 writeSettings,
1205823f
C
226 deleteSettings,
227
8d2be0ed
C
228 getServerCredentials,
229
1205823f 230 buildCommonVideoOptions,
26fcf2ef
C
231 buildVideoAttributesFromCommander,
232
233 getAdminTokenOrDie
8704acf4 234}