]>
Commit | Line | Data |
---|---|---|
1a12f66d | 1 | import { Netrc } from 'netrc-parser' |
1205823f | 2 | import { getAppNumber, isTestInstance } from '../helpers/core-utils' |
1a12f66d | 3 | import { join } from 'path' |
8d2be0ed C |
4 | import { root } from '../../shared/extra-utils/miscs/miscs' |
5 | import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels' | |
1205823f C |
6 | import { Command } from 'commander' |
7 | import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' | |
bda3b705 | 8 | import { createLogger, format, transports } from 'winston' |
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 | |
8704acf4 RK |
17 | interface Settings { |
18 | remotes: any[], | |
19 | default: number | |
20 | } | |
21 | ||
2b4dd7e2 | 22 | function getSettings () { |
8704acf4 | 23 | return new Promise<Settings>((res, rej) => { |
2b4dd7e2 | 24 | const defaultSettings = { |
8704acf4 | 25 | remotes: [], |
1a12f66d | 26 | default: -1 |
2b4dd7e2 C |
27 | } |
28 | ||
8704acf4 | 29 | config.read((err, data) => { |
2b4dd7e2 C |
30 | if (err) return rej(err) |
31 | ||
32 | return res(Object.keys(data).length === 0 ? defaultSettings : data) | |
8704acf4 RK |
33 | }) |
34 | }) | |
35 | } | |
36 | ||
2b4dd7e2 | 37 | async function getNetrc () { |
1a12f66d C |
38 | const Netrc = require('netrc-parser').Netrc |
39 | ||
40 | const netrc = isTestInstance() | |
41 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | |
42 | : new Netrc() | |
43 | ||
2b4dd7e2 C |
44 | await netrc.load() |
45 | ||
46 | return netrc | |
47 | } | |
48 | ||
49 | function writeSettings (settings) { | |
8704acf4 | 50 | return new Promise((res, rej) => { |
1a12f66d C |
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) => { | |
2b4dd7e2 C |
62 | if (err) return rej(err) |
63 | ||
8704acf4 RK |
64 | return res() |
65 | }) | |
66 | }) | |
67 | } | |
68 | ||
8d2be0ed C |
69 | function getRemoteObjectOrDie ( |
70 | program: any, | |
71 | settings: Settings, | |
72 | netrc: Netrc | |
73 | ): { url: string, username: string, password: string } { | |
2b4dd7e2 | 74 | if (!program['url'] || !program['username'] || !program['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) { |
2b4dd7e2 C |
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 | ||
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 { | |
99 | url: program[ 'url' ], | |
100 | username: program[ 'username' ], | |
101 | password: program[ 'password' ] | |
102 | } | |
103 | } | |
8704acf4 | 104 | |
1205823f C |
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') | |
bda3b705 | 123 | .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') |
1205823f C |
124 | } |
125 | ||
6f530096 | 126 | async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { |
6b226c32 C |
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 } | {} = {} | |
1205823f | 135 | |
6b226c32 | 136 | for (const key of Object.keys(defaultBooleanAttributes)) { |
1205823f C |
137 | if (command[ key ] !== undefined) { |
138 | booleanAttributes[key] = command[ key ] | |
139 | } else if (defaultAttributes[key] !== undefined) { | |
140 | booleanAttributes[key] = defaultAttributes[key] | |
141 | } else { | |
6b226c32 | 142 | booleanAttributes[key] = defaultBooleanAttributes[key] |
1205823f C |
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, | |
6f530096 C |
152 | support: command[ 'support' ] || defaultAttributes.support || undefined, |
153 | description: command[ 'videoDescription' ] || defaultAttributes.description || undefined, | |
154 | tags: command[ 'tags' ] || defaultAttributes.tags || undefined | |
1205823f C |
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 | ||
8d2be0ed C |
173 | function getServerCredentials (program: any) { |
174 | return Promise.all([ getSettings(), getNetrc() ]) | |
175 | .then(([ settings, netrc ]) => { | |
176 | return getRemoteObjectOrDie(program, settings, netrc) | |
177 | }) | |
178 | } | |
179 | ||
bda3b705 FL |
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 | ||
8704acf4 RK |
210 | // --------------------------------------------------------------------------- |
211 | ||
212 | export { | |
213 | version, | |
214 | config, | |
bda3b705 | 215 | getLogger, |
8704acf4 | 216 | getSettings, |
2b4dd7e2 C |
217 | getNetrc, |
218 | getRemoteObjectOrDie, | |
1a12f66d | 219 | writeSettings, |
1205823f C |
220 | deleteSettings, |
221 | ||
8d2be0ed C |
222 | getServerCredentials, |
223 | ||
1205823f C |
224 | buildCommonVideoOptions, |
225 | buildVideoAttributesFromCommander | |
8704acf4 | 226 | } |