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