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