From 078f17e6d90376050f43ce639e88e11869b49ee7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 15:03:44 +0200 Subject: Fix CLI tools --- server/tools/peertube-redundancy.ts | 61 ++++++++++++------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) (limited to 'server/tools/peertube-redundancy.ts') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 4810deee0..76d633f9e 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -1,17 +1,14 @@ -// eslint-disable @typescript-eslint/no-unnecessary-type-assertion - import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() -import { program, Command } from 'commander' -import { getAdminTokenOrDie, getServerCredentials } from './cli' -import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' -import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy' -import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' -import validator from 'validator' import * as CliTable3 from 'cli-table3' -import { URL } from 'url' +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 bytes = require('bytes') @@ -63,15 +60,16 @@ program.parse(process.argv) async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const { url, username, password } = await getServerCredentials(program) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) - const redundancies = await listVideoRedundanciesData(url, accessToken, target) + const { data } = await server.redundancyCommand.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' ] }) as any - for (const redundancy of redundancies) { + for (const redundancy of data) { const webtorrentFiles = redundancy.redundancies.files const streamingPlaylists = redundancy.redundancies.streamingPlaylists @@ -106,7 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { async function addRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) 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') @@ -115,11 +114,7 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { } try { - await addVideoRedundancy({ - url, - accessToken, - videoId: options.video - }) + await server.redundancyCommand.addVideo({ videoId: options.video }) console.log('Video will be duplicated by your instance!') @@ -139,7 +134,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 accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to remove from your redundancies.\n') @@ -149,12 +145,12 @@ async function removeRedundancyCLI (options: { video: number }, command: Command const videoId = parseInt(options.video + '', 10) - let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') - let videoRedundancy = redundancies.find(r => videoId === r.id) + const myVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'my-videos' }) + let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) if (!videoRedundancy) { - redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos') - videoRedundancy = redundancies.find(r => videoId === r.id) + const remoteVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'remote-videos' }) + videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) } if (!videoRedundancy) { @@ -168,11 +164,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command .map(r => r.id) for (const id of ids) { - await removeVideoRedundancy({ - url, - accessToken, - redundancyId: id - }) + await server.redundancyCommand.removeVideo({ redundancyId: id }) } console.log('Video redundancy removed!') @@ -183,16 +175,3 @@ async function removeRedundancyCLI (options: { video: number }, command: Command process.exit(-1) } } - -async function listVideoRedundanciesData (url: string, accessToken: string, target: VideoRedundanciesTarget) { - const res = await listVideoRedundancies({ - url, - accessToken, - start: 0, - count: 100, - sort: 'name', - target - }) - - return res.body.data as VideoRedundancy[] -} -- cgit v1.2.3 From d0a0fa429d4651710ed951a3c11af0219e408964 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 11:44:16 +0200 Subject: Adapt CLI to new commands --- server/tools/peertube-redundancy.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'server/tools/peertube-redundancy.ts') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 76d633f9e..0f6b3086c 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -8,7 +8,7 @@ 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 { assignToken, buildServer, getServerCredentials } from './cli' import bytes = require('bytes') @@ -60,8 +60,8 @@ 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 }) @@ -104,8 +104,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') @@ -134,8 +134,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') -- cgit v1.2.3 From 89d241a79c262b9775c233b73cff080043ebb5e6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 09:04:35 +0200 Subject: Shorter server command names --- server/tools/peertube-redundancy.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'server/tools/peertube-redundancy.ts') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 0f6b3086c..5fda68c8e 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -63,7 +63,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { 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' ] @@ -114,7 +114,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!') @@ -145,11 +145,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 +164,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!') -- cgit v1.2.3 From c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:42:24 +0200 Subject: Refactor requests --- server/tools/peertube-redundancy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/tools/peertube-redundancy.ts') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 5fda68c8e..bcaae63a3 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -6,7 +6,7 @@ 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 { HttpStatusCode } from '@shared/models' import { VideoRedundanciesTarget } from '@shared/models' import { assignToken, buildServer, getServerCredentials } from './cli' -- cgit v1.2.3 From 4c7e60bc17ee5830399bac4aa273356903421b4c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 14:27:30 +0200 Subject: Reorganize imports --- server/tools/peertube-redundancy.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'server/tools/peertube-redundancy.ts') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index bcaae63a3..73b026ac8 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -6,8 +6,7 @@ import { Command, program } from 'commander' import { uniq } from 'lodash' import { URL } from 'url' import validator from 'validator' -import { HttpStatusCode } from '@shared/models' -import { VideoRedundanciesTarget } from '@shared/models' +import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models' import { assignToken, buildServer, getServerCredentials } from './cli' import bytes = require('bytes') -- cgit v1.2.3