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