1 import CliTable3 from 'cli-table3'
2 import { OptionValues, program } from 'commander'
3 import { isUserUsernameValid } from '../helpers/custom-validators/users'
4 import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
6 import prompt = require('prompt')
8 async function delInstance (url: string) {
9 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
11 const index = settings.remotes.indexOf(url)
12 settings.remotes.splice(index)
14 if (settings.default === index) settings.default = -1
16 await writeSettings(settings)
18 delete netrc.machines[url]
23 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
24 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
26 if (settings.remotes.includes(url) === false) {
27 settings.remotes.push(url)
30 if (isDefault || settings.remotes.length === 1) {
31 settings.default = settings.remotes.length - 1
34 await writeSettings(settings)
36 netrc.machines[url] = { login: username, password }
40 function isURLaPeerTubeInstance (url: string) {
41 return url.startsWith('http://') || url.startsWith('https://')
44 function stripExtraneousFromPeerTubeUrl (url: string) {
45 // Get everything before the 3rd /.
46 const urlLength = url.includes('/', 8)
50 return url.substring(0, urlLength)
55 .usage('[command] [options]')
59 .description('remember your accounts on remote instances for easier use')
60 .option('-u, --url <url>', 'Server url')
61 .option('-U, --username <username>', 'Username')
62 .option('-p, --password <token>', 'Password')
63 .option('--default', 'add the entry as the new default')
64 .action((options: OptionValues) => {
65 /* eslint-disable no-import-assign */
66 prompt.override = options
71 description: 'instance url',
72 conform: (value) => isURLaPeerTubeInstance(value),
73 message: 'It should be an URL (https://peertube.example.com)',
77 conform: (value) => isUserUsernameValid(value),
78 message: 'Name must be only letters, spaces, or dashes',
87 }, async (_, result) => {
91 // Strip out everything after the domain:port.
92 // See https://github.com/Chocobozzz/PeerTube/issues/3520
93 result.url = stripExtraneousFromPeerTubeUrl(result.url)
95 const server = buildServer(result.url)
96 await assignToken(server, result.username, result.password)
98 console.error(err.message)
102 await setInstance(result.url, result.username, result.password, options.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.addHelpText('after', '\n\n Examples:\n\n' +
161 ' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
162 ' $ peertube auth add -u https://peertube.cpy.re -U root\n' +
163 ' $ peertube auth list\n' +
164 ' $ peertube auth del https://peertube.cpy.re\n'
167 if (!process.argv.slice(2).length) {
171 program.parse(process.argv)