]>
Commit | Line | Data |
---|---|---|
078f17e6 | 1 | import { Command } from 'commander' |
1a12f66d | 2 | import { Netrc } from 'netrc-parser' |
1a12f66d | 3 | import { join } from 'path' |
bda3b705 | 4 | import { createLogger, format, transports } from 'winston' |
1a73a7dc | 5 | import { getAppNumber, isTestInstance } from '@server/helpers/core-utils' |
f8360396 C |
6 | import { loadLanguages } from '@server/initializers/constants' |
7 | import { root } from '@shared/core-utils' | |
1a73a7dc | 8 | import { UserRole, VideoPrivacy } from '@shared/models' |
f8360396 | 9 | import { PeerTubeServer } from '@shared/server-commands' |
1a12f66d C |
10 | |
11 | let configName = 'PeerTube/CLI' | |
12 | if (isTestInstance()) configName += `-${getAppNumber()}` | |
13 | ||
14 | const config = require('application-config')(configName) | |
8704acf4 | 15 | |
f8360396 | 16 | const version = require(join(root(), 'package.json')).version |
8704acf4 | 17 | |
254d3579 | 18 | async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) { |
89d241a7 C |
19 | const token = await server.login.getAccessToken(username, password) |
20 | const me = await server.users.getMyInfo({ token }) | |
26fcf2ef | 21 | |
c08e4cfc | 22 | if (me.role.id !== UserRole.ADMINISTRATOR) { |
26fcf2ef C |
23 | console.error('You must be an administrator.') |
24 | process.exit(-1) | |
25 | } | |
26 | ||
7926c5f9 | 27 | return token |
26fcf2ef C |
28 | } |
29 | ||
8704acf4 | 30 | interface Settings { |
a1587156 | 31 | remotes: any[] |
8704acf4 RK |
32 | default: number |
33 | } | |
34 | ||
3d3bb238 C |
35 | async function getSettings (): Promise<Settings> { |
36 | const defaultSettings = { | |
37 | remotes: [], | |
38 | default: -1 | |
39 | } | |
2b4dd7e2 | 40 | |
3d3bb238 | 41 | const data = await config.read() |
2b4dd7e2 | 42 | |
3d3bb238 C |
43 | return Object.keys(data).length === 0 |
44 | ? defaultSettings | |
45 | : data | |
8704acf4 RK |
46 | } |
47 | ||
2b4dd7e2 | 48 | async function getNetrc () { |
1a12f66d C |
49 | const Netrc = require('netrc-parser').Netrc |
50 | ||
51 | const netrc = isTestInstance() | |
52 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | |
53 | : new Netrc() | |
54 | ||
2b4dd7e2 C |
55 | await netrc.load() |
56 | ||
57 | return netrc | |
58 | } | |
59 | ||
3d3bb238 C |
60 | function writeSettings (settings: Settings) { |
61 | return config.write(settings) | |
1a12f66d C |
62 | } |
63 | ||
64 | function deleteSettings () { | |
3d3bb238 | 65 | return config.trash() |
8704acf4 RK |
66 | } |
67 | ||
8d2be0ed | 68 | function getRemoteObjectOrDie ( |
12152aa0 | 69 | program: Command, |
8d2be0ed C |
70 | settings: Settings, |
71 | netrc: Netrc | |
72 | ): { url: string, username: string, password: string } { | |
ba5a8d89 C |
73 | const options = program.opts() |
74 | ||
74220dba | 75 | function exitIfNoOptions (optionNames: string[], errorPrefix: string = '') { |
4dfd57ae C |
76 | let exit = false |
77 | ||
74220dba | 78 | for (const key of optionNames) { |
4dfd57ae | 79 | if (!options[key]) { |
4abc7b05 C |
80 | if (exit === false && errorPrefix) console.error(errorPrefix) |
81 | ||
4dfd57ae C |
82 | console.error(`--${key} field is required`) |
83 | exit = true | |
84 | } | |
2b4dd7e2 C |
85 | } |
86 | ||
4dfd57ae C |
87 | if (exit) process.exit(-1) |
88 | } | |
89 | ||
4abc7b05 C |
90 | // If username or password are specified, both are mandatory |
91 | if (options.username || options.password) { | |
92 | exitIfNoOptions([ 'username', 'password' ]) | |
93 | } | |
2b4dd7e2 | 94 | |
4abc7b05 C |
95 | // If no available machines, url, username and password args are mandatory |
96 | if (Object.keys(netrc.machines).length === 0) { | |
97 | exitIfNoOptions([ 'url', 'username', 'password' ], 'No account found in netrc') | |
98 | } | |
2b4dd7e2 | 99 | |
4abc7b05 C |
100 | if (settings.remotes.length === 0 || settings.default === -1) { |
101 | exitIfNoOptions([ 'url' ], 'No default instance found') | |
102 | } | |
1a12f66d | 103 | |
4abc7b05 C |
104 | let url: string = options.url |
105 | let username: string = options.username | |
106 | let password: string = options.password | |
2b4dd7e2 | 107 | |
4abc7b05 | 108 | if (!url && settings.default !== -1) url = settings.remotes[settings.default] |
2b4dd7e2 | 109 | |
4abc7b05 C |
110 | const machine = netrc.machines[url] |
111 | if ((!username || !password) && !machine) { | |
112 | console.error('Cannot find existing configuration for %s.', url) | |
113 | process.exit(-1) | |
2b4dd7e2 | 114 | } |
4abc7b05 C |
115 | |
116 | if (!username && machine) username = machine.login | |
117 | if (!password && machine) password = machine.password | |
118 | ||
119 | return { url, username, password } | |
2b4dd7e2 | 120 | } |
8704acf4 | 121 | |
12152aa0 | 122 | function buildCommonVideoOptions (command: Command) { |
1205823f C |
123 | function list (val) { |
124 | return val.split(',') | |
125 | } | |
126 | ||
127 | return command | |
128 | .option('-n, --video-name <name>', 'Video name') | |
129 | .option('-c, --category <category_number>', 'Category number') | |
130 | .option('-l, --licence <licence_number>', 'Licence number') | |
131 | .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)') | |
132 | .option('-t, --tags <tags>', 'Video tags', list) | |
133 | .option('-N, --nsfw', 'Video is Not Safe For Work') | |
134 | .option('-d, --video-description <description>', 'Video description') | |
135 | .option('-P, --privacy <privacy_number>', 'Privacy') | |
136 | .option('-C, --channel-name <channel_name>', 'Channel name') | |
bd65cf02 | 137 | .option('--no-comments-enabled', 'Disable video comments') |
1205823f | 138 | .option('-s, --support <support>', 'Video support text') |
bd65cf02 C |
139 | .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video') |
140 | .option('--no-download-enabled', 'Disable video download') | |
bda3b705 | 141 | .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') |
1205823f C |
142 | } |
143 | ||
254d3579 | 144 | async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) { |
ba5a8d89 C |
145 | const options = command.opts() |
146 | ||
6b226c32 C |
147 | const defaultBooleanAttributes = { |
148 | nsfw: false, | |
149 | commentsEnabled: true, | |
150 | downloadEnabled: true, | |
151 | waitTranscoding: true | |
152 | } | |
153 | ||
154 | const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} | |
1205823f | 155 | |
6b226c32 | 156 | for (const key of Object.keys(defaultBooleanAttributes)) { |
ba5a8d89 C |
157 | if (options[key] !== undefined) { |
158 | booleanAttributes[key] = options[key] | |
1205823f C |
159 | } else if (defaultAttributes[key] !== undefined) { |
160 | booleanAttributes[key] = defaultAttributes[key] | |
161 | } else { | |
6b226c32 | 162 | booleanAttributes[key] = defaultBooleanAttributes[key] |
1205823f C |
163 | } |
164 | } | |
165 | ||
166 | const videoAttributes = { | |
ba5a8d89 C |
167 | name: options.videoName || defaultAttributes.name, |
168 | category: options.category || defaultAttributes.category || undefined, | |
169 | licence: options.licence || defaultAttributes.licence || undefined, | |
170 | language: options.language || defaultAttributes.language || undefined, | |
171 | privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC, | |
172 | support: options.support || defaultAttributes.support || undefined, | |
173 | description: options.videoDescription || defaultAttributes.description || undefined, | |
174 | tags: options.tags || defaultAttributes.tags || undefined | |
1205823f C |
175 | } |
176 | ||
177 | Object.assign(videoAttributes, booleanAttributes) | |
178 | ||
ba5a8d89 | 179 | if (options.channelName) { |
89d241a7 | 180 | const videoChannel = await server.channels.get({ channelName: options.channelName }) |
1205823f C |
181 | |
182 | Object.assign(videoAttributes, { channelId: videoChannel.id }) | |
183 | ||
184 | if (!videoAttributes.support && videoChannel.support) { | |
185 | Object.assign(videoAttributes, { support: videoChannel.support }) | |
186 | } | |
187 | } | |
188 | ||
189 | return videoAttributes | |
190 | } | |
191 | ||
12152aa0 | 192 | function getServerCredentials (program: Command) { |
8d2be0ed | 193 | return Promise.all([ getSettings(), getNetrc() ]) |
a1587156 C |
194 | .then(([ settings, netrc ]) => { |
195 | return getRemoteObjectOrDie(program, settings, netrc) | |
196 | }) | |
8d2be0ed C |
197 | } |
198 | ||
254d3579 | 199 | function buildServer (url: string) { |
06aad801 | 200 | loadLanguages() |
254d3579 | 201 | return new PeerTubeServer({ url }) |
078f17e6 C |
202 | } |
203 | ||
254d3579 | 204 | async function assignToken (server: PeerTubeServer, username: string, password: string) { |
89d241a7 | 205 | const bodyClient = await server.login.getClient() |
d0a0fa42 C |
206 | const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } |
207 | ||
89d241a7 | 208 | const body = await server.login.login({ client, user: { username, password } }) |
d0a0fa42 C |
209 | |
210 | server.accessToken = body.access_token | |
211 | } | |
212 | ||
bda3b705 FL |
213 | function getLogger (logLevel = 'info') { |
214 | const logLevels = { | |
215 | 0: 0, | |
216 | error: 0, | |
217 | 1: 1, | |
218 | warn: 1, | |
219 | 2: 2, | |
220 | info: 2, | |
221 | 3: 3, | |
222 | verbose: 3, | |
223 | 4: 4, | |
224 | debug: 4 | |
225 | } | |
226 | ||
227 | const logger = createLogger({ | |
228 | levels: logLevels, | |
229 | format: format.combine( | |
230 | format.splat(), | |
231 | format.simple() | |
232 | ), | |
233 | transports: [ | |
234 | new (transports.Console)({ | |
235 | level: logLevel | |
236 | }) | |
237 | ] | |
238 | }) | |
239 | ||
240 | return logger | |
241 | } | |
242 | ||
8704acf4 RK |
243 | // --------------------------------------------------------------------------- |
244 | ||
245 | export { | |
246 | version, | |
bda3b705 | 247 | getLogger, |
8704acf4 | 248 | getSettings, |
2b4dd7e2 C |
249 | getNetrc, |
250 | getRemoteObjectOrDie, | |
1a12f66d | 251 | writeSettings, |
1205823f C |
252 | deleteSettings, |
253 | ||
8d2be0ed C |
254 | getServerCredentials, |
255 | ||
1205823f | 256 | buildCommonVideoOptions, |
26fcf2ef C |
257 | buildVideoAttributesFromCommander, |
258 | ||
078f17e6 | 259 | getAdminTokenOrDie, |
d0a0fa42 C |
260 | buildServer, |
261 | assignToken | |
8704acf4 | 262 | } |