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