]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/peertube-redundancy.ts
Add ability to cancel & delete video imports
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-redundancy.ts
index 76d633f9e1757397afccd137fa70032338ac3efc..2c62a3c195db7524d54a4f0f17d2cd765f3bf3f8 100644 (file)
@@ -1,19 +1,15 @@
-import { registerTSPaths } from '../helpers/register-ts-paths'
-registerTSPaths()
-
-import * as CliTable3 from 'cli-table3'
+import CliTable3 from 'cli-table3'
 import { Command, program } from 'commander'
 import { uniq } from 'lodash'
 import { URL } from 'url'
 import validator from 'validator'
-import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
-import { VideoRedundanciesTarget } from '@shared/models'
-import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli'
+import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models'
+import { assignToken, buildServer, getServerCredentials } from './cli'
 
 import bytes = require('bytes')
 
 program
-  .name('plugins')
+  .name('redundancy')
   .usage('[command] [options]')
 
 program
@@ -60,10 +56,10 @@ program.parse(process.argv)
 
 async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
   const { url, username, password } = await getServerCredentials(program)
-  const token = await getAdminTokenOrDie(url, username, password)
-  const server = buildServer(url, token)
+  const server = buildServer(url)
+  await assignToken(server, username, password)
 
-  const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target })
+  const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })
 
   const table = new CliTable3({
     head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
@@ -104,8 +100,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
 
 async function addRedundancyCLI (options: { video: number }, command: Command) {
   const { url, username, password } = await getServerCredentials(command)
-  const token = await getAdminTokenOrDie(url, username, password)
-  const server = buildServer(url, token)
+  const server = buildServer(url)
+  await assignToken(server, username, password)
 
   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')
@@ -114,7 +110,7 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
   }
 
   try {
-    await server.redundancyCommand.addVideo({ videoId: options.video })
+    await server.redundancy.addVideo({ videoId: options.video })
 
     console.log('Video will be duplicated by your instance!')
 
@@ -134,8 +130,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
 
 async function removeRedundancyCLI (options: { video: number }, command: Command) {
   const { url, username, password } = await getServerCredentials(command)
-  const token = await getAdminTokenOrDie(url, username, password)
-  const server = buildServer(url, token)
+  const server = buildServer(url)
+  await assignToken(server, username, password)
 
   if (!options.video || validator.isInt('' + options.video) === false) {
     console.error('You need to specify the video id to remove from your redundancies.\n')
@@ -145,11 +141,11 @@ async function removeRedundancyCLI (options: { video: number }, command: Command
 
   const videoId = parseInt(options.video + '', 10)
 
-  const myVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'my-videos' })
+  const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
   let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
 
   if (!videoRedundancy) {
-    const remoteVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'remote-videos' })
+    const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
     videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
   }
 
@@ -164,7 +160,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command
                                .map(r => r.id)
 
     for (const id of ids) {
-      await server.redundancyCommand.removeVideo({ redundancyId: id })
+      await server.redundancy.removeVideo({ redundancyId: id })
     }
 
     console.log('Video redundancy removed!')