]>
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' |
254d3579 | 5 | import { PeerTubeServer } from '@shared/extra-utils' |
7926c5f9 | 6 | import { UserRole } from '@shared/models' |
078f17e6 | 7 | import { VideoPrivacy } from '../../shared/models/videos' |
d0a0fa42 | 8 | import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' |
1a12f66d C |
9 | |
10 | let configName = 'PeerTube/CLI' | |
11 | if (isTestInstance()) configName += `-${getAppNumber()}` | |
12 | ||
13 | const config = require('application-config')(configName) | |
8704acf4 | 14 | |
499d9015 | 15 | const version = require('../../../package.json').version |
8704acf4 | 16 | |
254d3579 | 17 | async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) { |
89d241a7 C |
18 | const token = await server.login.getAccessToken(username, password) |
19 | const me = await server.users.getMyInfo({ token }) | |
26fcf2ef C |
20 | |
21 | if (me.role !== UserRole.ADMINISTRATOR) { | |
22 | console.error('You must be an administrator.') | |
23 | process.exit(-1) | |
24 | } | |
25 | ||
7926c5f9 | 26 | return token |
26fcf2ef C |
27 | } |
28 | ||
8704acf4 | 29 | interface Settings { |
a1587156 | 30 | remotes: any[] |
8704acf4 RK |
31 | default: number |
32 | } | |
33 | ||
3d3bb238 C |
34 | async function getSettings (): Promise<Settings> { |
35 | const defaultSettings = { | |
36 | remotes: [], | |
37 | default: -1 | |
38 | } | |
2b4dd7e2 | 39 | |
3d3bb238 | 40 | const data = await config.read() |
2b4dd7e2 | 41 | |
3d3bb238 C |
42 | return Object.keys(data).length === 0 |
43 | ? defaultSettings | |
44 | : data | |
8704acf4 RK |
45 | } |
46 | ||
2b4dd7e2 | 47 | async function getNetrc () { |
1a12f66d C |
48 | const Netrc = require('netrc-parser').Netrc |
49 | ||
50 | const netrc = isTestInstance() | |
51 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | |
52 | : new Netrc() | |
53 | ||
2b4dd7e2 C |
54 | await netrc.load() |
55 | ||
56 | return netrc | |
57 | } | |
58 | ||
3d3bb238 C |
59 | function writeSettings (settings: Settings) { |
60 | return config.write(settings) | |
1a12f66d C |
61 | } |
62 | ||
63 | function deleteSettings () { | |
3d3bb238 | 64 | return config.trash() |
8704acf4 RK |
65 | } |
66 | ||
8d2be0ed | 67 | function getRemoteObjectOrDie ( |
12152aa0 | 68 | program: Command, |
8d2be0ed C |
69 | settings: Settings, |
70 | netrc: Netrc | |
71 | ): { url: string, username: string, password: string } { | |
ba5a8d89 C |
72 | const options = program.opts() |
73 | ||
74 | if (!options.url || !options.username || !options.password) { | |
1a12f66d | 75 | // No remote and we don't have program parameters: quit |
9f167f12 | 76 | if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { |
ba5a8d89 C |
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.') | |
2b4dd7e2 C |
80 | |
81 | return process.exit(-1) | |
82 | } | |
83 | ||
ba5a8d89 C |
84 | let url: string = options.url |
85 | let username: string = options.username | |
86 | let password: string = options.password | |
2b4dd7e2 | 87 | |
1a12f66d | 88 | if (!url && settings.default !== -1) url = settings.remotes[settings.default] |
2b4dd7e2 | 89 | |
9f167f12 | 90 | const machine = netrc.machines[url] |
1a12f66d C |
91 | |
92 | if (!username && machine) username = machine.login | |
93 | if (!password && machine) password = machine.password | |
2b4dd7e2 C |
94 | |
95 | return { url, username, password } | |
96 | } | |
97 | ||
98 | return { | |
ba5a8d89 C |
99 | url: options.url, |
100 | username: options.username, | |
101 | password: options.password | |
2b4dd7e2 C |
102 | } |
103 | } | |
8704acf4 | 104 | |
12152aa0 | 105 | function buildCommonVideoOptions (command: Command) { |
1205823f C |
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') | |
bd65cf02 | 120 | .option('--no-comments-enabled', 'Disable video comments') |
1205823f | 121 | .option('-s, --support <support>', 'Video support text') |
bd65cf02 C |
122 | .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video') |
123 | .option('--no-download-enabled', 'Disable video download') | |
bda3b705 | 124 | .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') |
1205823f C |
125 | } |
126 | ||
254d3579 | 127 | async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) { |
ba5a8d89 C |
128 | const options = command.opts() |
129 | ||
6b226c32 C |
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 } | {} = {} | |
1205823f | 138 | |
6b226c32 | 139 | for (const key of Object.keys(defaultBooleanAttributes)) { |
ba5a8d89 C |
140 | if (options[key] !== undefined) { |
141 | booleanAttributes[key] = options[key] | |
1205823f C |
142 | } else if (defaultAttributes[key] !== undefined) { |
143 | booleanAttributes[key] = defaultAttributes[key] | |
144 | } else { | |
6b226c32 | 145 | booleanAttributes[key] = defaultBooleanAttributes[key] |
1205823f C |
146 | } |
147 | } | |
148 | ||
149 | const videoAttributes = { | |
ba5a8d89 C |
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 | |
1205823f C |
158 | } |
159 | ||
160 | Object.assign(videoAttributes, booleanAttributes) | |
161 | ||
ba5a8d89 | 162 | if (options.channelName) { |
89d241a7 | 163 | const videoChannel = await server.channels.get({ channelName: options.channelName }) |
1205823f C |
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 | ||
12152aa0 | 175 | function getServerCredentials (program: Command) { |
8d2be0ed | 176 | return Promise.all([ getSettings(), getNetrc() ]) |
a1587156 C |
177 | .then(([ settings, netrc ]) => { |
178 | return getRemoteObjectOrDie(program, settings, netrc) | |
179 | }) | |
8d2be0ed C |
180 | } |
181 | ||
254d3579 C |
182 | function buildServer (url: string) { |
183 | return new PeerTubeServer({ url }) | |
078f17e6 C |
184 | } |
185 | ||
254d3579 | 186 | async function assignToken (server: PeerTubeServer, username: string, password: string) { |
89d241a7 | 187 | const bodyClient = await server.login.getClient() |
d0a0fa42 C |
188 | const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } |
189 | ||
89d241a7 | 190 | const body = await server.login.login({ client, user: { username, password } }) |
d0a0fa42 C |
191 | |
192 | server.accessToken = body.access_token | |
193 | } | |
194 | ||
bda3b705 FL |
195 | function getLogger (logLevel = 'info') { |
196 | const logLevels = { | |
197 | 0: 0, | |
198 | error: 0, | |
199 | 1: 1, | |
200 | warn: 1, | |
201 | 2: 2, | |
202 | info: 2, | |
203 | 3: 3, | |
204 | verbose: 3, | |
205 | 4: 4, | |
206 | debug: 4 | |
207 | } | |
208 | ||
209 | const logger = createLogger({ | |
210 | levels: logLevels, | |
211 | format: format.combine( | |
212 | format.splat(), | |
213 | format.simple() | |
214 | ), | |
215 | transports: [ | |
216 | new (transports.Console)({ | |
217 | level: logLevel | |
218 | }) | |
219 | ] | |
220 | }) | |
221 | ||
222 | return logger | |
223 | } | |
224 | ||
8704acf4 RK |
225 | // --------------------------------------------------------------------------- |
226 | ||
227 | export { | |
228 | version, | |
bda3b705 | 229 | getLogger, |
8704acf4 | 230 | getSettings, |
2b4dd7e2 C |
231 | getNetrc, |
232 | getRemoteObjectOrDie, | |
1a12f66d | 233 | writeSettings, |
1205823f C |
234 | deleteSettings, |
235 | ||
8d2be0ed C |
236 | getServerCredentials, |
237 | ||
1205823f | 238 | buildCommonVideoOptions, |
26fcf2ef C |
239 | buildVideoAttributesFromCommander, |
240 | ||
078f17e6 | 241 | getAdminTokenOrDie, |
d0a0fa42 C |
242 | buildServer, |
243 | assignToken | |
8704acf4 | 244 | } |