1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
3 import { registerTSPaths } from '../helpers/register-ts-paths'
6 import * as program from 'commander'
7 import * as prompt from 'prompt'
8 import { getNetrc, getSettings, writeSettings } from './cli'
9 import { isUserUsernameValid } from '../helpers/custom-validators/users'
10 import { getAccessToken } from '../../shared/extra-utils'
11 import * as CliTable3 from 'cli-table3'
13 async function delInstance (url: string) {
14 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
16 const index = settings.remotes.indexOf(url)
17 settings.remotes.splice(index)
19 if (settings.default === index) settings.default = -1
21 await writeSettings(settings)
23 delete netrc.machines[url]
28 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
29 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
31 if (settings.remotes.includes(url) === false) {
32 settings.remotes.push(url)
35 if (isDefault || settings.remotes.length === 1) {
36 settings.default = settings.remotes.length - 1
39 await writeSettings(settings)
41 netrc.machines[url] = { login: username, password }
45 function isURLaPeerTubeInstance (url: string) {
46 return url.startsWith('http://') || url.startsWith('https://')
51 .usage('[command] [options]')
55 .description('remember your accounts on remote instances for easier use')
56 .option('-u, --url <url>', 'Server url')
57 .option('-U, --username <username>', 'Username')
58 .option('-p, --password <token>', 'Password')
59 .option('--default', 'add the entry as the new default')
61 prompt.override = options
66 description: 'instance url',
67 conform: (value) => isURLaPeerTubeInstance(value),
68 message: 'It should be an URL (https://peertube.example.com)',
72 conform: (value) => isUserUsernameValid(value),
73 message: 'Name must be only letters, spaces, or dashes',
82 }, async (_, result) => {
85 await getAccessToken(result.url, result.username, result.password)
87 console.error(err.message)
91 await setInstance(result.url, result.username, result.password, program['default'])
99 .description('unregisters a remote instance')
100 .action(async url => {
101 await delInstance(url)
108 .description('lists registered remote instances')
109 .action(async () => {
110 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
112 const table = new CliTable3({
113 head: [ 'instance', 'login' ],
114 colWidths: [ 30, 30 ]
117 settings.remotes.forEach(element => {
118 if (!netrc.machines[element]) return
122 netrc.machines[element].login
126 console.log(table.toString())
132 .command('set-default <url>')
133 .description('set an existing entry as default')
134 .action(async url => {
135 const settings = await getSettings()
136 const instanceExists = settings.remotes.includes(url)
138 if (instanceExists) {
139 settings.default = settings.remotes.indexOf(url)
140 await writeSettings(settings)
144 console.log('<url> is not a registered instance.')
149 program.on('--help', function () {
150 console.log(' Examples:')
152 console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
153 console.log(' $ peertube add -u https://peertube.cpy.re -U root')
154 console.log(' $ peertube list')
155 console.log(' $ peertube del https://peertube.cpy.re')
159 if (!process.argv.slice(2).length) {
163 program.parse(process.argv)