1 import * as program from 'commander'
2 import * as prompt from 'prompt'
3 import { getSettings, writeSettings, getNetrc } from './cli'
4 import { isHostValid } from '../helpers/custom-validators/servers'
5 import { isUserUsernameValid } from '../helpers/custom-validators/users'
7 const Table = require('cli-table')
9 async function delInstance (url: string) {
10 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
12 const index = settings.remotes.indexOf(url)
13 settings.remotes.splice(index)
15 if (settings.default === index) settings.default = -1
17 await writeSettings(settings)
19 delete netrc.machines[url]
24 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
25 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
27 if (settings.remotes.indexOf(url) === -1) {
28 settings.remotes.push(url)
31 if (isDefault || settings.remotes.length === 1) {
32 settings.default = settings.remotes.length - 1
35 await writeSettings(settings)
37 netrc.machines[url] = { login: username, password }
41 function isURLaPeerTubeInstance (url: string) {
42 return isHostValid(url) || (url.includes('localhost'))
47 .usage('[command] [options]')
51 .description('remember your accounts on remote instances for easier use')
52 .option('-u, --url <url>', 'Server url')
53 .option('-U, --username <username>', 'Username')
54 .option('-p, --password <token>', 'Password')
55 .option('--default', 'add the entry as the new default')
57 prompt.override = options
62 description: 'instance url',
63 conform: (value) => isURLaPeerTubeInstance(value),
67 conform: (value) => isUserUsernameValid(value),
68 message: 'Name must be only letters, spaces, or dashes',
77 }, async (_, result) => {
78 await setInstance(result.url, result.username, result.password, program['default'])
86 .description('unregisters a remote instance')
87 .action(async url => {
88 await delInstance(url)
95 .description('lists registered remote instances')
97 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
99 const table = new Table({
100 head: ['instance', 'login'],
104 settings.remotes.forEach(element => {
105 if (!netrc.machines[element]) return
109 netrc.machines[element].login
113 console.log(table.toString())
119 .command('set-default <url>')
120 .description('set an existing entry as default')
121 .action(async url => {
122 const settings = await getSettings()
123 const instanceExists = settings.remotes.indexOf(url) !== -1
125 if (instanceExists) {
126 settings.default = settings.remotes.indexOf(url)
127 await writeSettings(settings)
131 console.log('<url> is not a registered instance.')
136 program.on('--help', function () {
137 console.log(' Examples:')
139 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
140 console.log(' $ peertube add -u peertube.cpy.re -U root')
141 console.log(' $ peertube list')
142 console.log(' $ peertube del peertube.cpy.re')
146 if (!process.argv.slice(2).length) {
150 program.parse(process.argv)