1 import * as program from 'commander'
2 import * as prompt from 'prompt'
3 const Table = require('cli-table')
4 import { getSettings, writeSettings, netrc } from './cli'
5 import { isHostValid } from '../helpers/custom-validators/servers'
6 import { isUserUsernameValid } from '../helpers/custom-validators/users'
8 function delInstance (url: string) {
9 return new Promise((res, rej): void => {
11 .then(async (settings) => {
12 settings.remotes.splice(settings.remotes.indexOf(url))
13 await writeSettings(settings)
14 delete netrc.machines[url]
18 .catch(err => rej(err))
22 async function setInstance (url: string, username: string, password: string) {
23 return new Promise((res, rej): void => {
25 .then(async settings => {
26 if (settings.remotes.indexOf(url) === -1) {
27 settings.remotes.push(url)
29 await writeSettings(settings)
30 netrc.machines[url] = { login: username, password }
34 .catch(err => rej(err))
38 function isURLaPeerTubeInstance (url: string) {
39 return isHostValid(url) || (url.includes('localhost'))
44 .usage('[command] [options]')
48 .description('remember your accounts on remote instances for easier use')
49 .option('-u, --url <url>', 'Server url')
50 .option('-U, --username <username>', 'Username')
51 .option('-p, --password <token>', 'Password')
52 .option('--default', 'add the entry as the new default')
54 prompt.override = options
59 description: 'instance url',
60 conform: (value) => isURLaPeerTubeInstance(value),
64 conform: (value) => isUserUsernameValid(value),
65 message: 'Name must be only letters, spaces, or dashes',
75 setInstance(result.url, result.username, result.password)
81 .description('unregisters a remote instance')
88 .description('lists registered remote instances')
92 const table = new Table({
93 head: ['instance', 'login'],
97 settings.remotes.forEach(element => {
100 netrc.machines[element].login
104 console.log(table.toString())
109 .command('set-default <url>')
110 .description('set an existing entry as default')
114 const instanceExists = settings.remotes.indexOf(url) !== -1
116 if (instanceExists) {
117 settings.default = settings.remotes.indexOf(url)
118 writeSettings(settings)
120 console.log('<url> is not a registered instance.')
126 program.on('--help', function () {
127 console.log(' Examples:')
129 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
130 console.log(' $ peertube add -u peertube.cpy.re -U root')
131 console.log(' $ peertube list')
132 console.log(' $ peertube del peertube.cpy.re')
136 if (!process.argv.slice(2).length) {
140 program.parse(process.argv)