]>
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' | |
1a12f66d C |
8 | |
9 | let configName = 'PeerTube/CLI' | |
10 | if (isTestInstance()) configName += `-${getAppNumber()}` | |
11 | ||
12 | const config = require('application-config')(configName) | |
8704acf4 | 13 | |
499d9015 | 14 | const version = require('../../../package.json').version |
8704acf4 | 15 | |
8704acf4 RK |
16 | interface Settings { |
17 | remotes: any[], | |
18 | default: number | |
19 | } | |
20 | ||
2b4dd7e2 | 21 | function getSettings () { |
8704acf4 | 22 | return new Promise<Settings>((res, rej) => { |
2b4dd7e2 | 23 | const defaultSettings = { |
8704acf4 | 24 | remotes: [], |
1a12f66d | 25 | default: -1 |
2b4dd7e2 C |
26 | } |
27 | ||
8704acf4 | 28 | config.read((err, data) => { |
2b4dd7e2 C |
29 | if (err) return rej(err) |
30 | ||
31 | return res(Object.keys(data).length === 0 ? defaultSettings : data) | |
8704acf4 RK |
32 | }) |
33 | }) | |
34 | } | |
35 | ||
2b4dd7e2 | 36 | async function getNetrc () { |
1a12f66d C |
37 | const Netrc = require('netrc-parser').Netrc |
38 | ||
39 | const netrc = isTestInstance() | |
40 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | |
41 | : new Netrc() | |
42 | ||
2b4dd7e2 C |
43 | await netrc.load() |
44 | ||
45 | return netrc | |
46 | } | |
47 | ||
48 | function writeSettings (settings) { | |
8704acf4 | 49 | return new Promise((res, rej) => { |
1a12f66d C |
50 | config.write(settings, err => { |
51 | if (err) return rej(err) | |
52 | ||
53 | return res() | |
54 | }) | |
55 | }) | |
56 | } | |
57 | ||
58 | function deleteSettings () { | |
59 | return new Promise((res, rej) => { | |
60 | config.trash((err) => { | |
2b4dd7e2 C |
61 | if (err) return rej(err) |
62 | ||
8704acf4 RK |
63 | return res() |
64 | }) | |
65 | }) | |
66 | } | |
67 | ||
8d2be0ed C |
68 | function getRemoteObjectOrDie ( |
69 | program: any, | |
70 | settings: Settings, | |
71 | netrc: Netrc | |
72 | ): { url: string, username: string, password: string } { | |
2b4dd7e2 | 73 | if (!program['url'] || !program['username'] || !program['password']) { |
1a12f66d | 74 | // No remote and we don't have program parameters: quit |
9f167f12 | 75 | if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { |
2b4dd7e2 C |
76 | if (!program[ 'url' ]) console.error('--url field is required.') |
77 | if (!program[ 'username' ]) console.error('--username field is required.') | |
78 | if (!program[ 'password' ]) console.error('--password field is required.') | |
79 | ||
80 | return process.exit(-1) | |
81 | } | |
82 | ||
83 | let url: string = program['url'] | |
84 | let username: string = program['username'] | |
85 | let password: string = program['password'] | |
86 | ||
1a12f66d | 87 | if (!url && settings.default !== -1) url = settings.remotes[settings.default] |
2b4dd7e2 | 88 | |
9f167f12 | 89 | const machine = netrc.machines[url] |
1a12f66d C |
90 | |
91 | if (!username && machine) username = machine.login | |
92 | if (!password && machine) password = machine.password | |
2b4dd7e2 C |
93 | |
94 | return { url, username, password } | |
95 | } | |
96 | ||
97 | return { | |
98 | url: program[ 'url' ], | |
99 | username: program[ 'username' ], | |
100 | password: program[ 'password' ] | |
101 | } | |
102 | } | |
8704acf4 | 103 | |
1205823f C |
104 | function buildCommonVideoOptions (command: Command) { |
105 | function list (val) { | |
106 | return val.split(',') | |
107 | } | |
108 | ||
109 | return command | |
110 | .option('-n, --video-name <name>', 'Video name') | |
111 | .option('-c, --category <category_number>', 'Category number') | |
112 | .option('-l, --licence <licence_number>', 'Licence number') | |
113 | .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)') | |
114 | .option('-t, --tags <tags>', 'Video tags', list) | |
115 | .option('-N, --nsfw', 'Video is Not Safe For Work') | |
116 | .option('-d, --video-description <description>', 'Video description') | |
117 | .option('-P, --privacy <privacy_number>', 'Privacy') | |
118 | .option('-C, --channel-name <channel_name>', 'Channel name') | |
119 | .option('-m, --comments-enabled', 'Enable comments') | |
120 | .option('-s, --support <support>', 'Video support text') | |
121 | .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video') | |
122 | } | |
123 | ||
6f530096 | 124 | async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { |
6b226c32 C |
125 | const defaultBooleanAttributes = { |
126 | nsfw: false, | |
127 | commentsEnabled: true, | |
128 | downloadEnabled: true, | |
129 | waitTranscoding: true | |
130 | } | |
131 | ||
132 | const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} | |
1205823f | 133 | |
6b226c32 | 134 | for (const key of Object.keys(defaultBooleanAttributes)) { |
1205823f C |
135 | if (command[ key ] !== undefined) { |
136 | booleanAttributes[key] = command[ key ] | |
137 | } else if (defaultAttributes[key] !== undefined) { | |
138 | booleanAttributes[key] = defaultAttributes[key] | |
139 | } else { | |
6b226c32 | 140 | booleanAttributes[key] = defaultBooleanAttributes[key] |
1205823f C |
141 | } |
142 | } | |
143 | ||
144 | const videoAttributes = { | |
145 | name: command[ 'videoName' ] || defaultAttributes.name, | |
146 | category: command[ 'category' ] || defaultAttributes.category || undefined, | |
147 | licence: command[ 'licence' ] || defaultAttributes.licence || undefined, | |
148 | language: command[ 'language' ] || defaultAttributes.language || undefined, | |
149 | privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC, | |
6f530096 C |
150 | support: command[ 'support' ] || defaultAttributes.support || undefined, |
151 | description: command[ 'videoDescription' ] || defaultAttributes.description || undefined, | |
152 | tags: command[ 'tags' ] || defaultAttributes.tags || undefined | |
1205823f C |
153 | } |
154 | ||
155 | Object.assign(videoAttributes, booleanAttributes) | |
156 | ||
157 | if (command[ 'channelName' ]) { | |
158 | const res = await getVideoChannel(url, command['channelName']) | |
159 | const videoChannel: VideoChannel = res.body | |
160 | ||
161 | Object.assign(videoAttributes, { channelId: videoChannel.id }) | |
162 | ||
163 | if (!videoAttributes.support && videoChannel.support) { | |
164 | Object.assign(videoAttributes, { support: videoChannel.support }) | |
165 | } | |
166 | } | |
167 | ||
168 | return videoAttributes | |
169 | } | |
170 | ||
8d2be0ed C |
171 | function getServerCredentials (program: any) { |
172 | return Promise.all([ getSettings(), getNetrc() ]) | |
173 | .then(([ settings, netrc ]) => { | |
174 | return getRemoteObjectOrDie(program, settings, netrc) | |
175 | }) | |
176 | } | |
177 | ||
8704acf4 RK |
178 | // --------------------------------------------------------------------------- |
179 | ||
180 | export { | |
181 | version, | |
182 | config, | |
8704acf4 | 183 | getSettings, |
2b4dd7e2 C |
184 | getNetrc, |
185 | getRemoteObjectOrDie, | |
1a12f66d | 186 | writeSettings, |
1205823f C |
187 | deleteSettings, |
188 | ||
8d2be0ed C |
189 | getServerCredentials, |
190 | ||
1205823f C |
191 | buildCommonVideoOptions, |
192 | buildVideoAttributesFromCommander | |
8704acf4 | 193 | } |