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