1 import { registerTSPaths } from '../helpers/register-ts-paths'
4 import * as program from 'commander'
5 import * as prompt from 'prompt'
6 import { getNetrc, getSettings, writeSettings } from './cli'
7 import { isUserUsernameValid } from '../helpers/custom-validators/users'
8 import { getAccessToken, login } from '../../shared/extra-utils'
10 const Table = require('cli-table')
12 async function delInstance (url: string) {
13 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
15 const index = settings.remotes.indexOf(url)
16 settings.remotes.splice(index)
18 if (settings.default === index) settings.default = -1
20 await writeSettings(settings)
22 delete netrc.machines[url]
27 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
28 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
30 if (settings.remotes.indexOf(url) === -1) {
31 settings.remotes.push(url)
34 if (isDefault || settings.remotes.length === 1) {
35 settings.default = settings.remotes.length - 1
38 await writeSettings(settings)
40 netrc.machines[url] = { login: username, password }
44 function isURLaPeerTubeInstance (url: string) {
45 return url.startsWith('http://') || url.startsWith('https://')
50 .usage('[command] [options]')
54 .description('remember your accounts on remote instances for easier use')
55 .option('-u, --url <url>', 'Server url')
56 .option('-U, --username <username>', 'Username')
57 .option('-p, --password <token>', 'Password')
58 .option('--default', 'add the entry as the new default')
60 prompt.override = options
65 description: 'instance url',
66 conform: (value) => isURLaPeerTubeInstance(value),
67 message: 'It should be an URL (https://peertube.example.com)',
71 conform: (value) => isUserUsernameValid(value),
72 message: 'Name must be only letters, spaces, or dashes',
81 }, async (_, result) => {
84 await getAccessToken(result.url, result.username, result.password)
86 console.error(err.message)
90 await setInstance(result.url, result.username, result.password, program['default'])
98 .description('unregisters a remote instance')
99 .action(async url => {
100 await delInstance(url)
107 .description('lists registered remote instances')
108 .action(async () => {
109 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
111 const table = new Table({
112 head: ['instance', 'login'],
116 settings.remotes.forEach(element => {
117 if (!netrc.machines[element]) return
121 netrc.machines[element].login
125 console.log(table.toString())
131 .command('set-default <url>')
132 .description('set an existing entry as default')
133 .action(async url => {
134 const settings = await getSettings()
135 const instanceExists = settings.remotes.indexOf(url) !== -1
137 if (instanceExists) {
138 settings.default = settings.remotes.indexOf(url)
139 await writeSettings(settings)
143 console.log('<url> is not a registered instance.')
148 program.on('--help', function () {
149 console.log(' Examples:')
151 console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
152 console.log(' $ peertube add -u https://peertube.cpy.re -U root')
153 console.log(' $ peertube list')
154 console.log(' $ peertube del https://peertube.cpy.re')
158 if (!process.argv.slice(2).length) {
162 program.parse(process.argv)