aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/cli.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/cli.ts')
-rw-r--r--server/tools/cli.ts44
1 files changed, 27 insertions, 17 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 7b94306cd..52e6ea593 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -1,14 +1,11 @@
1import { Command } from 'commander'
1import { Netrc } from 'netrc-parser' 2import { Netrc } from 'netrc-parser'
2import { getAppNumber, isTestInstance } from '../helpers/core-utils'
3import { join } from 'path' 3import { join } from 'path'
4import { root } from '../../shared/extra-utils/miscs/miscs'
5import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
6import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
7import { createLogger, format, transports } from 'winston' 4import { createLogger, format, transports } from 'winston'
8import { getMyUserInformation } from '@shared/extra-utils/users/users' 5import { PeerTubeServer } from '@shared/extra-utils'
9import { User, UserRole } from '@shared/models' 6import { UserRole } from '@shared/models'
10import { getAccessToken } from '@shared/extra-utils/users/login' 7import { VideoPrivacy } from '../../shared/models/videos'
11import { Command } from 'commander' 8import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
12 9
13let configName = 'PeerTube/CLI' 10let configName = 'PeerTube/CLI'
14if (isTestInstance()) configName += `-${getAppNumber()}` 11if (isTestInstance()) configName += `-${getAppNumber()}`
@@ -17,17 +14,16 @@ const config = require('application-config')(configName)
17 14
18const version = require('../../../package.json').version 15const version = require('../../../package.json').version
19 16
20async function getAdminTokenOrDie (url: string, username: string, password: string) { 17async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
21 const accessToken = await getAccessToken(url, username, password) 18 const token = await server.login.getAccessToken(username, password)
22 const resMe = await getMyUserInformation(url, accessToken) 19 const me = await server.users.getMyInfo({ token })
23 const me: User = resMe.body
24 20
25 if (me.role !== UserRole.ADMINISTRATOR) { 21 if (me.role !== UserRole.ADMINISTRATOR) {
26 console.error('You must be an administrator.') 22 console.error('You must be an administrator.')
27 process.exit(-1) 23 process.exit(-1)
28 } 24 }
29 25
30 return accessToken 26 return token
31} 27}
32 28
33interface Settings { 29interface Settings {
@@ -128,7 +124,7 @@ function buildCommonVideoOptions (command: Command) {
128 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') 124 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
129} 125}
130 126
131async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { 127async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
132 const options = command.opts() 128 const options = command.opts()
133 129
134 const defaultBooleanAttributes = { 130 const defaultBooleanAttributes = {
@@ -164,8 +160,7 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
164 Object.assign(videoAttributes, booleanAttributes) 160 Object.assign(videoAttributes, booleanAttributes)
165 161
166 if (options.channelName) { 162 if (options.channelName) {
167 const res = await getVideoChannel(url, options.channelName) 163 const videoChannel = await server.channels.get({ channelName: options.channelName })
168 const videoChannel: VideoChannel = res.body
169 164
170 Object.assign(videoAttributes, { channelId: videoChannel.id }) 165 Object.assign(videoAttributes, { channelId: videoChannel.id })
171 166
@@ -184,6 +179,19 @@ function getServerCredentials (program: Command) {
184 }) 179 })
185} 180}
186 181
182function buildServer (url: string) {
183 return new PeerTubeServer({ url })
184}
185
186async function assignToken (server: PeerTubeServer, username: string, password: string) {
187 const bodyClient = await server.login.getClient()
188 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
189
190 const body = await server.login.login({ client, user: { username, password } })
191
192 server.accessToken = body.access_token
193}
194
187function getLogger (logLevel = 'info') { 195function getLogger (logLevel = 'info') {
188 const logLevels = { 196 const logLevels = {
189 0: 0, 197 0: 0,
@@ -230,5 +238,7 @@ export {
230 buildCommonVideoOptions, 238 buildCommonVideoOptions,
231 buildVideoAttributesFromCommander, 239 buildVideoAttributesFromCommander,
232 240
233 getAdminTokenOrDie 241 getAdminTokenOrDie,
242 buildServer,
243 assignToken
234} 244}