X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fcli.ts;h=d1a631b69efbde489f49722689eb7e340f6d65fa;hb=daaaf3c487f188e54556c6d556d95ee6c2a31bce;hp=58e2445ac47f0c40e9f1e4f2cafa1ff8c860ca26;hpb=bda3b70537549d3b4f22a56f8fd020e88f19f2a5;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 58e2445ac..d1a631b69 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -6,6 +6,9 @@ import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels' import { Command } from 'commander' import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' import { createLogger, format, transports } from 'winston' +import { getMyUserInformation } from '@shared/extra-utils/users/users' +import { User, UserRole } from '@shared/models' +import { getAccessToken } from '@shared/extra-utils/users/login' let configName = 'PeerTube/CLI' if (isTestInstance()) configName += `-${getAppNumber()}` @@ -14,8 +17,21 @@ const config = require('application-config')(configName) const version = require('../../../package.json').version +async function getAdminTokenOrDie (url: string, username: string, password: string) { + const accessToken = await getAccessToken(url, username, password) + const resMe = await getMyUserInformation(url, accessToken) + const me: User = resMe.body + + if (me.role !== UserRole.ADMINISTRATOR) { + console.error('You must be an administrator.') + process.exit(-1) + } + + return accessToken +} + interface Settings { - remotes: any[], + remotes: any[] default: number } @@ -74,9 +90,9 @@ function getRemoteObjectOrDie ( if (!program['url'] || !program['username'] || !program['password']) { // No remote and we don't have program parameters: quit if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { - if (!program[ 'url' ]) console.error('--url field is required.') - if (!program[ 'username' ]) console.error('--username field is required.') - if (!program[ 'password' ]) console.error('--password field is required.') + if (!program['url']) console.error('--url field is required.') + if (!program['username']) console.error('--username field is required.') + if (!program['password']) console.error('--password field is required.') return process.exit(-1) } @@ -96,9 +112,9 @@ function getRemoteObjectOrDie ( } return { - url: program[ 'url' ], - username: program[ 'username' ], - password: program[ 'password' ] + url: program['url'], + username: program['username'], + password: program['password'] } } @@ -134,8 +150,8 @@ async function buildVideoAttributesFromCommander (url: string, command: Command, const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} for (const key of Object.keys(defaultBooleanAttributes)) { - if (command[ key ] !== undefined) { - booleanAttributes[key] = command[ key ] + if (command[key] !== undefined) { + booleanAttributes[key] = command[key] } else if (defaultAttributes[key] !== undefined) { booleanAttributes[key] = defaultAttributes[key] } else { @@ -144,19 +160,19 @@ async function buildVideoAttributesFromCommander (url: string, command: Command, } const videoAttributes = { - name: command[ 'videoName' ] || defaultAttributes.name, - category: command[ 'category' ] || defaultAttributes.category || undefined, - licence: command[ 'licence' ] || defaultAttributes.licence || undefined, - language: command[ 'language' ] || defaultAttributes.language || undefined, - privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC, - support: command[ 'support' ] || defaultAttributes.support || undefined, - description: command[ 'videoDescription' ] || defaultAttributes.description || undefined, - tags: command[ 'tags' ] || defaultAttributes.tags || undefined + name: command['videoName'] || defaultAttributes.name, + category: command['category'] || defaultAttributes.category || undefined, + licence: command['licence'] || defaultAttributes.licence || undefined, + language: command['language'] || defaultAttributes.language || undefined, + privacy: command['privacy'] || defaultAttributes.privacy || VideoPrivacy.PUBLIC, + support: command['support'] || defaultAttributes.support || undefined, + description: command['videoDescription'] || defaultAttributes.description || undefined, + tags: command['tags'] || defaultAttributes.tags || undefined } Object.assign(videoAttributes, booleanAttributes) - if (command[ 'channelName' ]) { + if (command['channelName']) { const res = await getVideoChannel(url, command['channelName']) const videoChannel: VideoChannel = res.body @@ -172,9 +188,9 @@ async function buildVideoAttributesFromCommander (url: string, command: Command, function getServerCredentials (program: any) { return Promise.all([ getSettings(), getNetrc() ]) - .then(([ settings, netrc ]) => { - return getRemoteObjectOrDie(program, settings, netrc) - }) + .then(([ settings, netrc ]) => { + return getRemoteObjectOrDie(program, settings, netrc) + }) } function getLogger (logLevel = 'info') { @@ -222,5 +238,7 @@ export { getServerCredentials, buildCommonVideoOptions, - buildVideoAttributesFromCommander + buildVideoAttributesFromCommander, + + getAdminTokenOrDie }