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