Adapt CLI to new commands
This commit is contained in:
parent
41d1d07501
commit
d0a0fa429d
|
@ -3,12 +3,10 @@ import { Netrc } from 'netrc-parser'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { createLogger, format, transports } from 'winston'
|
import { createLogger, format, transports } from 'winston'
|
||||||
import { assignCommands, ServerInfo } from '@shared/extra-utils'
|
import { assignCommands, ServerInfo } from '@shared/extra-utils'
|
||||||
import { getAccessToken } from '@shared/extra-utils/users/login'
|
|
||||||
import { getMyUserInformation } from '@shared/extra-utils/users/users'
|
import { getMyUserInformation } from '@shared/extra-utils/users/users'
|
||||||
import { User, UserRole } from '@shared/models'
|
import { User, UserRole } from '@shared/models'
|
||||||
import { root } from '../../shared/extra-utils/miscs/miscs'
|
|
||||||
import { VideoPrivacy } from '../../shared/models/videos'
|
import { VideoPrivacy } from '../../shared/models/videos'
|
||||||
import { getAppNumber, isTestInstance } from '../helpers/core-utils'
|
import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
|
||||||
|
|
||||||
let configName = 'PeerTube/CLI'
|
let configName = 'PeerTube/CLI'
|
||||||
if (isTestInstance()) configName += `-${getAppNumber()}`
|
if (isTestInstance()) configName += `-${getAppNumber()}`
|
||||||
|
@ -17,9 +15,9 @@ const config = require('application-config')(configName)
|
||||||
|
|
||||||
const version = require('../../../package.json').version
|
const version = require('../../../package.json').version
|
||||||
|
|
||||||
async function getAdminTokenOrDie (url: string, username: string, password: string) {
|
async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) {
|
||||||
const accessToken = await getAccessToken(url, username, password)
|
const accessToken = await server.loginCommand.getAccessToken(username, password)
|
||||||
const resMe = await getMyUserInformation(url, accessToken)
|
const resMe = await getMyUserInformation(server.url, accessToken)
|
||||||
const me: User = resMe.body
|
const me: User = resMe.body
|
||||||
|
|
||||||
if (me.role !== UserRole.ADMINISTRATOR) {
|
if (me.role !== UserRole.ADMINISTRATOR) {
|
||||||
|
@ -30,10 +28,6 @@ async function getAdminTokenOrDie (url: string, username: string, password: stri
|
||||||
return accessToken
|
return accessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAccessTokenOrDie (url: string, username: string, password: string) {
|
|
||||||
return getAccessToken(url, username, password)
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Settings {
|
interface Settings {
|
||||||
remotes: any[]
|
remotes: any[]
|
||||||
default: number
|
default: number
|
||||||
|
@ -187,13 +181,22 @@ function getServerCredentials (program: Command) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildServer (url: string, accessToken?: string): ServerInfo {
|
function buildServer (url: string): ServerInfo {
|
||||||
const server = { url, accessToken, internalServerNumber: undefined }
|
const server = { url, internalServerNumber: undefined }
|
||||||
assignCommands(server)
|
assignCommands(server)
|
||||||
|
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function assignToken (server: ServerInfo, username: string, password: string) {
|
||||||
|
const bodyClient = await server.loginCommand.getClient()
|
||||||
|
const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
|
||||||
|
|
||||||
|
const body = await server.loginCommand.login({ client, user: { username, password } })
|
||||||
|
|
||||||
|
server.accessToken = body.access_token
|
||||||
|
}
|
||||||
|
|
||||||
function getLogger (logLevel = 'info') {
|
function getLogger (logLevel = 'info') {
|
||||||
const logLevels = {
|
const logLevels = {
|
||||||
0: 0,
|
0: 0,
|
||||||
|
@ -241,6 +244,6 @@ export {
|
||||||
buildVideoAttributesFromCommander,
|
buildVideoAttributesFromCommander,
|
||||||
|
|
||||||
getAdminTokenOrDie,
|
getAdminTokenOrDie,
|
||||||
getAccessTokenOrDie,
|
buildServer,
|
||||||
buildServer
|
assignToken
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,8 @@ registerTSPaths()
|
||||||
|
|
||||||
import { OptionValues, program } from 'commander'
|
import { OptionValues, program } from 'commander'
|
||||||
import * as prompt from 'prompt'
|
import * as prompt from 'prompt'
|
||||||
import { getNetrc, getSettings, writeSettings } from './cli'
|
import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
|
||||||
import { isUserUsernameValid } from '../helpers/custom-validators/users'
|
import { isUserUsernameValid } from '../helpers/custom-validators/users'
|
||||||
import { getAccessToken } from '../../shared/extra-utils'
|
|
||||||
import * as CliTable3 from 'cli-table3'
|
import * as CliTable3 from 'cli-table3'
|
||||||
|
|
||||||
async function delInstance (url: string) {
|
async function delInstance (url: string) {
|
||||||
|
@ -97,7 +96,8 @@ program
|
||||||
// @see https://github.com/Chocobozzz/PeerTube/issues/3520
|
// @see https://github.com/Chocobozzz/PeerTube/issues/3520
|
||||||
result.url = stripExtraneousFromPeerTubeUrl(result.url)
|
result.url = stripExtraneousFromPeerTubeUrl(result.url)
|
||||||
|
|
||||||
await getAccessToken(result.url, result.username, result.password)
|
const server = buildServer(result.url)
|
||||||
|
await assignToken(server, result.username, result.password)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.message)
|
console.error(err.message)
|
||||||
process.exit(-1)
|
process.exit(-1)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
|
||||||
registerTSPaths()
|
registerTSPaths()
|
||||||
|
|
||||||
import { program } from 'commander'
|
import { program } from 'commander'
|
||||||
import { getAccessToken } from '../../shared/extra-utils'
|
import { assignToken, buildServer } from './cli'
|
||||||
|
|
||||||
program
|
program
|
||||||
.option('-u, --url <url>', 'Server url')
|
.option('-u, --url <url>', 'Server url')
|
||||||
|
@ -24,9 +24,11 @@ if (
|
||||||
process.exit(-1)
|
process.exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccessToken(options.url, options.username, options.password)
|
const server = buildServer(options.url)
|
||||||
.then(accessToken => {
|
|
||||||
console.log(accessToken)
|
assignToken(server, options.username, options.password)
|
||||||
|
.then(() => {
|
||||||
|
console.log(server.accessToken)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|
|
@ -14,10 +14,10 @@ import { sha256 } from '../helpers/core-utils'
|
||||||
import { doRequestAndSaveToFile } from '../helpers/requests'
|
import { doRequestAndSaveToFile } from '../helpers/requests'
|
||||||
import { CONSTRAINTS_FIELDS } from '../initializers/constants'
|
import { CONSTRAINTS_FIELDS } from '../initializers/constants'
|
||||||
import {
|
import {
|
||||||
|
assignToken,
|
||||||
buildCommonVideoOptions,
|
buildCommonVideoOptions,
|
||||||
buildServer,
|
buildServer,
|
||||||
buildVideoAttributesFromCommander,
|
buildVideoAttributesFromCommander,
|
||||||
getAccessTokenOrDie,
|
|
||||||
getLogger,
|
getLogger,
|
||||||
getServerCredentials
|
getServerCredentials
|
||||||
} from './cli'
|
} from './cli'
|
||||||
|
@ -232,8 +232,8 @@ async function uploadVideoOnPeerTube (parameters: {
|
||||||
tags
|
tags
|
||||||
}
|
}
|
||||||
|
|
||||||
let accessToken = await getAccessTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, accessToken)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes)
|
const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes)
|
||||||
|
|
||||||
|
@ -247,14 +247,14 @@ async function uploadVideoOnPeerTube (parameters: {
|
||||||
log.info('\nUploading on PeerTube video "%s".', videoAttributes.name)
|
log.info('\nUploading on PeerTube video "%s".', videoAttributes.name)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await uploadVideo(url, accessToken, videoAttributes)
|
await uploadVideo(url, server.accessToken, videoAttributes)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message.indexOf('401') !== -1) {
|
if (err.message.indexOf('401') !== -1) {
|
||||||
log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.')
|
log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.')
|
||||||
|
|
||||||
accessToken = await getAccessTokenOrDie(url, username, password)
|
server.accessToken = await server.loginCommand.getAccessToken(username, password)
|
||||||
|
|
||||||
await uploadVideo(url, accessToken, videoAttributes)
|
await uploadVideo(url, server.accessToken, videoAttributes)
|
||||||
} else {
|
} else {
|
||||||
exitError(err.message)
|
exitError(err.message)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
|
||||||
registerTSPaths()
|
registerTSPaths()
|
||||||
|
|
||||||
import { program, Command, OptionValues } from 'commander'
|
import { program, Command, OptionValues } from 'commander'
|
||||||
import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli'
|
import { assignToken, buildServer, getServerCredentials } from './cli'
|
||||||
import { PluginType } from '../../shared/models'
|
import { PluginType } from '../../shared/models'
|
||||||
import { isAbsolute } from 'path'
|
import { isAbsolute } from 'path'
|
||||||
import * as CliTable3 from 'cli-table3'
|
import * as CliTable3 from 'cli-table3'
|
||||||
|
@ -62,8 +62,8 @@ program.parse(process.argv)
|
||||||
|
|
||||||
async function pluginsListCLI (command: Command, options: OptionValues) {
|
async function pluginsListCLI (command: Command, options: OptionValues) {
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
let pluginType: PluginType
|
let pluginType: PluginType
|
||||||
if (options.onlyThemes) pluginType = PluginType.THEME
|
if (options.onlyThemes) pluginType = PluginType.THEME
|
||||||
|
@ -105,8 +105,8 @@ async function installPluginCLI (command: Command, options: OptionValues) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await server.pluginsCommand.install({ npmName: options.npmName, path: options.path })
|
await server.pluginsCommand.install({ npmName: options.npmName, path: options.path })
|
||||||
|
@ -132,8 +132,8 @@ async function updatePluginCLI (command: Command, options: OptionValues) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await server.pluginsCommand.update({ npmName: options.npmName, path: options.path })
|
await server.pluginsCommand.update({ npmName: options.npmName, path: options.path })
|
||||||
|
@ -154,8 +154,8 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await server.pluginsCommand.uninstall({ npmName: options.npmName })
|
await server.pluginsCommand.uninstall({ npmName: options.npmName })
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { URL } from 'url'
|
||||||
import validator from 'validator'
|
import validator from 'validator'
|
||||||
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
|
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
|
||||||
import { VideoRedundanciesTarget } from '@shared/models'
|
import { VideoRedundanciesTarget } from '@shared/models'
|
||||||
import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli'
|
import { assignToken, buildServer, getServerCredentials } from './cli'
|
||||||
|
|
||||||
import bytes = require('bytes')
|
import bytes = require('bytes')
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ program.parse(process.argv)
|
||||||
|
|
||||||
async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
|
async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
|
||||||
const { url, username, password } = await getServerCredentials(program)
|
const { url, username, password } = await getServerCredentials(program)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target })
|
const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target })
|
||||||
|
|
||||||
|
@ -104,8 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
|
||||||
|
|
||||||
async function addRedundancyCLI (options: { video: number }, command: Command) {
|
async function addRedundancyCLI (options: { video: number }, command: Command) {
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
if (!options.video || validator.isInt('' + options.video) === false) {
|
if (!options.video || validator.isInt('' + options.video) === false) {
|
||||||
console.error('You need to specify the video id to duplicate and it should be a number.\n')
|
console.error('You need to specify the video id to duplicate and it should be a number.\n')
|
||||||
|
@ -134,8 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
|
||||||
|
|
||||||
async function removeRedundancyCLI (options: { video: number }, command: Command) {
|
async function removeRedundancyCLI (options: { video: number }, command: Command) {
|
||||||
const { url, username, password } = await getServerCredentials(command)
|
const { url, username, password } = await getServerCredentials(command)
|
||||||
const token = await getAdminTokenOrDie(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
if (!options.video || validator.isInt('' + options.video) === false) {
|
if (!options.video || validator.isInt('' + options.video) === false) {
|
||||||
console.error('You need to specify the video id to remove from your redundancies.\n')
|
console.error('You need to specify the video id to remove from your redundancies.\n')
|
||||||
|
|
|
@ -4,9 +4,8 @@ registerTSPaths()
|
||||||
import { program } from 'commander'
|
import { program } from 'commander'
|
||||||
import { access, constants } from 'fs-extra'
|
import { access, constants } from 'fs-extra'
|
||||||
import { isAbsolute } from 'path'
|
import { isAbsolute } from 'path'
|
||||||
import { getAccessToken } from '../../shared/extra-utils'
|
|
||||||
import { uploadVideo } from '../../shared/extra-utils/'
|
import { uploadVideo } from '../../shared/extra-utils/'
|
||||||
import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
|
import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
|
||||||
|
|
||||||
let command = program
|
let command = program
|
||||||
.name('upload')
|
.name('upload')
|
||||||
|
@ -46,8 +45,8 @@ getServerCredentials(command)
|
||||||
.catch(err => console.error(err))
|
.catch(err => console.error(err))
|
||||||
|
|
||||||
async function run (url: string, username: string, password: string) {
|
async function run (url: string, username: string, password: string) {
|
||||||
const token = await getAccessToken(url, username, password)
|
const server = buildServer(url)
|
||||||
const server = buildServer(url, token)
|
await assignToken(server, username, password)
|
||||||
|
|
||||||
await access(options.file, constants.F_OK)
|
await access(options.file, constants.F_OK)
|
||||||
|
|
||||||
|
@ -62,7 +61,7 @@ async function run (url: string, username: string, password: string) {
|
||||||
})
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await uploadVideo(url, token, videoAttributes)
|
await uploadVideo(url, server.accessToken, videoAttributes)
|
||||||
console.log(`Video ${options.videoName} uploaded.`)
|
console.log(`Video ${options.videoName} uploaded.`)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { PeerTubeRequestError } from '@server/helpers/requests'
|
|
||||||
import { HttpStatusCode } from '@shared/core-utils'
|
import { HttpStatusCode } from '@shared/core-utils'
|
||||||
import { PeerTubeProblemDocument } from '@shared/models'
|
import { PeerTubeProblemDocument } from '@shared/models'
|
||||||
import { unwrapBody } from '../requests'
|
import { unwrapBody } from '../requests'
|
||||||
|
@ -34,8 +33,8 @@ export class LoginCommand extends AbstractCommand {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccessToken (user?: { username: string, password: string }): Promise<string>
|
getAccessToken (arg1?: { username: string, password: string }): Promise<string>
|
||||||
getAccessToken (username: string, password: string): Promise<string>
|
getAccessToken (arg1: string, password: string): Promise<string>
|
||||||
async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) {
|
async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) {
|
||||||
let user: { username: string, password: string }
|
let user: { username: string, password: string }
|
||||||
|
|
||||||
|
@ -48,7 +47,7 @@ export class LoginCommand extends AbstractCommand {
|
||||||
|
|
||||||
return body.access_token
|
return body.access_token
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error('Cannot authenticate. Please check your username/password.')
|
throw new Error(`Cannot authenticate. Please check your username/password. (${err})`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue