はじまりの大地
このコミットが含まれているのは:
@@ -0,0 +1,171 @@
|
||||
import CliTable3 from 'cli-table3'
|
||||
import prompt from 'prompt'
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './shared/index.js'
|
||||
|
||||
export function defineAuthProgram () {
|
||||
const program = new Command()
|
||||
.name('auth')
|
||||
.description('Register your accounts on remote instances to use them with other commands')
|
||||
|
||||
program
|
||||
.command('add')
|
||||
.description('remember your accounts on remote instances for easier use')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('--default', 'add the entry as the new default')
|
||||
.action(options => {
|
||||
/* eslint-disable no-import-assign */
|
||||
prompt.override = options
|
||||
prompt.start()
|
||||
prompt.get({
|
||||
properties: {
|
||||
url: {
|
||||
description: 'instance url',
|
||||
conform: value => isURLaPeerTubeInstance(value),
|
||||
message: 'It should be an URL (https://peertube.example.com)',
|
||||
required: true
|
||||
},
|
||||
username: {
|
||||
conform: value => typeof value === 'string' && value.length !== 0,
|
||||
message: 'Name must be only letters, spaces, or dashes',
|
||||
required: true
|
||||
},
|
||||
password: {
|
||||
hidden: true,
|
||||
replace: '*',
|
||||
required: true
|
||||
}
|
||||
}
|
||||
}, async (_, result) => {
|
||||
|
||||
// Check credentials
|
||||
try {
|
||||
// Strip out everything after the domain:port.
|
||||
// See https://github.com/Chocobozzz/PeerTube/issues/3520
|
||||
result.url = stripExtraneousFromPeerTubeUrl(result.url)
|
||||
|
||||
const server = buildServer(result.url)
|
||||
await assignToken(server, result.username, result.password)
|
||||
} catch (err) {
|
||||
console.error(err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
await setInstance(result.url, result.username, result.password, options.default)
|
||||
|
||||
process.exit(0)
|
||||
})
|
||||
})
|
||||
|
||||
program
|
||||
.command('del <url>')
|
||||
.description('Unregisters a remote instance')
|
||||
.action(async url => {
|
||||
await delInstance(url)
|
||||
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
program
|
||||
.command('list')
|
||||
.description('List registered remote instances')
|
||||
.action(async () => {
|
||||
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
|
||||
|
||||
const table = new CliTable3({
|
||||
head: [ 'instance', 'login' ],
|
||||
colWidths: [ 30, 30 ]
|
||||
}) as any
|
||||
|
||||
settings.remotes.forEach(element => {
|
||||
if (!netrc.machines[element]) return
|
||||
|
||||
table.push([
|
||||
element,
|
||||
netrc.machines[element].login
|
||||
])
|
||||
})
|
||||
|
||||
console.log(table.toString())
|
||||
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
program
|
||||
.command('set-default <url>')
|
||||
.description('Set an existing entry as default')
|
||||
.action(async url => {
|
||||
const settings = await getSettings()
|
||||
const instanceExists = settings.remotes.includes(url)
|
||||
|
||||
if (instanceExists) {
|
||||
settings.default = settings.remotes.indexOf(url)
|
||||
await writeSettings(settings)
|
||||
|
||||
process.exit(0)
|
||||
} else {
|
||||
console.log('<url> is not a registered instance.')
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program.addHelpText('after', '\n\n Examples:\n\n' +
|
||||
' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
|
||||
' $ peertube auth add -u https://peertube.cpy.re -U root\n' +
|
||||
' $ peertube auth list\n' +
|
||||
' $ peertube auth del https://peertube.cpy.re\n'
|
||||
)
|
||||
|
||||
return program
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Private
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function delInstance (url: string) {
|
||||
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
|
||||
|
||||
const index = settings.remotes.indexOf(url)
|
||||
settings.remotes.splice(index)
|
||||
|
||||
if (settings.default === index) settings.default = -1
|
||||
|
||||
await writeSettings(settings)
|
||||
|
||||
delete netrc.machines[url]
|
||||
|
||||
await netrc.save()
|
||||
}
|
||||
|
||||
async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
|
||||
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
|
||||
|
||||
if (settings.remotes.includes(url) === false) {
|
||||
settings.remotes.push(url)
|
||||
}
|
||||
|
||||
if (isDefault || settings.remotes.length === 1) {
|
||||
settings.default = settings.remotes.length - 1
|
||||
}
|
||||
|
||||
await writeSettings(settings)
|
||||
|
||||
netrc.machines[url] = { login: username, password }
|
||||
await netrc.save()
|
||||
}
|
||||
|
||||
function isURLaPeerTubeInstance (url: string) {
|
||||
return url.startsWith('http://') || url.startsWith('https://')
|
||||
}
|
||||
|
||||
function stripExtraneousFromPeerTubeUrl (url: string) {
|
||||
// Get everything before the 3rd /.
|
||||
const urlLength = url.includes('/', 8)
|
||||
? url.indexOf('/', 8)
|
||||
: url.length
|
||||
|
||||
return url.substring(0, urlLength)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { assignToken, buildServer } from './shared/index.js'
|
||||
|
||||
export function defineGetAccessProgram () {
|
||||
const program = new Command()
|
||||
.name('get-access-token')
|
||||
.description('Get a peertube access token')
|
||||
.alias('token')
|
||||
|
||||
program
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-n, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.action(async options => {
|
||||
try {
|
||||
if (
|
||||
!options.url ||
|
||||
!options.username ||
|
||||
!options.password
|
||||
) {
|
||||
if (!options.url) console.error('--url field is required.')
|
||||
if (!options.username) console.error('--username field is required.')
|
||||
if (!options.password) console.error('--password field is required.')
|
||||
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
const server = buildServer(options.url)
|
||||
await assignToken(server, options.username, options.password)
|
||||
|
||||
console.log(server.accessToken)
|
||||
} catch (err) {
|
||||
console.error('Cannot get access token: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
return program
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import CliTable3 from 'cli-table3'
|
||||
import { isAbsolute } from 'path'
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { PluginType, PluginType_Type } from '@peertube/peertube-models'
|
||||
import { assignToken, buildServer, CommonProgramOptions, getServerCredentials } from './shared/index.js'
|
||||
|
||||
export function definePluginsProgram () {
|
||||
const program = new Command()
|
||||
|
||||
program
|
||||
.name('plugins')
|
||||
.description('Manage instance plugins/themes')
|
||||
.alias('p')
|
||||
|
||||
program
|
||||
.command('list')
|
||||
.description('List installed plugins')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('-t, --only-themes', 'List themes only')
|
||||
.option('-P, --only-plugins', 'List plugins only')
|
||||
.action(async options => {
|
||||
try {
|
||||
await pluginsListCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot list plugins: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('install')
|
||||
.description('Install a plugin or a theme')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('-P --path <path>', 'Install from a path')
|
||||
.option('-n, --npm-name <npmName>', 'Install from npm')
|
||||
.option('--plugin-version <pluginVersion>', 'Specify the plugin version to install (only available when installing from npm)')
|
||||
.action(async options => {
|
||||
try {
|
||||
await installPluginCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot install plugin: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('update')
|
||||
.description('Update a plugin or a theme')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('-P --path <path>', 'Update from a path')
|
||||
.option('-n, --npm-name <npmName>', 'Update from npm')
|
||||
.action(async options => {
|
||||
try {
|
||||
await updatePluginCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot update plugin: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('uninstall')
|
||||
.description('Uninstall a plugin or a theme')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
|
||||
.action(async options => {
|
||||
try {
|
||||
await uninstallPluginCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot uninstall plugin: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
return program
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
async function pluginsListCLI (options: CommonProgramOptions & { onlyThemes?: true, onlyPlugins?: true }) {
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
let pluginType: PluginType_Type
|
||||
if (options.onlyThemes) pluginType = PluginType.THEME
|
||||
if (options.onlyPlugins) pluginType = PluginType.PLUGIN
|
||||
|
||||
const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType })
|
||||
|
||||
const table = new CliTable3({
|
||||
head: [ 'name', 'version', 'homepage' ],
|
||||
colWidths: [ 50, 20, 50 ]
|
||||
}) as any
|
||||
|
||||
for (const plugin of data) {
|
||||
const npmName = plugin.type === PluginType.PLUGIN
|
||||
? 'peertube-plugin-' + plugin.name
|
||||
: 'peertube-theme-' + plugin.name
|
||||
|
||||
table.push([
|
||||
npmName,
|
||||
plugin.version,
|
||||
plugin.homepage
|
||||
])
|
||||
}
|
||||
|
||||
console.log(table.toString())
|
||||
}
|
||||
|
||||
async function installPluginCLI (options: CommonProgramOptions & { path?: string, npmName?: string, pluginVersion?: string }) {
|
||||
if (!options.path && !options.npmName) {
|
||||
throw new Error('You need to specify the npm name or the path of the plugin you want to install.')
|
||||
}
|
||||
|
||||
if (options.path && !isAbsolute(options.path)) {
|
||||
throw new Error('Path should be absolute.')
|
||||
}
|
||||
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
await server.plugins.install({ npmName: options.npmName, path: options.path, pluginVersion: options.pluginVersion })
|
||||
|
||||
console.log('Plugin installed.')
|
||||
}
|
||||
|
||||
async function updatePluginCLI (options: CommonProgramOptions & { path?: string, npmName?: string }) {
|
||||
if (!options.path && !options.npmName) {
|
||||
throw new Error('You need to specify the npm name or the path of the plugin you want to update.')
|
||||
}
|
||||
|
||||
if (options.path && !isAbsolute(options.path)) {
|
||||
throw new Error('Path should be absolute.')
|
||||
}
|
||||
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
await server.plugins.update({ npmName: options.npmName, path: options.path })
|
||||
|
||||
console.log('Plugin updated.')
|
||||
}
|
||||
|
||||
async function uninstallPluginCLI (options: CommonProgramOptions & { npmName?: string }) {
|
||||
if (!options.npmName) {
|
||||
throw new Error('You need to specify the npm name of the plugin/theme you want to uninstall.')
|
||||
}
|
||||
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
await server.plugins.uninstall({ npmName: options.npmName })
|
||||
|
||||
console.log('Plugin uninstalled.')
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
import bytes from 'bytes'
|
||||
import CliTable3 from 'cli-table3'
|
||||
import { URL } from 'url'
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { forceNumber, uniqify } from '@peertube/peertube-core-utils'
|
||||
import { HttpStatusCode, VideoRedundanciesTarget } from '@peertube/peertube-models'
|
||||
import { assignToken, buildServer, CommonProgramOptions, getServerCredentials } from './shared/index.js'
|
||||
|
||||
export function defineRedundancyProgram () {
|
||||
const program = new Command()
|
||||
.name('redundancy')
|
||||
.description('Manage instance redundancies')
|
||||
.alias('r')
|
||||
|
||||
program
|
||||
.command('list-remote-redundancies')
|
||||
.description('List remote redundancies on your videos')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.action(async options => {
|
||||
try {
|
||||
await listRedundanciesCLI({ target: 'my-videos', ...options })
|
||||
} catch (err) {
|
||||
console.error('Cannot list remote redundancies: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('list-my-redundancies')
|
||||
.description('List your redundancies of remote videos')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.action(async options => {
|
||||
try {
|
||||
await listRedundanciesCLI({ target: 'remote-videos', ...options })
|
||||
} catch (err) {
|
||||
console.error('Cannot list redundancies: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('add')
|
||||
.description('Duplicate a video in your redundancy system')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.requiredOption('-v, --video <videoId>', 'Video id to duplicate', parseInt)
|
||||
.action(async options => {
|
||||
try {
|
||||
await addRedundancyCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot duplicate video: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
program
|
||||
.command('remove')
|
||||
.description('Remove a video from your redundancies')
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.requiredOption('-v, --video <videoId>', 'Video id to remove from redundancies', parseInt)
|
||||
.action(async options => {
|
||||
try {
|
||||
await removeRedundancyCLI(options)
|
||||
} catch (err) {
|
||||
console.error('Cannot remove redundancy: ' + err)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
return program
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
async function listRedundanciesCLI (options: CommonProgramOptions & { target: VideoRedundanciesTarget }) {
|
||||
const { target } = options
|
||||
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
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' ]
|
||||
}) as any
|
||||
|
||||
for (const redundancy of data) {
|
||||
const webVideoFiles = redundancy.redundancies.files
|
||||
const streamingPlaylists = redundancy.redundancies.streamingPlaylists
|
||||
|
||||
let totalSize = ''
|
||||
if (target === 'remote-videos') {
|
||||
const tmp = webVideoFiles.concat(streamingPlaylists)
|
||||
.reduce((a, b) => a + b.size, 0)
|
||||
|
||||
// FIXME: don't use external dependency to stringify bytes: we already have the functions in the client
|
||||
totalSize = bytes(tmp)
|
||||
}
|
||||
|
||||
const instances = uniqify(
|
||||
webVideoFiles.concat(streamingPlaylists)
|
||||
.map(r => r.fileUrl)
|
||||
.map(u => new URL(u).host)
|
||||
)
|
||||
|
||||
table.push([
|
||||
redundancy.id.toString(),
|
||||
redundancy.name,
|
||||
redundancy.url,
|
||||
webVideoFiles.length,
|
||||
streamingPlaylists.length,
|
||||
instances.join('\n'),
|
||||
totalSize
|
||||
])
|
||||
}
|
||||
|
||||
console.log(table.toString())
|
||||
}
|
||||
|
||||
async function addRedundancyCLI (options: { video: number } & CommonProgramOptions) {
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
if (!options.video || isNaN(options.video)) {
|
||||
throw new Error('You need to specify the video id to duplicate and it should be a number.')
|
||||
}
|
||||
|
||||
try {
|
||||
await server.redundancy.addVideo({ videoId: options.video })
|
||||
|
||||
console.log('Video will be duplicated by your instance!')
|
||||
} catch (err) {
|
||||
if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
|
||||
throw new Error('This video is already duplicated by your instance.')
|
||||
}
|
||||
|
||||
if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
|
||||
throw new Error('This video id does not exist.')
|
||||
}
|
||||
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function removeRedundancyCLI (options: CommonProgramOptions & { video: number }) {
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
if (!options.video || isNaN(options.video)) {
|
||||
throw new Error('You need to specify the video id to remove from your redundancies')
|
||||
}
|
||||
|
||||
const videoId = forceNumber(options.video)
|
||||
|
||||
const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
|
||||
let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
|
||||
|
||||
if (!videoRedundancy) {
|
||||
const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
|
||||
videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
|
||||
}
|
||||
|
||||
if (!videoRedundancy) {
|
||||
throw new Error('Video redundancy not found.')
|
||||
}
|
||||
|
||||
const ids = videoRedundancy.redundancies.files
|
||||
.concat(videoRedundancy.redundancies.streamingPlaylists)
|
||||
.map(r => r.id)
|
||||
|
||||
for (const id of ids) {
|
||||
await server.redundancy.removeVideo({ redundancyId: id })
|
||||
}
|
||||
|
||||
console.log('Video redundancy removed!')
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { VideoCommentPolicy, VideoPrivacy, VideoPrivacyType } from '@peertube/peertube-models'
|
||||
import { PeerTubeServer } from '@peertube/peertube-server-commands'
|
||||
import { access, constants } from 'fs/promises'
|
||||
import { isAbsolute } from 'path'
|
||||
import { inspect } from 'util'
|
||||
import { assignToken, buildServer, getServerCredentials, listOptions } from './shared/index.js'
|
||||
|
||||
type UploadOptions = {
|
||||
url?: string
|
||||
username?: string
|
||||
password?: string
|
||||
thumbnail?: string
|
||||
preview?: string
|
||||
file?: string
|
||||
videoName?: string
|
||||
category?: number
|
||||
licence?: number
|
||||
language?: string
|
||||
tags?: string[]
|
||||
nsfw?: true
|
||||
videoDescription?: string
|
||||
privacy?: VideoPrivacyType
|
||||
channelName?: string
|
||||
noCommentsEnabled?: true
|
||||
support?: string
|
||||
noWaitTranscoding?: true
|
||||
noDownloadEnabled?: true
|
||||
}
|
||||
|
||||
export function defineUploadProgram () {
|
||||
const program = new Command('upload')
|
||||
.description('Upload a video on a PeerTube instance')
|
||||
.alias('up')
|
||||
|
||||
program
|
||||
.option('-u, --url <url>', 'Server url')
|
||||
.option('-U, --username <username>', 'Username')
|
||||
.option('-p, --password <token>', 'Password')
|
||||
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
|
||||
.option('--preview <previewPath>', 'Preview path')
|
||||
.option('-f, --file <file>', 'Video absolute file path')
|
||||
.option('-n, --video-name <name>', 'Video name')
|
||||
.option('-c, --category <category_number>', 'Category number', parseInt)
|
||||
.option('-l, --licence <licence_number>', 'Licence number', parseInt)
|
||||
.option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
|
||||
.option('-t, --tags <tags>', 'Video tags', listOptions)
|
||||
.option('-N, --nsfw', 'Video is Not Safe For Work')
|
||||
.option('-d, --video-description <description>', 'Video description')
|
||||
.option('-P, --privacy <privacy_number>', 'Privacy', v => parseInt(v) as VideoPrivacyType)
|
||||
.option('-C, --channel-name <channel_name>', 'Channel name')
|
||||
.option('--no-comments-enabled', 'Disable video comments')
|
||||
.option('-s, --support <support>', 'Video support text')
|
||||
.option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
|
||||
.option('--no-download-enabled', 'Disable video download')
|
||||
.option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
|
||||
.action(async options => {
|
||||
try {
|
||||
const { url, username, password } = await getServerCredentials(options)
|
||||
|
||||
if (!options.videoName || !options.file) {
|
||||
if (!options.videoName) console.error('--video-name is required.')
|
||||
if (!options.file) console.error('--file is required.')
|
||||
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
if (isAbsolute(options.file) === false) {
|
||||
console.error('File path should be absolute.')
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
await run({ ...options, url, username, password })
|
||||
} catch (err) {
|
||||
console.error('Cannot upload video: ' + err.message)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
return program
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Private
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function run (options: UploadOptions) {
|
||||
const { url, username, password } = options
|
||||
|
||||
const server = buildServer(url)
|
||||
await assignToken(server, username, password)
|
||||
|
||||
await access(options.file, constants.F_OK)
|
||||
|
||||
console.log('Uploading %s video...', options.videoName)
|
||||
|
||||
const baseAttributes = await buildVideoAttributesFromCommander(server, options)
|
||||
|
||||
const attributes = {
|
||||
...baseAttributes,
|
||||
|
||||
fixture: options.file,
|
||||
thumbnailfile: options.thumbnail,
|
||||
previewfile: options.preview
|
||||
}
|
||||
|
||||
try {
|
||||
await server.videos.upload({ attributes })
|
||||
console.log(`Video ${options.videoName} uploaded.`)
|
||||
process.exit(0)
|
||||
} catch (err) {
|
||||
const message = err.message || ''
|
||||
if (message.includes('413')) {
|
||||
console.error('Aborted: user quota is exceeded or video file is too big for this PeerTube instance.')
|
||||
} else {
|
||||
console.error(inspect(err))
|
||||
}
|
||||
|
||||
process.exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
async function buildVideoAttributesFromCommander (server: PeerTubeServer, options: UploadOptions) {
|
||||
const defaultBooleanAttributes = {
|
||||
nsfw: false,
|
||||
downloadEnabled: true,
|
||||
waitTranscoding: true
|
||||
}
|
||||
|
||||
const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
|
||||
|
||||
for (const key of Object.keys(defaultBooleanAttributes)) {
|
||||
if (options[key] !== undefined) {
|
||||
booleanAttributes[key] = options[key]
|
||||
} else {
|
||||
booleanAttributes[key] = defaultBooleanAttributes[key]
|
||||
}
|
||||
}
|
||||
|
||||
const videoAttributes = {
|
||||
name: options.videoName,
|
||||
category: options.category || undefined,
|
||||
licence: options.licence || undefined,
|
||||
language: options.language || undefined,
|
||||
privacy: options.privacy || VideoPrivacy.PUBLIC,
|
||||
support: options.support || undefined,
|
||||
description: options.videoDescription || undefined,
|
||||
tags: options.tags || undefined,
|
||||
|
||||
commentsPolicy: options.noCommentsEnabled !== undefined
|
||||
? options.noCommentsEnabled === true
|
||||
? VideoCommentPolicy.DISABLED
|
||||
: VideoCommentPolicy.ENABLED
|
||||
: undefined,
|
||||
|
||||
...booleanAttributes
|
||||
}
|
||||
|
||||
if (options.channelName) {
|
||||
const videoChannel = await server.channels.get({ channelName: options.channelName })
|
||||
|
||||
Object.assign(videoAttributes, { channelId: videoChannel.id })
|
||||
|
||||
if (!videoAttributes.support && videoChannel.support) {
|
||||
Object.assign(videoAttributes, { support: videoChannel.support })
|
||||
}
|
||||
}
|
||||
|
||||
return videoAttributes
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { Command } from '@commander-js/extra-typings'
|
||||
import { defineAuthProgram } from './peertube-auth.js'
|
||||
import { defineGetAccessProgram } from './peertube-get-access-token.js'
|
||||
import { definePluginsProgram } from './peertube-plugins.js'
|
||||
import { defineRedundancyProgram } from './peertube-redundancy.js'
|
||||
import { defineUploadProgram } from './peertube-upload.js'
|
||||
import { getSettings, version } from './shared/index.js'
|
||||
|
||||
const program = new Command()
|
||||
|
||||
program
|
||||
.version(version, '-v, --version')
|
||||
.usage('[command] [options]')
|
||||
|
||||
program.addCommand(defineAuthProgram())
|
||||
program.addCommand(defineUploadProgram())
|
||||
program.addCommand(defineRedundancyProgram())
|
||||
program.addCommand(definePluginsProgram())
|
||||
program.addCommand(defineGetAccessProgram())
|
||||
|
||||
// help on no command
|
||||
if (!process.argv.slice(2).length) {
|
||||
const logo = '░P░e░e░r░T░u░b░e░'
|
||||
console.log(`
|
||||
___/),.._ ` + logo + `
|
||||
/' ,. ."'._
|
||||
( "' '-.__"-._ ,-
|
||||
\\'='='), "\\ -._-"-. -"/
|
||||
/ ""/"\\,_\\,__"" _" /,-
|
||||
/ / -" _/"/
|
||||
/ | ._\\\\ |\\ |_.".-" /
|
||||
/ | __\\)|)|),/|_." _,."
|
||||
/ \\_." " ") | ).-""---''--
|
||||
( "/.""7__-""''
|
||||
| " ."._--._
|
||||
\\ \\ (_ __ "" ".,_
|
||||
\\.,. \\ "" -"".-"
|
||||
".,_, (",_-,,,-".-
|
||||
"'-,\\_ __,-"
|
||||
",)" ")
|
||||
/"\\-"
|
||||
,"\\/
|
||||
_,.__/"\\/_ (the CLI for red chocobos)
|
||||
/ \\) "./, ".
|
||||
--/---"---" "-) )---- by Chocobozzz et al.\n`)
|
||||
}
|
||||
|
||||
getSettings()
|
||||
.then(settings => {
|
||||
const state = (settings.default === undefined || settings.default === -1)
|
||||
? 'no instance selected, commands will require explicit arguments'
|
||||
: 'instance ' + settings.remotes[settings.default] + ' selected'
|
||||
|
||||
program
|
||||
.addHelpText('after', '\n\n State: ' + state + '\n\n' +
|
||||
' Examples:\n\n' +
|
||||
' $ peertube auth add -u "PEERTUBE_URL" -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
|
||||
' $ peertube up <videoFile>\n'
|
||||
)
|
||||
.parse(process.argv)
|
||||
})
|
||||
.catch(err => console.error(err))
|
||||
@@ -0,0 +1,195 @@
|
||||
import applicationConfig from 'application-config'
|
||||
import { Netrc } from 'netrc-parser'
|
||||
import { join } from 'path'
|
||||
import { createLogger, format, transports } from 'winston'
|
||||
import { UserRole } from '@peertube/peertube-models'
|
||||
import { getAppNumber, isTestInstance, root } from '@peertube/peertube-node-utils'
|
||||
import { PeerTubeServer } from '@peertube/peertube-server-commands'
|
||||
|
||||
export type CommonProgramOptions = {
|
||||
url?: string
|
||||
username?: string
|
||||
password?: string
|
||||
}
|
||||
|
||||
let configName = 'PeerTube/CLI'
|
||||
if (isTestInstance()) configName += `-${getAppNumber()}`
|
||||
|
||||
const config = applicationConfig(configName)
|
||||
|
||||
const version: string = process.env.PACKAGE_VERSION
|
||||
|
||||
async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
|
||||
const token = await server.login.getAccessToken(username, password)
|
||||
const me = await server.users.getMyInfo({ token })
|
||||
|
||||
if (me.role.id !== UserRole.ADMINISTRATOR) {
|
||||
console.error('You must be an administrator.')
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
interface Settings {
|
||||
remotes: any[]
|
||||
default: number
|
||||
}
|
||||
|
||||
async function getSettings () {
|
||||
const defaultSettings: Settings = {
|
||||
remotes: [],
|
||||
default: -1
|
||||
}
|
||||
|
||||
const data = await config.read() as Promise<Settings>
|
||||
|
||||
return Object.keys(data).length === 0
|
||||
? defaultSettings
|
||||
: data
|
||||
}
|
||||
|
||||
async function getNetrc () {
|
||||
const netrc = isTestInstance()
|
||||
? new Netrc(join(root(import.meta.url), 'test' + getAppNumber(), 'netrc'))
|
||||
: new Netrc()
|
||||
|
||||
await netrc.load()
|
||||
|
||||
return netrc
|
||||
}
|
||||
|
||||
function writeSettings (settings: Settings) {
|
||||
return config.write(settings)
|
||||
}
|
||||
|
||||
function deleteSettings () {
|
||||
return config.trash()
|
||||
}
|
||||
|
||||
function getRemoteObjectOrDie (
|
||||
options: CommonProgramOptions,
|
||||
settings: Settings,
|
||||
netrc: Netrc
|
||||
): { url: string, username: string, password: string } {
|
||||
|
||||
function exitIfNoOptions (optionNames: string[], errorPrefix: string = '') {
|
||||
let exit = false
|
||||
|
||||
for (const key of optionNames) {
|
||||
if (!options[key]) {
|
||||
if (exit === false && errorPrefix) console.error(errorPrefix)
|
||||
|
||||
console.error(`--${key} field is required`)
|
||||
exit = true
|
||||
}
|
||||
}
|
||||
|
||||
if (exit) process.exit(-1)
|
||||
}
|
||||
|
||||
// If username or password are specified, both are mandatory
|
||||
if (options.username || options.password) {
|
||||
exitIfNoOptions([ 'username', 'password' ])
|
||||
}
|
||||
|
||||
// If no available machines, url, username and password args are mandatory
|
||||
if (Object.keys(netrc.machines).length === 0) {
|
||||
exitIfNoOptions([ 'url', 'username', 'password' ], 'No account found in netrc')
|
||||
}
|
||||
|
||||
if (settings.remotes.length === 0 || settings.default === -1) {
|
||||
exitIfNoOptions([ 'url' ], 'No default instance found')
|
||||
}
|
||||
|
||||
let url: string = options.url
|
||||
let username: string = options.username
|
||||
let password: string = options.password
|
||||
|
||||
if (!url && settings.default !== -1) url = settings.remotes[settings.default]
|
||||
|
||||
const machine = netrc.machines[url]
|
||||
if ((!username || !password) && !machine) {
|
||||
console.error('Cannot find existing configuration for %s.', url)
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
if (!username && machine) username = machine.login
|
||||
if (!password && machine) password = machine.password
|
||||
|
||||
return { url, username, password }
|
||||
}
|
||||
|
||||
function listOptions (val: string) {
|
||||
return val.split(',')
|
||||
}
|
||||
|
||||
function getServerCredentials (options: CommonProgramOptions) {
|
||||
return Promise.all([ getSettings(), getNetrc() ])
|
||||
.then(([ settings, netrc ]) => {
|
||||
return getRemoteObjectOrDie(options, settings, netrc)
|
||||
})
|
||||
}
|
||||
|
||||
function buildServer (url: string) {
|
||||
return new PeerTubeServer({ url })
|
||||
}
|
||||
|
||||
async function assignToken (server: PeerTubeServer, username: string, password: string) {
|
||||
const bodyClient = await server.login.getClient()
|
||||
const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
|
||||
|
||||
const body = await server.login.login({ client, user: { username, password } })
|
||||
|
||||
server.accessToken = body.access_token
|
||||
}
|
||||
|
||||
function getLogger (logLevel = 'info') {
|
||||
const logLevels = {
|
||||
0: 0,
|
||||
error: 0,
|
||||
1: 1,
|
||||
warn: 1,
|
||||
2: 2,
|
||||
info: 2,
|
||||
3: 3,
|
||||
verbose: 3,
|
||||
4: 4,
|
||||
debug: 4
|
||||
}
|
||||
|
||||
const logger = createLogger({
|
||||
levels: logLevels,
|
||||
format: format.combine(
|
||||
format.splat(),
|
||||
format.simple()
|
||||
),
|
||||
transports: [
|
||||
new (transports.Console)({
|
||||
level: logLevel
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
version,
|
||||
getLogger,
|
||||
getSettings,
|
||||
getNetrc,
|
||||
getRemoteObjectOrDie,
|
||||
writeSettings,
|
||||
deleteSettings,
|
||||
|
||||
getServerCredentials,
|
||||
|
||||
listOptions,
|
||||
|
||||
getAdminTokenOrDie,
|
||||
buildServer,
|
||||
assignToken
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './cli.js'
|
||||
新しい課題から参照
ユーザをブロックする