aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/cli.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/cli.ts')
-rw-r--r--server/tools/cli.ts112
1 files changed, 58 insertions, 54 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 58e2445ac..d5416fc38 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -3,9 +3,12 @@ import { getAppNumber, isTestInstance } from '../helpers/core-utils'
3import { join } from 'path' 3import { join } from 'path'
4import { root } from '../../shared/extra-utils/miscs/miscs' 4import { root } from '../../shared/extra-utils/miscs/miscs'
5import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels' 5import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
6import { Command } from 'commander' 6import { CommanderStatic } from 'commander'
7import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' 7import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
8import { createLogger, format, transports } from 'winston' 8import { createLogger, format, transports } from 'winston'
9import { getMyUserInformation } from '@shared/extra-utils/users/users'
10import { User, UserRole } from '@shared/models'
11import { getAccessToken } from '@shared/extra-utils/users/login'
9 12
10let configName = 'PeerTube/CLI' 13let configName = 'PeerTube/CLI'
11if (isTestInstance()) configName += `-${getAppNumber()}` 14if (isTestInstance()) configName += `-${getAppNumber()}`
@@ -14,24 +17,35 @@ const config = require('application-config')(configName)
14 17
15const version = require('../../../package.json').version 18const version = require('../../../package.json').version
16 19
20async function getAdminTokenOrDie (url: string, username: string, password: string) {
21 const accessToken = await getAccessToken(url, username, password)
22 const resMe = await getMyUserInformation(url, accessToken)
23 const me: User = resMe.body
24
25 if (me.role !== UserRole.ADMINISTRATOR) {
26 console.error('You must be an administrator.')
27 process.exit(-1)
28 }
29
30 return accessToken
31}
32
17interface Settings { 33interface Settings {
18 remotes: any[], 34 remotes: any[]
19 default: number 35 default: number
20} 36}
21 37
22function getSettings () { 38async function getSettings (): Promise<Settings> {
23 return new Promise<Settings>((res, rej) => { 39 const defaultSettings = {
24 const defaultSettings = { 40 remotes: [],
25 remotes: [], 41 default: -1
26 default: -1 42 }
27 }
28 43
29 config.read((err, data) => { 44 const data = await config.read()
30 if (err) return rej(err)
31 45
32 return res(Object.keys(data).length === 0 ? defaultSettings : data) 46 return Object.keys(data).length === 0
33 }) 47 ? defaultSettings
34 }) 48 : data
35} 49}
36 50
37async function getNetrc () { 51async function getNetrc () {
@@ -46,24 +60,12 @@ async function getNetrc () {
46 return netrc 60 return netrc
47} 61}
48 62
49function writeSettings (settings) { 63function writeSettings (settings: Settings) {
50 return new Promise((res, rej) => { 64 return config.write(settings)
51 config.write(settings, err => {
52 if (err) return rej(err)
53
54 return res()
55 })
56 })
57} 65}
58 66
59function deleteSettings () { 67function deleteSettings () {
60 return new Promise((res, rej) => { 68 return config.trash()
61 config.trash((err) => {
62 if (err) return rej(err)
63
64 return res()
65 })
66 })
67} 69}
68 70
69function getRemoteObjectOrDie ( 71function getRemoteObjectOrDie (
@@ -74,9 +76,9 @@ function getRemoteObjectOrDie (
74 if (!program['url'] || !program['username'] || !program['password']) { 76 if (!program['url'] || !program['username'] || !program['password']) {
75 // No remote and we don't have program parameters: quit 77 // No remote and we don't have program parameters: quit
76 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { 78 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
77 if (!program[ 'url' ]) console.error('--url field is required.') 79 if (!program['url']) console.error('--url field is required.')
78 if (!program[ 'username' ]) console.error('--username field is required.') 80 if (!program['username']) console.error('--username field is required.')
79 if (!program[ 'password' ]) console.error('--password field is required.') 81 if (!program['password']) console.error('--password field is required.')
80 82
81 return process.exit(-1) 83 return process.exit(-1)
82 } 84 }
@@ -96,13 +98,13 @@ function getRemoteObjectOrDie (
96 } 98 }
97 99
98 return { 100 return {
99 url: program[ 'url' ], 101 url: program['url'],
100 username: program[ 'username' ], 102 username: program['username'],
101 password: program[ 'password' ] 103 password: program['password']
102 } 104 }
103} 105}
104 106
105function buildCommonVideoOptions (command: Command) { 107function buildCommonVideoOptions (command: CommanderStatic) {
106 function list (val) { 108 function list (val) {
107 return val.split(',') 109 return val.split(',')
108 } 110 }
@@ -117,13 +119,14 @@ function buildCommonVideoOptions (command: Command) {
117 .option('-d, --video-description <description>', 'Video description') 119 .option('-d, --video-description <description>', 'Video description')
118 .option('-P, --privacy <privacy_number>', 'Privacy') 120 .option('-P, --privacy <privacy_number>', 'Privacy')
119 .option('-C, --channel-name <channel_name>', 'Channel name') 121 .option('-C, --channel-name <channel_name>', 'Channel name')
120 .option('-m, --comments-enabled', 'Enable comments') 122 .option('--no-comments-enabled', 'Disable video comments')
121 .option('-s, --support <support>', 'Video support text') 123 .option('-s, --support <support>', 'Video support text')
122 .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video') 124 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
125 .option('--no-download-enabled', 'Disable video download')
123 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') 126 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
124} 127}
125 128
126async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { 129async function buildVideoAttributesFromCommander (url: string, command: CommanderStatic, defaultAttributes: any = {}) {
127 const defaultBooleanAttributes = { 130 const defaultBooleanAttributes = {
128 nsfw: false, 131 nsfw: false,
129 commentsEnabled: true, 132 commentsEnabled: true,
@@ -134,8 +137,8 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
134 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {} 137 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
135 138
136 for (const key of Object.keys(defaultBooleanAttributes)) { 139 for (const key of Object.keys(defaultBooleanAttributes)) {
137 if (command[ key ] !== undefined) { 140 if (command[key] !== undefined) {
138 booleanAttributes[key] = command[ key ] 141 booleanAttributes[key] = command[key]
139 } else if (defaultAttributes[key] !== undefined) { 142 } else if (defaultAttributes[key] !== undefined) {
140 booleanAttributes[key] = defaultAttributes[key] 143 booleanAttributes[key] = defaultAttributes[key]
141 } else { 144 } else {
@@ -144,19 +147,19 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
144 } 147 }
145 148
146 const videoAttributes = { 149 const videoAttributes = {
147 name: command[ 'videoName' ] || defaultAttributes.name, 150 name: command['videoName'] || defaultAttributes.name,
148 category: command[ 'category' ] || defaultAttributes.category || undefined, 151 category: command['category'] || defaultAttributes.category || undefined,
149 licence: command[ 'licence' ] || defaultAttributes.licence || undefined, 152 licence: command['licence'] || defaultAttributes.licence || undefined,
150 language: command[ 'language' ] || defaultAttributes.language || undefined, 153 language: command['language'] || defaultAttributes.language || undefined,
151 privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC, 154 privacy: command['privacy'] || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
152 support: command[ 'support' ] || defaultAttributes.support || undefined, 155 support: command['support'] || defaultAttributes.support || undefined,
153 description: command[ 'videoDescription' ] || defaultAttributes.description || undefined, 156 description: command['videoDescription'] || defaultAttributes.description || undefined,
154 tags: command[ 'tags' ] || defaultAttributes.tags || undefined 157 tags: command['tags'] || defaultAttributes.tags || undefined
155 } 158 }
156 159
157 Object.assign(videoAttributes, booleanAttributes) 160 Object.assign(videoAttributes, booleanAttributes)
158 161
159 if (command[ 'channelName' ]) { 162 if (command['channelName']) {
160 const res = await getVideoChannel(url, command['channelName']) 163 const res = await getVideoChannel(url, command['channelName'])
161 const videoChannel: VideoChannel = res.body 164 const videoChannel: VideoChannel = res.body
162 165
@@ -172,9 +175,9 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
172 175
173function getServerCredentials (program: any) { 176function getServerCredentials (program: any) {
174 return Promise.all([ getSettings(), getNetrc() ]) 177 return Promise.all([ getSettings(), getNetrc() ])
175 .then(([ settings, netrc ]) => { 178 .then(([ settings, netrc ]) => {
176 return getRemoteObjectOrDie(program, settings, netrc) 179 return getRemoteObjectOrDie(program, settings, netrc)
177 }) 180 })
178} 181}
179 182
180function getLogger (logLevel = 'info') { 183function getLogger (logLevel = 'info') {
@@ -211,7 +214,6 @@ function getLogger (logLevel = 'info') {
211 214
212export { 215export {
213 version, 216 version,
214 config,
215 getLogger, 217 getLogger,
216 getSettings, 218 getSettings,
217 getNetrc, 219 getNetrc,
@@ -222,5 +224,7 @@ export {
222 getServerCredentials, 224 getServerCredentials,
223 225
224 buildCommonVideoOptions, 226 buildCommonVideoOptions,
225 buildVideoAttributesFromCommander 227 buildVideoAttributesFromCommander,
228
229 getAdminTokenOrDie
226} 230}