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