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) => {
83 const stripExtraneousFromPeerTubeUrl = function (url: string) {
84 // Get everything before the 3rd /.
85 const urlLength: number = url.includes('/', 8) ? url.indexOf('/', 8) : url.length
87 return url.substr(0, urlLength)
92 // Strip out everything after the domain:port.
93 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
94 result.url = stripExtraneousFromPeerTubeUrl(result.url)
96 await getAccessToken(result.url, result.username, result.password)
98 console.error(err.message)
102 await setInstance(result.url, result.username, result.password, program['default'])
109 .command('del <url>')
110 .description('unregisters a remote instance')
111 .action(async url => {
112 await delInstance(url)
119 .description('lists registered remote instances')
120 .action(async () => {
121 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
123 const table = new CliTable3({
124 head: [ 'instance', 'login' ],
125 colWidths: [ 30, 30 ]
128 settings.remotes.forEach(element => {
129 if (!netrc.machines[element]) return
133 netrc.machines[element].login
137 console.log(table.toString())
143 .command('set-default <url>')
144 .description('set an existing entry as default')
145 .action(async url => {
146 const settings = await getSettings()
147 const instanceExists = settings.remotes.includes(url)
149 if (instanceExists) {
150 settings.default = settings.remotes.indexOf(url)
151 await writeSettings(settings)
155 console.log('<url> is not a registered instance.')
160 program.on('--help', function () {
161 console.log(' Examples:')
163 console.log(' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
164 console.log(' $ peertube auth add -u https://peertube.cpy.re -U root')
165 console.log(' $ peertube auth list')
166 console.log(' $ peertube auth del https://peertube.cpy.re')
170 if (!process.argv.slice(2).length) {
174 program.parse(process.argv)