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