diff options
Diffstat (limited to 'server/tools/cli.ts')
-rw-r--r-- | server/tools/cli.ts | 44 |
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 @@ | |||
1 | import { Command } from 'commander' | ||
1 | import { Netrc } from 'netrc-parser' | 2 | import { Netrc } from 'netrc-parser' |
2 | import { getAppNumber, isTestInstance } from '../helpers/core-utils' | ||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { root } from '../../shared/extra-utils/miscs/miscs' | ||
5 | import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels' | ||
6 | import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' | ||
7 | import { createLogger, format, transports } from 'winston' | 4 | import { createLogger, format, transports } from 'winston' |
8 | import { getMyUserInformation } from '@shared/extra-utils/users/users' | 5 | import { PeerTubeServer } from '@shared/extra-utils' |
9 | import { User, UserRole } from '@shared/models' | 6 | import { UserRole } from '@shared/models' |
10 | import { getAccessToken } from '@shared/extra-utils/users/login' | 7 | import { VideoPrivacy } from '../../shared/models/videos' |
11 | import { Command } from 'commander' | 8 | import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' |
12 | 9 | ||
13 | let configName = 'PeerTube/CLI' | 10 | let configName = 'PeerTube/CLI' |
14 | if (isTestInstance()) configName += `-${getAppNumber()}` | 11 | if (isTestInstance()) configName += `-${getAppNumber()}` |
@@ -17,17 +14,16 @@ const config = require('application-config')(configName) | |||
17 | 14 | ||
18 | const version = require('../../../package.json').version | 15 | const version = require('../../../package.json').version |
19 | 16 | ||
20 | async function getAdminTokenOrDie (url: string, username: string, password: string) { | 17 | async 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 | ||
33 | interface Settings { | 29 | interface 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 | ||
131 | async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { | 127 | async 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 | ||
182 | function buildServer (url: string) { | ||
183 | return new PeerTubeServer({ url }) | ||
184 | } | ||
185 | |||
186 | async 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 | |||
187 | function getLogger (logLevel = 'info') { | 195 | function 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 | } |