X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-auth.ts;h=6597a5c367e78d3d0c9a92f920c70ae990584db2;hb=51b34a11b298b466faef9c8d24beec4442d7add3;hp=8bc3d332c9cb43f24759328fb52be2be87b6f6f7;hpb=2b4dd7e26d93c2d9bef4f365cb03c511eff4ca8f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts index 8bc3d332c..6597a5c36 100644 --- a/server/tools/peertube-auth.ts +++ b/server/tools/peertube-auth.ts @@ -1,15 +1,22 @@ +import { registerTSPaths } from '../helpers/register-ts-paths' +registerTSPaths() + import * as program from 'commander' import * as prompt from 'prompt' -import { getSettings, writeSettings, getNetrc } from './cli' -import { isHostValid } from '../helpers/custom-validators/servers' +import { getNetrc, getSettings, writeSettings } from './cli' import { isUserUsernameValid } from '../helpers/custom-validators/users' +import { getAccessToken, login } from '../../shared/extra-utils' const Table = require('cli-table') async function delInstance (url: string) { const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) - settings.remotes.splice(settings.remotes.indexOf(url)) + const index = settings.remotes.indexOf(url) + settings.remotes.splice(index) + + if (settings.default === index) settings.default = -1 + await writeSettings(settings) delete netrc.machines[url] @@ -17,12 +24,17 @@ async function delInstance (url: string) { await netrc.save() } -async function setInstance (url: string, username: string, password: string) { +async function setInstance (url: string, username: string, password: string, isDefault: boolean) { const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) if (settings.remotes.indexOf(url) === -1) { settings.remotes.push(url) } + + if (isDefault || settings.remotes.length === 1) { + settings.default = settings.remotes.length - 1 + } + await writeSettings(settings) netrc.machines[url] = { login: username, password } @@ -30,7 +42,7 @@ async function setInstance (url: string, username: string, password: string) { } function isURLaPeerTubeInstance (url: string) { - return isHostValid(url) || (url.includes('localhost')) + return url.startsWith('http://') || url.startsWith('https://') } program @@ -52,6 +64,7 @@ program url: { description: 'instance url', conform: (value) => isURLaPeerTubeInstance(value), + message: 'It should be an URL (https://peertube.example.com)', required: true }, username: { @@ -66,7 +79,15 @@ program } } }, async (_, result) => { - await setInstance(result.url, result.username, result.password) + // Check credentials + try { + await getAccessToken(result.url, result.username, result.password) + } catch (err) { + console.error(err.message) + process.exit(-1) + } + + await setInstance(result.url, result.username, result.password, program['default']) process.exit(0) }) @@ -93,6 +114,8 @@ program }) settings.remotes.forEach(element => { + if (!netrc.machines[element]) return + table.push([ element, netrc.machines[element].login @@ -125,10 +148,10 @@ program program.on('--help', function () { console.log(' Examples:') console.log() - console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"') - console.log(' $ peertube add -u peertube.cpy.re -U root') + console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"') + console.log(' $ peertube add -u https://peertube.cpy.re -U root') console.log(' $ peertube list') - console.log(' $ peertube del peertube.cpy.re') + console.log(' $ peertube del https://peertube.cpy.re') console.log() })