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.ts122
1 files changed, 107 insertions, 15 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 59e9fcfc4..2eec51aa4 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -1,5 +1,14 @@
1const config = require('application-config')('PeerTube/CLI') 1import { Netrc } from 'netrc-parser'
2const netrc = require('netrc-parser').default 2import { getAppNumber, isTestInstance } from '../helpers/core-utils'
3import { join } from 'path'
4import { getVideoChannel, root } from '../../shared/extra-utils'
5import { Command } from 'commander'
6import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
7
8let configName = 'PeerTube/CLI'
9if (isTestInstance()) configName += `-${getAppNumber()}`
10
11const config = require('application-config')(configName)
3 12
4const version = require('../../../package.json').version 13const version = require('../../../package.json').version
5 14
@@ -12,7 +21,7 @@ function getSettings () {
12 return new Promise<Settings>((res, rej) => { 21 return new Promise<Settings>((res, rej) => {
13 const defaultSettings = { 22 const defaultSettings = {
14 remotes: [], 23 remotes: [],
15 default: 0 24 default: -1
16 } 25 }
17 26
18 config.read((err, data) => { 27 config.read((err, data) => {
@@ -24,6 +33,12 @@ function getSettings () {
24} 33}
25 34
26async function getNetrc () { 35async function getNetrc () {
36 const Netrc = require('netrc-parser').Netrc
37
38 const netrc = isTestInstance()
39 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
40 : new Netrc()
41
27 await netrc.load() 42 await netrc.load()
28 43
29 return netrc 44 return netrc
@@ -31,7 +46,17 @@ async function getNetrc () {
31 46
32function writeSettings (settings) { 47function writeSettings (settings) {
33 return new Promise((res, rej) => { 48 return new Promise((res, rej) => {
34 config.write(settings, function (err) { 49 config.write(settings, err => {
50 if (err) return rej(err)
51
52 return res()
53 })
54 })
55}
56
57function deleteSettings () {
58 return new Promise((res, rej) => {
59 config.trash((err) => {
35 if (err) return rej(err) 60 if (err) return rej(err)
36 61
37 return res() 62 return res()
@@ -39,10 +64,10 @@ function writeSettings (settings) {
39 }) 64 })
40} 65}
41 66
42function getRemoteObjectOrDie (program: any, settings: Settings) { 67function getRemoteObjectOrDie (program: any, settings: Settings, netrc: Netrc) {
43 if (!program['url'] || !program['username'] || !program['password']) { 68 if (!program['url'] || !program['username'] || !program['password']) {
44 // No remote and we don't have program parameters: throw 69 // No remote and we don't have program parameters: quit
45 if (settings.remotes.length === 0) { 70 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
46 if (!program[ 'url' ]) console.error('--url field is required.') 71 if (!program[ 'url' ]) console.error('--url field is required.')
47 if (!program[ 'username' ]) console.error('--username field is required.') 72 if (!program[ 'username' ]) console.error('--username field is required.')
48 if (!program[ 'password' ]) console.error('--password field is required.') 73 if (!program[ 'password' ]) console.error('--password field is required.')
@@ -54,14 +79,12 @@ function getRemoteObjectOrDie (program: any, settings: Settings) {
54 let username: string = program['username'] 79 let username: string = program['username']
55 let password: string = program['password'] 80 let password: string = program['password']
56 81
57 if (!url) { 82 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
58 url = settings.default !== -1 83
59 ? settings.remotes[settings.default] 84 const machine = netrc.machines[url]
60 : settings.remotes[0]
61 }
62 85
63 if (!username) username = netrc.machines[url].login 86 if (!username && machine) username = machine.login
64 if (!password) password = netrc.machines[url].password 87 if (!password && machine) password = machine.password
65 88
66 return { url, username, password } 89 return { url, username, password }
67 } 90 }
@@ -73,6 +96,71 @@ function getRemoteObjectOrDie (program: any, settings: Settings) {
73 } 96 }
74} 97}
75 98
99function buildCommonVideoOptions (command: Command) {
100 function list (val) {
101 return val.split(',')
102 }
103
104 return command
105 .option('-n, --video-name <name>', 'Video name')
106 .option('-c, --category <category_number>', 'Category number')
107 .option('-l, --licence <licence_number>', 'Licence number')
108 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
109 .option('-t, --tags <tags>', 'Video tags', list)
110 .option('-N, --nsfw', 'Video is Not Safe For Work')
111 .option('-d, --video-description <description>', 'Video description')
112 .option('-P, --privacy <privacy_number>', 'Privacy')
113 .option('-C, --channel-name <channel_name>', 'Channel name')
114 .option('-m, --comments-enabled', 'Enable comments')
115 .option('-s, --support <support>', 'Video support text')
116 .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video')
117}
118
119async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any) {
120 const defaultBooleanAttributes = {
121 nsfw: false,
122 commentsEnabled: true,
123 downloadEnabled: true,
124 waitTranscoding: true
125 }
126
127 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
128
129 for (const key of Object.keys(defaultBooleanAttributes)) {
130 if (command[ key ] !== undefined) {
131 booleanAttributes[key] = command[ key ]
132 } else if (defaultAttributes[key] !== undefined) {
133 booleanAttributes[key] = defaultAttributes[key]
134 } else {
135 booleanAttributes[key] = defaultBooleanAttributes[key]
136 }
137 }
138
139 const videoAttributes = {
140 name: command[ 'videoName' ] || defaultAttributes.name,
141 category: command[ 'category' ] || defaultAttributes.category || undefined,
142 licence: command[ 'licence' ] || defaultAttributes.licence || undefined,
143 language: command[ 'language' ] || defaultAttributes.language || undefined,
144 privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
145 support: command[ 'support' ] || defaultAttributes.support || undefined
146 }
147
148 Object.assign(videoAttributes, booleanAttributes)
149
150 if (command[ 'channelName' ]) {
151 const res = await getVideoChannel(url, command['channelName'])
152 const videoChannel: VideoChannel = res.body
153
154 Object.assign(videoAttributes, { channelId: videoChannel.id })
155
156 if (!videoAttributes.support && videoChannel.support) {
157 Object.assign(videoAttributes, { support: videoChannel.support })
158 }
159 }
160
161 return videoAttributes
162}
163
76// --------------------------------------------------------------------------- 164// ---------------------------------------------------------------------------
77 165
78export { 166export {
@@ -81,5 +169,9 @@ export {
81 getSettings, 169 getSettings,
82 getNetrc, 170 getNetrc,
83 getRemoteObjectOrDie, 171 getRemoteObjectOrDie,
84 writeSettings 172 writeSettings,
173 deleteSettings,
174
175 buildCommonVideoOptions,
176 buildVideoAttributesFromCommander
85} 177}