]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-auth.ts
Add more e2e tests
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
CommitLineData
8704acf4
RK
1import * as program from 'commander'
2import * as prompt from 'prompt'
2b4dd7e2 3import { getSettings, writeSettings, getNetrc } from './cli'
8704acf4
RK
4import { isHostValid } from '../helpers/custom-validators/servers'
5import { isUserUsernameValid } from '../helpers/custom-validators/users'
6
2b4dd7e2
C
7const Table = require('cli-table')
8
e5cb43e0 9async function delInstance (url: string) {
2b4dd7e2 10 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
e5cb43e0 11
1a12f66d
C
12 const index = settings.remotes.indexOf(url)
13 settings.remotes.splice(index)
14
15 if (settings.default === index) settings.default = -1
16
e5cb43e0
C
17 await writeSettings(settings)
18
19 delete netrc.machines[url]
2b4dd7e2 20
e5cb43e0 21 await netrc.save()
8704acf4
RK
22}
23
1a12f66d 24async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
2b4dd7e2
C
25 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
26
e5cb43e0
C
27 if (settings.remotes.indexOf(url) === -1) {
28 settings.remotes.push(url)
29 }
1a12f66d
C
30
31 if (isDefault || settings.remotes.length === 1) {
32 settings.default = settings.remotes.length - 1
33 }
34
e5cb43e0
C
35 await writeSettings(settings)
36
37 netrc.machines[url] = { login: username, password }
38 await netrc.save()
8704acf4
RK
39}
40
41function isURLaPeerTubeInstance (url: string) {
42 return isHostValid(url) || (url.includes('localhost'))
43}
44
45program
46 .name('auth')
47 .usage('[command] [options]')
48
49program
50 .command('add')
51 .description('remember your accounts on remote instances for easier use')
52 .option('-u, --url <url>', 'Server url')
53 .option('-U, --username <username>', 'Username')
54 .option('-p, --password <token>', 'Password')
55 .option('--default', 'add the entry as the new default')
56 .action(options => {
57 prompt.override = options
58 prompt.start()
59 prompt.get({
60 properties: {
61 url: {
62 description: 'instance url',
63 conform: (value) => isURLaPeerTubeInstance(value),
64 required: true
65 },
66 username: {
67 conform: (value) => isUserUsernameValid(value),
68 message: 'Name must be only letters, spaces, or dashes',
69 required: true
70 },
71 password: {
72 hidden: true,
73 replace: '*',
74 required: true
75 }
76 }
e5cb43e0 77 }, async (_, result) => {
1a12f66d 78 await setInstance(result.url, result.username, result.password, program['default'])
e5cb43e0
C
79
80 process.exit(0)
8704acf4
RK
81 })
82 })
83
84program
85 .command('del <url>')
86 .description('unregisters a remote instance')
e5cb43e0
C
87 .action(async url => {
88 await delInstance(url)
89
90 process.exit(0)
8704acf4
RK
91 })
92
93program
94 .command('list')
95 .description('lists registered remote instances')
e5cb43e0 96 .action(async () => {
2b4dd7e2
C
97 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
98
e5cb43e0
C
99 const table = new Table({
100 head: ['instance', 'login'],
101 colWidths: [30, 30]
102 })
2b4dd7e2 103
e5cb43e0 104 settings.remotes.forEach(element => {
9f167f12
C
105 if (!netrc.machines[element]) return
106
e5cb43e0
C
107 table.push([
108 element,
109 netrc.machines[element].login
110 ])
111 })
112
113 console.log(table.toString())
114
115 process.exit(0)
8704acf4
RK
116 })
117
118program
119 .command('set-default <url>')
120 .description('set an existing entry as default')
e5cb43e0
C
121 .action(async url => {
122 const settings = await getSettings()
123 const instanceExists = settings.remotes.indexOf(url) !== -1
124
125 if (instanceExists) {
126 settings.default = settings.remotes.indexOf(url)
127 await writeSettings(settings)
128
129 process.exit(0)
130 } else {
131 console.log('<url> is not a registered instance.')
132 process.exit(-1)
133 }
8704acf4
RK
134 })
135
136program.on('--help', function () {
137 console.log(' Examples:')
138 console.log()
139 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
140 console.log(' $ peertube add -u peertube.cpy.re -U root')
141 console.log(' $ peertube list')
142 console.log(' $ peertube del peertube.cpy.re')
143 console.log()
144})
145
146if (!process.argv.slice(2).length) {
147 program.outputHelp()
148}
149
150program.parse(process.argv)