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 async function delInstance (url: string) {
9 const settings = await getSettings()
11 settings.remotes.splice(settings.remotes.indexOf(url))
12 await writeSettings(settings)
14 delete netrc.machines[url]
18 async function setInstance (url: string, username: string, password: string) {
19 const settings = await getSettings()
20 if (settings.remotes.indexOf(url) === -1) {
21 settings.remotes.push(url)
23 await writeSettings(settings)
25 netrc.machines[url] = { login: username, password }
29 function isURLaPeerTubeInstance (url: string) {
30 return isHostValid(url) || (url.includes('localhost'))
35 .usage('[command] [options]')
39 .description('remember your accounts on remote instances for easier use')
40 .option('-u, --url <url>', 'Server url')
41 .option('-U, --username <username>', 'Username')
42 .option('-p, --password <token>', 'Password')
43 .option('--default', 'add the entry as the new default')
45 prompt.override = options
50 description: 'instance url',
51 conform: (value) => isURLaPeerTubeInstance(value),
55 conform: (value) => isUserUsernameValid(value),
56 message: 'Name must be only letters, spaces, or dashes',
65 }, async (_, result) => {
66 await setInstance(result.url, result.username, result.password)
74 .description('unregisters a remote instance')
75 .action(async url => {
76 await delInstance(url)
83 .description('lists registered remote instances')
85 const settings = await getSettings()
86 const table = new Table({
87 head: ['instance', 'login'],
91 settings.remotes.forEach(element => {
94 netrc.machines[element].login
98 console.log(table.toString())
104 .command('set-default <url>')
105 .description('set an existing entry as default')
106 .action(async url => {
107 const settings = await getSettings()
108 const instanceExists = settings.remotes.indexOf(url) !== -1
110 if (instanceExists) {
111 settings.default = settings.remotes.indexOf(url)
112 await writeSettings(settings)
116 console.log('<url> is not a registered instance.')
121 program.on('--help', function () {
122 console.log(' Examples:')
124 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
125 console.log(' $ peertube add -u peertube.cpy.re -U root')
126 console.log(' $ peertube list')
127 console.log(' $ peertube del peertube.cpy.re')
131 if (!process.argv.slice(2).length) {
135 program.parse(process.argv)