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 * as prompt from 'prompt'
8 import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
9 import { isUserUsernameValid } from '../helpers/custom-validators/users'
10 import * as CliTable3 from 'cli-table3'
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.includes(url) === false) {
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://')
48 function stripExtraneousFromPeerTubeUrl (url: string) {
49 // Get everything before the 3rd /.
50 const urlLength = url.includes('/', 8)
54 return url.substr(0, urlLength)
59 .usage('[command] [options]')
63 .description('remember your accounts on remote instances for easier use')
64 .option('-u, --url <url>', 'Server url')
65 .option('-U, --username <username>', 'Username')
66 .option('-p, --password <token>', 'Password')
67 .option('--default', 'add the entry as the new default')
68 .action((options: OptionValues) => {
69 /* eslint-disable no-import-assign */
70 prompt.override = options
75 description: 'instance url',
76 conform: (value) => isURLaPeerTubeInstance(value),
77 message: 'It should be an URL (https://peertube.example.com)',
81 conform: (value) => isUserUsernameValid(value),
82 message: 'Name must be only letters, spaces, or dashes',
91 }, async (_, result) => {
95 // Strip out everything after the domain:port.
96 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
97 result.url = stripExtraneousFromPeerTubeUrl(result.url)
99 const server = buildServer(result.url)
100 await assignToken(server, result.username, result.password)
102 console.error(err.message)
106 await setInstance(result.url, result.username, result.password, options.default)
113 .command('del <url>')
114 .description('unregisters a remote instance')
115 .action(async url => {
116 await delInstance(url)
123 .description('lists registered remote instances')
124 .action(async () => {
125 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
127 const table = new CliTable3({
128 head: [ 'instance', 'login' ],
129 colWidths: [ 30, 30 ]
132 settings.remotes.forEach(element => {
133 if (!netrc.machines[element]) return
137 netrc.machines[element].login
141 console.log(table.toString())
147 .command('set-default <url>')
148 .description('set an existing entry as default')
149 .action(async url => {
150 const settings = await getSettings()
151 const instanceExists = settings.remotes.includes(url)
153 if (instanceExists) {
154 settings.default = settings.remotes.indexOf(url)
155 await writeSettings(settings)
159 console.log('<url> is not a registered instance.')
164 program.addHelpText('after', '\n\n Examples:\n\n' +
165 ' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
166 ' $ peertube auth add -u https://peertube.cpy.re -U root\n' +
167 ' $ peertube auth list\n' +
168 ' $ peertube auth del https://peertube.cpy.re\n'
171 if (!process.argv.slice(2).length) {
175 program.parse(process.argv)