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