]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/cli.ts
Introduce login command
[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 { 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'
12
13 let configName = 'PeerTube/CLI'
14 if (isTestInstance()) configName += `-${getAppNumber()}`
15
16 const config = require('application-config')(configName)
17
18 const version = require('../../../package.json').version
19
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
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
33 async function getAccessTokenOrDie (url: string, username: string, password: string) {
34 return getAccessToken(url, username, password)
35 }
36
37 interface Settings {
38 remotes: any[]
39 default: number
40 }
41
42 async function getSettings (): Promise<Settings> {
43 const defaultSettings = {
44 remotes: [],
45 default: -1
46 }
47
48 const data = await config.read()
49
50 return Object.keys(data).length === 0
51 ? defaultSettings
52 : data
53 }
54
55 async function getNetrc () {
56 const Netrc = require('netrc-parser').Netrc
57
58 const netrc = isTestInstance()
59 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
60 : new Netrc()
61
62 await netrc.load()
63
64 return netrc
65 }
66
67 function writeSettings (settings: Settings) {
68 return config.write(settings)
69 }
70
71 function deleteSettings () {
72 return config.trash()
73 }
74
75 function getRemoteObjectOrDie (
76 program: Command,
77 settings: Settings,
78 netrc: Netrc
79 ): { url: string, username: string, password: string } {
80 const options = program.opts()
81
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.')
88
89 return process.exit(-1)
90 }
91
92 let url: string = options.url
93 let username: string = options.username
94 let password: string = options.password
95
96 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
97
98 const machine = netrc.machines[url]
99
100 if (!username && machine) username = machine.login
101 if (!password && machine) password = machine.password
102
103 return { url, username, password }
104 }
105
106 return {
107 url: options.url,
108 username: options.username,
109 password: options.password
110 }
111 }
112
113 function buildCommonVideoOptions (command: Command) {
114 function list (val) {
115 return val.split(',')
116 }
117
118 return command
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')
133 }
134
135 async function buildVideoAttributesFromCommander (server: ServerInfo, command: Command, defaultAttributes: any = {}) {
136 const options = command.opts()
137
138 const defaultBooleanAttributes = {
139 nsfw: false,
140 commentsEnabled: true,
141 downloadEnabled: true,
142 waitTranscoding: true
143 }
144
145 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
146
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]
152 } else {
153 booleanAttributes[key] = defaultBooleanAttributes[key]
154 }
155 }
156
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
166 }
167
168 Object.assign(videoAttributes, booleanAttributes)
169
170 if (options.channelName) {
171 const videoChannel = await server.channelsCommand.get({ channelName: options.channelName })
172
173 Object.assign(videoAttributes, { channelId: videoChannel.id })
174
175 if (!videoAttributes.support && videoChannel.support) {
176 Object.assign(videoAttributes, { support: videoChannel.support })
177 }
178 }
179
180 return videoAttributes
181 }
182
183 function getServerCredentials (program: Command) {
184 return Promise.all([ getSettings(), getNetrc() ])
185 .then(([ settings, netrc ]) => {
186 return getRemoteObjectOrDie(program, settings, netrc)
187 })
188 }
189
190 function buildServer (url: string, accessToken?: string): ServerInfo {
191 const server = { url, accessToken, internalServerNumber: undefined }
192 assignCommands(server)
193
194 return server
195 }
196
197 function getLogger (logLevel = 'info') {
198 const logLevels = {
199 0: 0,
200 error: 0,
201 1: 1,
202 warn: 1,
203 2: 2,
204 info: 2,
205 3: 3,
206 verbose: 3,
207 4: 4,
208 debug: 4
209 }
210
211 const logger = createLogger({
212 levels: logLevels,
213 format: format.combine(
214 format.splat(),
215 format.simple()
216 ),
217 transports: [
218 new (transports.Console)({
219 level: logLevel
220 })
221 ]
222 })
223
224 return logger
225 }
226
227 // ---------------------------------------------------------------------------
228
229 export {
230 version,
231 getLogger,
232 getSettings,
233 getNetrc,
234 getRemoteObjectOrDie,
235 writeSettings,
236 deleteSettings,
237
238 getServerCredentials,
239
240 buildCommonVideoOptions,
241 buildVideoAttributesFromCommander,
242
243 getAdminTokenOrDie,
244 getAccessTokenOrDie,
245 buildServer
246 }