]>
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 C |
22 | |
23 | if (me.role !== UserRole.ADMINISTRATOR) { | |
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 | ||
76 | if (!options.url || !options.username || !options.password) { | |
1a12f66d | 77 | // No remote and we don't have program parameters: quit |
9f167f12 | 78 | if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { |
ba5a8d89 C |
79 | if (!options.url) console.error('--url field is required.') |
80 | if (!options.username) console.error('--username field is required.') | |
81 | if (!options.password) console.error('--password field is required.') | |
2b4dd7e2 C |
82 | |
83 | return process.exit(-1) | |
84 | } | |
85 | ||
ba5a8d89 C |
86 | let url: string = options.url |
87 | let username: string = options.username | |
88 | let password: string = options.password | |
2b4dd7e2 | 89 | |
1a12f66d | 90 | if (!url && settings.default !== -1) url = settings.remotes[settings.default] |
2b4dd7e2 | 91 | |
9f167f12 | 92 | const machine = netrc.machines[url] |
1a12f66d C |
93 | |
94 | if (!username && machine) username = machine.login | |
95 | if (!password && machine) password = machine.password | |
2b4dd7e2 C |
96 | |
97 | return { url, username, password } | |
98 | } | |
99 | ||
100 | return { | |
ba5a8d89 C |
101 | url: options.url, |
102 | username: options.username, | |
103 | password: options.password | |
2b4dd7e2 C |
104 | } |
105 | } | |
8704acf4 | 106 | |
12152aa0 | 107 | function buildCommonVideoOptions (command: Command) { |
1205823f C |
108 | function list (val) { |
109 | return val.split(',') | |
110 | } | |
111 | ||
112 | return command | |
113 | .option('-n, --video-name <name>', 'Video name') | |
114 | .option('-c, --category <category_number>', 'Category number') | |
115 | .option('-l, --licence <licence_number>', 'Licence number') | |
116 | .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)') | |
117 | .option('-t, --tags <tags>', 'Video tags', list) | |
118 | .option('-N, --nsfw', 'Video is Not Safe For Work') | |
119 | .option('-d, --video-description <description>', 'Video description') | |
120 | .option('-P, --privacy <privacy_number>', 'Privacy') | |
121 | .option('-C, --channel-name <channel_name>', 'Channel name') | |
bd65cf02 | 122 | .option('--no-comments-enabled', 'Disable video comments') |
1205823f | 123 | .option('-s, --support <support>', 'Video support text') |
bd65cf02 C |
124 | .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video') |
125 | .option('--no-download-enabled', 'Disable video download') | |
bda3b705 | 126 | .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') |
1205823f C |
127 | } |
128 | ||
254d3579 | 129 | async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) { |
ba5a8d89 C |
130 | const options = command.opts() |
131 | ||
6b226c32 C |
132 | const defaultBooleanAttributes = { |
133 | nsfw: false, | |
134 | commentsEnabled: true, | |
135 | downloadEnabled: true, | |
136 | waitTranscoding: true | |
137 | } | |
138 | ||
139 | const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} | |
1205823f | 140 | |
6b226c32 | 141 | for (const key of Object.keys(defaultBooleanAttributes)) { |
ba5a8d89 C |
142 | if (options[key] !== undefined) { |
143 | booleanAttributes[key] = options[key] | |
1205823f C |
144 | } else if (defaultAttributes[key] !== undefined) { |
145 | booleanAttributes[key] = defaultAttributes[key] | |
146 | } else { | |
6b226c32 | 147 | booleanAttributes[key] = defaultBooleanAttributes[key] |
1205823f C |
148 | } |
149 | } | |
150 | ||
151 | const videoAttributes = { | |
ba5a8d89 C |
152 | name: options.videoName || defaultAttributes.name, |
153 | category: options.category || defaultAttributes.category || undefined, | |
154 | licence: options.licence || defaultAttributes.licence || undefined, | |
155 | language: options.language || defaultAttributes.language || undefined, | |
156 | privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC, | |
157 | support: options.support || defaultAttributes.support || undefined, | |
158 | description: options.videoDescription || defaultAttributes.description || undefined, | |
159 | tags: options.tags || defaultAttributes.tags || undefined | |
1205823f C |
160 | } |
161 | ||
162 | Object.assign(videoAttributes, booleanAttributes) | |
163 | ||
ba5a8d89 | 164 | if (options.channelName) { |
89d241a7 | 165 | const videoChannel = await server.channels.get({ channelName: options.channelName }) |
1205823f C |
166 | |
167 | Object.assign(videoAttributes, { channelId: videoChannel.id }) | |
168 | ||
169 | if (!videoAttributes.support && videoChannel.support) { | |
170 | Object.assign(videoAttributes, { support: videoChannel.support }) | |
171 | } | |
172 | } | |
173 | ||
174 | return videoAttributes | |
175 | } | |
176 | ||
12152aa0 | 177 | function getServerCredentials (program: Command) { |
8d2be0ed | 178 | return Promise.all([ getSettings(), getNetrc() ]) |
a1587156 C |
179 | .then(([ settings, netrc ]) => { |
180 | return getRemoteObjectOrDie(program, settings, netrc) | |
181 | }) | |
8d2be0ed C |
182 | } |
183 | ||
254d3579 | 184 | function buildServer (url: string) { |
06aad801 | 185 | loadLanguages() |
254d3579 | 186 | return new PeerTubeServer({ url }) |
078f17e6 C |
187 | } |
188 | ||
254d3579 | 189 | async function assignToken (server: PeerTubeServer, username: string, password: string) { |
89d241a7 | 190 | const bodyClient = await server.login.getClient() |
d0a0fa42 C |
191 | const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } |
192 | ||
89d241a7 | 193 | const body = await server.login.login({ client, user: { username, password } }) |
d0a0fa42 C |
194 | |
195 | server.accessToken = body.access_token | |
196 | } | |
197 | ||
bda3b705 FL |
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 | ||
8704acf4 RK |
228 | // --------------------------------------------------------------------------- |
229 | ||
230 | export { | |
231 | version, | |
bda3b705 | 232 | getLogger, |
8704acf4 | 233 | getSettings, |
2b4dd7e2 C |
234 | getNetrc, |
235 | getRemoteObjectOrDie, | |
1a12f66d | 236 | writeSettings, |
1205823f C |
237 | deleteSettings, |
238 | ||
8d2be0ed C |
239 | getServerCredentials, |
240 | ||
1205823f | 241 | buildCommonVideoOptions, |
26fcf2ef C |
242 | buildVideoAttributesFromCommander, |
243 | ||
078f17e6 | 244 | getAdminTokenOrDie, |
d0a0fa42 C |
245 | buildServer, |
246 | assignToken | |
8704acf4 | 247 | } |