]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/cli.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tools / cli.ts
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 { UserRole } from '@shared/models'
7 import { VideoPrivacy } from '../../shared/models/videos'
8 import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
9
10 let configName = 'PeerTube/CLI'
11 if (isTestInstance()) configName += `-${getAppNumber()}`
12
13 const config = require('application-config')(configName)
14
15 const version = require('../../../package.json').version
16
17 async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) {
18 const token = await server.login.getAccessToken(username, password)
19 const me = await server.users.getMyInfo({ token })
20
21 if (me.role !== UserRole.ADMINISTRATOR) {
22 console.error('You must be an administrator.')
23 process.exit(-1)
24 }
25
26 return token
27 }
28
29 interface Settings {
30 remotes: any[]
31 default: number
32 }
33
34 async function getSettings (): Promise<Settings> {
35 const defaultSettings = {
36 remotes: [],
37 default: -1
38 }
39
40 const data = await config.read()
41
42 return Object.keys(data).length === 0
43 ? defaultSettings
44 : data
45 }
46
47 async function getNetrc () {
48 const Netrc = require('netrc-parser').Netrc
49
50 const netrc = isTestInstance()
51 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
52 : new Netrc()
53
54 await netrc.load()
55
56 return netrc
57 }
58
59 function writeSettings (settings: Settings) {
60 return config.write(settings)
61 }
62
63 function deleteSettings () {
64 return config.trash()
65 }
66
67 function getRemoteObjectOrDie (
68 program: Command,
69 settings: Settings,
70 netrc: Netrc
71 ): { url: string, username: string, password: string } {
72 const options = program.opts()
73
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.')
80
81 return process.exit(-1)
82 }
83
84 let url: string = options.url
85 let username: string = options.username
86 let password: string = options.password
87
88 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
89
90 const machine = netrc.machines[url]
91
92 if (!username && machine) username = machine.login
93 if (!password && machine) password = machine.password
94
95 return { url, username, password }
96 }
97
98 return {
99 url: options.url,
100 username: options.username,
101 password: options.password
102 }
103 }
104
105 function buildCommonVideoOptions (command: Command) {
106 function list (val) {
107 return val.split(',')
108 }
109
110 return command
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')
125 }
126
127 async function buildVideoAttributesFromCommander (server: ServerInfo, command: Command, defaultAttributes: any = {}) {
128 const options = command.opts()
129
130 const defaultBooleanAttributes = {
131 nsfw: false,
132 commentsEnabled: true,
133 downloadEnabled: true,
134 waitTranscoding: true
135 }
136
137 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
138
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]
144 } else {
145 booleanAttributes[key] = defaultBooleanAttributes[key]
146 }
147 }
148
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
158 }
159
160 Object.assign(videoAttributes, booleanAttributes)
161
162 if (options.channelName) {
163 const videoChannel = await server.channels.get({ channelName: options.channelName })
164
165 Object.assign(videoAttributes, { channelId: videoChannel.id })
166
167 if (!videoAttributes.support && videoChannel.support) {
168 Object.assign(videoAttributes, { support: videoChannel.support })
169 }
170 }
171
172 return videoAttributes
173 }
174
175 function getServerCredentials (program: Command) {
176 return Promise.all([ getSettings(), getNetrc() ])
177 .then(([ settings, netrc ]) => {
178 return getRemoteObjectOrDie(program, settings, netrc)
179 })
180 }
181
182 function buildServer (url: string): ServerInfo {
183 const server = { url, internalServerNumber: undefined }
184 assignCommands(server)
185
186 return server
187 }
188
189 async function assignToken (server: ServerInfo, username: string, password: string) {
190 const bodyClient = await server.login.getClient()
191 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
192
193 const body = await server.login.login({ client, user: { username, password } })
194
195 server.accessToken = body.access_token
196 }
197
198 function getLogger (logLevel = 'info') {
199 const logLevels = {
200 0: 0,
201 error: 0,
202 1: 1,
203 warn: 1,
204 2: 2,
205 info: 2,
206 3: 3,
207 verbose: 3,
208 4: 4,
209 debug: 4
210 }
211
212 const logger = createLogger({
213 levels: logLevels,
214 format: format.combine(
215 format.splat(),
216 format.simple()
217 ),
218 transports: [
219 new (transports.Console)({
220 level: logLevel
221 })
222 ]
223 })
224
225 return logger
226 }
227
228 // ---------------------------------------------------------------------------
229
230 export {
231 version,
232 getLogger,
233 getSettings,
234 getNetrc,
235 getRemoteObjectOrDie,
236 writeSettings,
237 deleteSettings,
238
239 getServerCredentials,
240
241 buildCommonVideoOptions,
242 buildVideoAttributesFromCommander,
243
244 getAdminTokenOrDie,
245 buildServer,
246 assignToken
247 }