aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/shared/cli.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-02-15 16:30:33 +0100
committerChocobozzz <me@florianbigard.com>2023-02-15 16:30:33 +0100
commit1a73a7dcd0d38bebbb2cf7067851fc91b8b7f0d3 (patch)
tree8689f5121ea0ac09c9181a967083c48e6acc822d /server/tools/shared/cli.ts
parent1ec4835dcac769d0711bbfeaa63b4e1743fde265 (diff)
downloadPeerTube-1a73a7dcd0d38bebbb2cf7067851fc91b8b7f0d3.tar.gz
PeerTube-1a73a7dcd0d38bebbb2cf7067851fc91b8b7f0d3.tar.zst
PeerTube-1a73a7dcd0d38bebbb2cf7067851fc91b8b7f0d3.zip
Move cli.ts in a private shared directory
It's only used by cli scripts of this directory
Diffstat (limited to 'server/tools/shared/cli.ts')
-rw-r--r--server/tools/shared/cli.ts262
1 files changed, 262 insertions, 0 deletions
diff --git a/server/tools/shared/cli.ts b/server/tools/shared/cli.ts
new file mode 100644
index 000000000..e010ab320
--- /dev/null
+++ b/server/tools/shared/cli.ts
@@ -0,0 +1,262 @@
1import { Command } from 'commander'
2import { Netrc } from 'netrc-parser'
3import { join } from 'path'
4import { createLogger, format, transports } from 'winston'
5import { getAppNumber, isTestInstance } from '@server/helpers/core-utils'
6import { loadLanguages } from '@server/initializers/constants'
7import { root } from '@shared/core-utils'
8import { UserRole, VideoPrivacy } from '@shared/models'
9import { PeerTubeServer } from '@shared/server-commands'
10
11let configName = 'PeerTube/CLI'
12if (isTestInstance()) configName += `-${getAppNumber()}`
13
14const config = require('application-config')(configName)
15
16const version = require(join(root(), 'package.json')).version
17
18async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
19 const token = await server.login.getAccessToken(username, password)
20 const me = await server.users.getMyInfo({ token })
21
22 if (me.role.id !== UserRole.ADMINISTRATOR) {
23 console.error('You must be an administrator.')
24 process.exit(-1)
25 }
26
27 return token
28}
29
30interface Settings {
31 remotes: any[]
32 default: number
33}
34
35async function getSettings (): Promise<Settings> {
36 const defaultSettings = {
37 remotes: [],
38 default: -1
39 }
40
41 const data = await config.read()
42
43 return Object.keys(data).length === 0
44 ? defaultSettings
45 : data
46}
47
48async function getNetrc () {
49 const Netrc = require('netrc-parser').Netrc
50
51 const netrc = isTestInstance()
52 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
53 : new Netrc()
54
55 await netrc.load()
56
57 return netrc
58}
59
60function writeSettings (settings: Settings) {
61 return config.write(settings)
62}
63
64function deleteSettings () {
65 return config.trash()
66}
67
68function getRemoteObjectOrDie (
69 program: Command,
70 settings: Settings,
71 netrc: Netrc
72): { url: string, username: string, password: string } {
73 const options = program.opts()
74
75 function exitIfNoOptions (optionNames: string[], errorPrefix: string = '') {
76 let exit = false
77
78 for (const key of optionNames) {
79 if (!options[key]) {
80 if (exit === false && errorPrefix) console.error(errorPrefix)
81
82 console.error(`--${key} field is required`)
83 exit = true
84 }
85 }
86
87 if (exit) process.exit(-1)
88 }
89
90 // If username or password are specified, both are mandatory
91 if (options.username || options.password) {
92 exitIfNoOptions([ 'username', 'password' ])
93 }
94
95 // If no available machines, url, username and password args are mandatory
96 if (Object.keys(netrc.machines).length === 0) {
97 exitIfNoOptions([ 'url', 'username', 'password' ], 'No account found in netrc')
98 }
99
100 if (settings.remotes.length === 0 || settings.default === -1) {
101 exitIfNoOptions([ 'url' ], 'No default instance found')
102 }
103
104 let url: string = options.url
105 let username: string = options.username
106 let password: string = options.password
107
108 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
109
110 const machine = netrc.machines[url]
111 if ((!username || !password) && !machine) {
112 console.error('Cannot find existing configuration for %s.', url)
113 process.exit(-1)
114 }
115
116 if (!username && machine) username = machine.login
117 if (!password && machine) password = machine.password
118
119 return { url, username, password }
120}
121
122function buildCommonVideoOptions (command: Command) {
123 function list (val) {
124 return val.split(',')
125 }
126
127 return command
128 .option('-n, --video-name <name>', 'Video name')
129 .option('-c, --category <category_number>', 'Category number')
130 .option('-l, --licence <licence_number>', 'Licence number')
131 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
132 .option('-t, --tags <tags>', 'Video tags', list)
133 .option('-N, --nsfw', 'Video is Not Safe For Work')
134 .option('-d, --video-description <description>', 'Video description')
135 .option('-P, --privacy <privacy_number>', 'Privacy')
136 .option('-C, --channel-name <channel_name>', 'Channel name')
137 .option('--no-comments-enabled', 'Disable video comments')
138 .option('-s, --support <support>', 'Video support text')
139 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
140 .option('--no-download-enabled', 'Disable video download')
141 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
142}
143
144async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
145 const options = command.opts()
146
147 const defaultBooleanAttributes = {
148 nsfw: false,
149 commentsEnabled: true,
150 downloadEnabled: true,
151 waitTranscoding: true
152 }
153
154 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
155
156 for (const key of Object.keys(defaultBooleanAttributes)) {
157 if (options[key] !== undefined) {
158 booleanAttributes[key] = options[key]
159 } else if (defaultAttributes[key] !== undefined) {
160 booleanAttributes[key] = defaultAttributes[key]
161 } else {
162 booleanAttributes[key] = defaultBooleanAttributes[key]
163 }
164 }
165
166 const videoAttributes = {
167 name: options.videoName || defaultAttributes.name,
168 category: options.category || defaultAttributes.category || undefined,
169 licence: options.licence || defaultAttributes.licence || undefined,
170 language: options.language || defaultAttributes.language || undefined,
171 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
172 support: options.support || defaultAttributes.support || undefined,
173 description: options.videoDescription || defaultAttributes.description || undefined,
174 tags: options.tags || defaultAttributes.tags || undefined
175 }
176
177 Object.assign(videoAttributes, booleanAttributes)
178
179 if (options.channelName) {
180 const videoChannel = await server.channels.get({ channelName: options.channelName })
181
182 Object.assign(videoAttributes, { channelId: videoChannel.id })
183
184 if (!videoAttributes.support && videoChannel.support) {
185 Object.assign(videoAttributes, { support: videoChannel.support })
186 }
187 }
188
189 return videoAttributes
190}
191
192function getServerCredentials (program: Command) {
193 return Promise.all([ getSettings(), getNetrc() ])
194 .then(([ settings, netrc ]) => {
195 return getRemoteObjectOrDie(program, settings, netrc)
196 })
197}
198
199function buildServer (url: string) {
200 loadLanguages()
201 return new PeerTubeServer({ url })
202}
203
204async function assignToken (server: PeerTubeServer, username: string, password: string) {
205 const bodyClient = await server.login.getClient()
206 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
207
208 const body = await server.login.login({ client, user: { username, password } })
209
210 server.accessToken = body.access_token
211}
212
213function getLogger (logLevel = 'info') {
214 const logLevels = {
215 0: 0,
216 error: 0,
217 1: 1,
218 warn: 1,
219 2: 2,
220 info: 2,
221 3: 3,
222 verbose: 3,
223 4: 4,
224 debug: 4
225 }
226
227 const logger = createLogger({
228 levels: logLevels,
229 format: format.combine(
230 format.splat(),
231 format.simple()
232 ),
233 transports: [
234 new (transports.Console)({
235 level: logLevel
236 })
237 ]
238 })
239
240 return logger
241}
242
243// ---------------------------------------------------------------------------
244
245export {
246 version,
247 getLogger,
248 getSettings,
249 getNetrc,
250 getRemoteObjectOrDie,
251 writeSettings,
252 deleteSettings,
253
254 getServerCredentials,
255
256 buildCommonVideoOptions,
257 buildVideoAttributesFromCommander,
258
259 getAdminTokenOrDie,
260 buildServer,
261 assignToken
262}