1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
3 import { registerTSPaths } from '../helpers/register-ts-paths'
6 import { OptionValues, program } from 'commander'
7 import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
8 import { isUserUsernameValid } from '../helpers/custom-validators/users'
9 import CliTable3 from 'cli-table3'
11 import prompt = require('prompt')
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://')
49 function stripExtraneousFromPeerTubeUrl (url: string) {
50 // Get everything before the 3rd /.
51 const urlLength = url.includes('/', 8)
55 return url.substr(0, urlLength)
60 .usage('[command] [options]')
64 .description('remember your accounts on remote instances for easier use')
65 .option('-u, --url <url>', 'Server url')
66 .option('-U, --username <username>', 'Username')
67 .option('-p, --password <token>', 'Password')
68 .option('--default', 'add the entry as the new default')
69 .action((options: OptionValues) => {
70 /* eslint-disable no-import-assign */
71 prompt.override = options
76 description: 'instance url',
77 conform: (value) => isURLaPeerTubeInstance(value),
78 message: 'It should be an URL (https://peertube.example.com)',
82 conform: (value) => isUserUsernameValid(value),
83 message: 'Name must be only letters, spaces, or dashes',
92 }, async (_, result) => {
96 // Strip out everything after the domain:port.
97 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
98 result.url = stripExtraneousFromPeerTubeUrl(result.url)
100 const server = buildServer(result.url)
101 await assignToken(server, result.username, result.password)
103 console.error(err.message)
107 await setInstance(result.url, result.username, result.password, options.default)
114 .command('del <url>')
115 .description('unregisters a remote instance')
116 .action(async url => {
117 await delInstance(url)
124 .description('lists registered remote instances')
125 .action(async () => {
126 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
128 const table = new CliTable3({
129 head: [ 'instance', 'login' ],
130 colWidths: [ 30, 30 ]
133 settings.remotes.forEach(element => {
134 if (!netrc.machines[element]) return
138 netrc.machines[element].login
142 console.log(table.toString())
148 .command('set-default <url>')
149 .description('set an existing entry as default')
150 .action(async url => {
151 const settings = await getSettings()
152 const instanceExists = settings.remotes.includes(url)
154 if (instanceExists) {
155 settings.default = settings.remotes.indexOf(url)
156 await writeSettings(settings)
160 console.log('<url> is not a registered instance.')
165 program.addHelpText('after', '\n\n Examples:\n\n' +
166 ' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
167 ' $ peertube auth add -u https://peertube.cpy.re -U root\n' +
168 ' $ peertube auth list\n' +
169 ' $ peertube auth del https://peertube.cpy.re\n'
172 if (!process.argv.slice(2).length) {
176 program.parse(process.argv)