]>
Commit | Line | Data |
---|---|---|
8704acf4 RK |
1 | import * as program from 'commander' |
2 | import * as prompt from 'prompt' | |
8d2be0ed | 3 | import { getNetrc, getSettings, writeSettings } from './cli' |
8704acf4 | 4 | import { isUserUsernameValid } from '../helpers/custom-validators/users' |
8d2be0ed | 5 | import { getAccessToken, login } from '../../shared/extra-utils' |
8704acf4 | 6 | |
2b4dd7e2 C |
7 | const Table = require('cli-table') |
8 | ||
e5cb43e0 | 9 | async 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 | 24 | async 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 | ||
41 | function isURLaPeerTubeInstance (url: string) { | |
fd0bfc3a | 42 | return url.startsWith('http://') || url.startsWith('https://') |
8704acf4 RK |
43 | } |
44 | ||
45 | program | |
46 | .name('auth') | |
47 | .usage('[command] [options]') | |
48 | ||
49 | program | |
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), | |
fd0bfc3a | 64 | message: 'It should be an URL (https://peertube.example.com)', |
8704acf4 RK |
65 | required: true |
66 | }, | |
67 | username: { | |
68 | conform: (value) => isUserUsernameValid(value), | |
69 | message: 'Name must be only letters, spaces, or dashes', | |
70 | required: true | |
71 | }, | |
72 | password: { | |
73 | hidden: true, | |
74 | replace: '*', | |
75 | required: true | |
76 | } | |
77 | } | |
e5cb43e0 | 78 | }, async (_, result) => { |
8d2be0ed C |
79 | // Check credentials |
80 | try { | |
81 | await getAccessToken(result.url, result.username, result.password) | |
82 | } catch (err) { | |
83 | console.error(err.message) | |
84 | process.exit(-1) | |
85 | } | |
86 | ||
1a12f66d | 87 | await setInstance(result.url, result.username, result.password, program['default']) |
e5cb43e0 C |
88 | |
89 | process.exit(0) | |
8704acf4 RK |
90 | }) |
91 | }) | |
92 | ||
93 | program | |
94 | .command('del <url>') | |
95 | .description('unregisters a remote instance') | |
e5cb43e0 C |
96 | .action(async url => { |
97 | await delInstance(url) | |
98 | ||
99 | process.exit(0) | |
8704acf4 RK |
100 | }) |
101 | ||
102 | program | |
103 | .command('list') | |
104 | .description('lists registered remote instances') | |
e5cb43e0 | 105 | .action(async () => { |
2b4dd7e2 C |
106 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
107 | ||
e5cb43e0 C |
108 | const table = new Table({ |
109 | head: ['instance', 'login'], | |
110 | colWidths: [30, 30] | |
111 | }) | |
2b4dd7e2 | 112 | |
e5cb43e0 | 113 | settings.remotes.forEach(element => { |
9f167f12 C |
114 | if (!netrc.machines[element]) return |
115 | ||
e5cb43e0 C |
116 | table.push([ |
117 | element, | |
118 | netrc.machines[element].login | |
119 | ]) | |
120 | }) | |
121 | ||
122 | console.log(table.toString()) | |
123 | ||
124 | process.exit(0) | |
8704acf4 RK |
125 | }) |
126 | ||
127 | program | |
128 | .command('set-default <url>') | |
129 | .description('set an existing entry as default') | |
e5cb43e0 C |
130 | .action(async url => { |
131 | const settings = await getSettings() | |
132 | const instanceExists = settings.remotes.indexOf(url) !== -1 | |
133 | ||
134 | if (instanceExists) { | |
135 | settings.default = settings.remotes.indexOf(url) | |
136 | await writeSettings(settings) | |
137 | ||
138 | process.exit(0) | |
139 | } else { | |
140 | console.log('<url> is not a registered instance.') | |
141 | process.exit(-1) | |
142 | } | |
8704acf4 RK |
143 | }) |
144 | ||
145 | program.on('--help', function () { | |
146 | console.log(' Examples:') | |
147 | console.log() | |
148 | console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"') | |
149 | console.log(' $ peertube add -u peertube.cpy.re -U root') | |
150 | console.log(' $ peertube list') | |
151 | console.log(' $ peertube del peertube.cpy.re') | |
152 | console.log() | |
153 | }) | |
154 | ||
155 | if (!process.argv.slice(2).length) { | |
156 | program.outputHelp() | |
157 | } | |
158 | ||
159 | program.parse(process.argv) |