]>
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 { Command } from 'commander' | |
7 | import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' | |
8 | import { createLogger, format, transports } from 'winston' | |
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 | interface Settings { | |
18 | remotes: any[], | |
19 | default: number | |
20 | } | |
21 | ||
22 | function getSettings () { | |
23 | return new Promise<Settings>((res, rej) => { | |
24 | const defaultSettings = { | |
25 | remotes: [], | |
26 | default: -1 | |
27 | } | |
28 | ||
29 | config.read((err, data) => { | |
30 | if (err) return rej(err) | |
31 | ||
32 | return res(Object.keys(data).length === 0 ? defaultSettings : data) | |
33 | }) | |
34 | }) | |
35 | } | |
36 | ||
37 | async function getNetrc () { | |
38 | const Netrc = require('netrc-parser').Netrc | |
39 | ||
40 | const netrc = isTestInstance() | |
41 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | |
42 | : new Netrc() | |
43 | ||
44 | await netrc.load() | |
45 | ||
46 | return netrc | |
47 | } | |
48 | ||
49 | function writeSettings (settings) { | |
50 | return new Promise((res, rej) => { | |
51 | config.write(settings, err => { | |
52 | if (err) return rej(err) | |
53 | ||
54 | return res() | |
55 | }) | |
56 | }) | |
57 | } | |
58 | ||
59 | function deleteSettings () { | |
60 | return new Promise((res, rej) => { | |
61 | config.trash((err) => { | |
62 | if (err) return rej(err) | |
63 | ||
64 | return res() | |
65 | }) | |
66 | }) | |
67 | } | |
68 | ||
69 | function getRemoteObjectOrDie ( | |
70 | program: any, | |
71 | settings: Settings, | |
72 | netrc: Netrc | |
73 | ): { url: string, username: string, password: string } { | |
74 | if (!program['url'] || !program['username'] || !program['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 (!program[ 'url' ]) console.error('--url field is required.') | |
78 | if (!program[ 'username' ]) console.error('--username field is required.') | |
79 | if (!program[ 'password' ]) console.error('--password field is required.') | |
80 | ||
81 | return process.exit(-1) | |
82 | } | |
83 | ||
84 | let url: string = program['url'] | |
85 | let username: string = program['username'] | |
86 | let password: string = program['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: program[ 'url' ], | |
100 | username: program[ 'username' ], | |
101 | password: program[ '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('-m, --comments-enabled', 'Enable comments') | |
121 | .option('-s, --support <support>', 'Video support text') | |
122 | .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video') | |
123 | .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') | |
124 | } | |
125 | ||
126 | async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { | |
127 | const defaultBooleanAttributes = { | |
128 | nsfw: false, | |
129 | commentsEnabled: true, | |
130 | downloadEnabled: true, | |
131 | waitTranscoding: true | |
132 | } | |
133 | ||
134 | const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} | |
135 | ||
136 | for (const key of Object.keys(defaultBooleanAttributes)) { | |
137 | if (command[ key ] !== undefined) { | |
138 | booleanAttributes[key] = command[ key ] | |
139 | } else if (defaultAttributes[key] !== undefined) { | |
140 | booleanAttributes[key] = defaultAttributes[key] | |
141 | } else { | |
142 | booleanAttributes[key] = defaultBooleanAttributes[key] | |
143 | } | |
144 | } | |
145 | ||
146 | const videoAttributes = { | |
147 | name: command[ 'videoName' ] || defaultAttributes.name, | |
148 | category: command[ 'category' ] || defaultAttributes.category || undefined, | |
149 | licence: command[ 'licence' ] || defaultAttributes.licence || undefined, | |
150 | language: command[ 'language' ] || defaultAttributes.language || undefined, | |
151 | privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC, | |
152 | support: command[ 'support' ] || defaultAttributes.support || undefined, | |
153 | description: command[ 'videoDescription' ] || defaultAttributes.description || undefined, | |
154 | tags: command[ 'tags' ] || defaultAttributes.tags || undefined | |
155 | } | |
156 | ||
157 | Object.assign(videoAttributes, booleanAttributes) | |
158 | ||
159 | if (command[ 'channelName' ]) { | |
160 | const res = await getVideoChannel(url, command['channelName']) | |
161 | const videoChannel: VideoChannel = res.body | |
162 | ||
163 | Object.assign(videoAttributes, { channelId: videoChannel.id }) | |
164 | ||
165 | if (!videoAttributes.support && videoChannel.support) { | |
166 | Object.assign(videoAttributes, { support: videoChannel.support }) | |
167 | } | |
168 | } | |
169 | ||
170 | return videoAttributes | |
171 | } | |
172 | ||
173 | function getServerCredentials (program: any) { | |
174 | return Promise.all([ getSettings(), getNetrc() ]) | |
175 | .then(([ settings, netrc ]) => { | |
176 | return getRemoteObjectOrDie(program, settings, netrc) | |
177 | }) | |
178 | } | |
179 | ||
180 | function getLogger (logLevel = 'info') { | |
181 | const logLevels = { | |
182 | 0: 0, | |
183 | error: 0, | |
184 | 1: 1, | |
185 | warn: 1, | |
186 | 2: 2, | |
187 | info: 2, | |
188 | 3: 3, | |
189 | verbose: 3, | |
190 | 4: 4, | |
191 | debug: 4 | |
192 | } | |
193 | ||
194 | const logger = createLogger({ | |
195 | levels: logLevels, | |
196 | format: format.combine( | |
197 | format.splat(), | |
198 | format.simple() | |
199 | ), | |
200 | transports: [ | |
201 | new (transports.Console)({ | |
202 | level: logLevel | |
203 | }) | |
204 | ] | |
205 | }) | |
206 | ||
207 | return logger | |
208 | } | |
209 | ||
210 | // --------------------------------------------------------------------------- | |
211 | ||
212 | export { | |
213 | version, | |
214 | config, | |
215 | getLogger, | |
216 | getSettings, | |
217 | getNetrc, | |
218 | getRemoteObjectOrDie, | |
219 | writeSettings, | |
220 | deleteSettings, | |
221 | ||
222 | getServerCredentials, | |
223 | ||
224 | buildCommonVideoOptions, | |
225 | buildVideoAttributesFromCommander | |
226 | } |