1 import * as program from 'commander'
2 import * as prompt from 'prompt'
3 import { getNetrc, getSettings, writeSettings } from './cli'
4 import { isUserUsernameValid } from '../helpers/custom-validators/users'
5 import { getAccessToken, login } from '../../shared/extra-utils'
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 url.startsWith('http://') || url.startsWith('https://')
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),
64 message: 'It should be an URL (https://peertube.example.com)',
68 conform: (value) => isUserUsernameValid(value),
69 message: 'Name must be only letters, spaces, or dashes',
78 }, async (_, result) => {
81 await getAccessToken(result.url, result.username, result.password)
83 console.error(err.message)
87 await setInstance(result.url, result.username, result.password, program['default'])
95 .description('unregisters a remote instance')
96 .action(async url => {
97 await delInstance(url)
104 .description('lists registered remote instances')
105 .action(async () => {
106 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
108 const table = new Table({
109 head: ['instance', 'login'],
113 settings.remotes.forEach(element => {
114 if (!netrc.machines[element]) return
118 netrc.machines[element].login
122 console.log(table.toString())
128 .command('set-default <url>')
129 .description('set an existing entry as default')
130 .action(async url => {
131 const settings = await getSettings()
132 const instanceExists = settings.remotes.indexOf(url) !== -1
134 if (instanceExists) {
135 settings.default = settings.remotes.indexOf(url)
136 await writeSettings(settings)
140 console.log('<url> is not a registered instance.')
145 program.on('--help', function () {
146 console.log(' Examples:')
148 console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
149 console.log(' $ peertube add -u https://peertube.cpy.re -U root')
150 console.log(' $ peertube list')
151 console.log(' $ peertube del https://peertube.cpy.re')
155 if (!process.argv.slice(2).length) {
159 program.parse(process.argv)