]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/cli.ts
Add more e2e tests
[github/Chocobozzz/PeerTube.git] / server / tools / cli.ts
CommitLineData
1a12f66d 1import { Netrc } from 'netrc-parser'
1205823f 2import { getAppNumber, isTestInstance } from '../helpers/core-utils'
1a12f66d 3import { join } from 'path'
1205823f
C
4import { getVideoChannel, root } from '../../shared/extra-utils'
5import { Command } from 'commander'
6import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
1a12f66d
C
7
8let configName = 'PeerTube/CLI'
9if (isTestInstance()) configName += `-${getAppNumber()}`
10
11const config = require('application-config')(configName)
8704acf4 12
499d9015 13const version = require('../../../package.json').version
8704acf4 14
8704acf4
RK
15interface Settings {
16 remotes: any[],
17 default: number
18}
19
2b4dd7e2 20function getSettings () {
8704acf4 21 return new Promise<Settings>((res, rej) => {
2b4dd7e2 22 const defaultSettings = {
8704acf4 23 remotes: [],
1a12f66d 24 default: -1
2b4dd7e2
C
25 }
26
8704acf4 27 config.read((err, data) => {
2b4dd7e2
C
28 if (err) return rej(err)
29
30 return res(Object.keys(data).length === 0 ? defaultSettings : data)
8704acf4
RK
31 })
32 })
33}
34
2b4dd7e2 35async function getNetrc () {
1a12f66d
C
36 const Netrc = require('netrc-parser').Netrc
37
38 const netrc = isTestInstance()
39 ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
40 : new Netrc()
41
2b4dd7e2
C
42 await netrc.load()
43
44 return netrc
45}
46
47function writeSettings (settings) {
8704acf4 48 return new Promise((res, rej) => {
1a12f66d
C
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) => {
2b4dd7e2
C
60 if (err) return rej(err)
61
8704acf4
RK
62 return res()
63 })
64 })
65}
66
1a12f66d 67function getRemoteObjectOrDie (program: any, settings: Settings, netrc: Netrc) {
2b4dd7e2 68 if (!program['url'] || !program['username'] || !program['password']) {
1a12f66d 69 // No remote and we don't have program parameters: quit
9f167f12 70 if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
2b4dd7e2
C
71 if (!program[ 'url' ]) console.error('--url field is required.')
72 if (!program[ 'username' ]) console.error('--username field is required.')
73 if (!program[ 'password' ]) console.error('--password field is required.')
74
75 return process.exit(-1)
76 }
77
78 let url: string = program['url']
79 let username: string = program['username']
80 let password: string = program['password']
81
1a12f66d 82 if (!url && settings.default !== -1) url = settings.remotes[settings.default]
2b4dd7e2 83
9f167f12 84 const machine = netrc.machines[url]
1a12f66d
C
85
86 if (!username && machine) username = machine.login
87 if (!password && machine) password = machine.password
2b4dd7e2
C
88
89 return { url, username, password }
90 }
91
92 return {
93 url: program[ 'url' ],
94 username: program[ 'username' ],
95 password: program[ 'password' ]
96 }
97}
8704acf4 98
1205823f
C
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) {
6b226c32
C
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 } | {} = {}
1205823f 128
6b226c32 129 for (const key of Object.keys(defaultBooleanAttributes)) {
1205823f
C
130 if (command[ key ] !== undefined) {
131 booleanAttributes[key] = command[ key ]
132 } else if (defaultAttributes[key] !== undefined) {
133 booleanAttributes[key] = defaultAttributes[key]
134 } else {
6b226c32 135 booleanAttributes[key] = defaultBooleanAttributes[key]
1205823f
C
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
8704acf4
RK
164// ---------------------------------------------------------------------------
165
166export {
167 version,
168 config,
8704acf4 169 getSettings,
2b4dd7e2
C
170 getNetrc,
171 getRemoteObjectOrDie,
1a12f66d 172 writeSettings,
1205823f
C
173 deleteSettings,
174
175 buildCommonVideoOptions,
176 buildVideoAttributesFromCommander
8704acf4 177}