aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-auth.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2018-09-13 14:27:44 +0200
committerRigel Kent <sendmemail@rigelk.eu>2018-09-14 11:08:55 +0200
commit8704acf49efc770d73bf07c10468ed8c74d28a83 (patch)
treeffd46289fcf9a13ac4412b167e9f71dfb35753c5 /server/tools/peertube-auth.ts
parent1d9d9cfdcf3983e3fd89026bc4b5633a8abf5752 (diff)
downloadPeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.tar.gz
PeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.tar.zst
PeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.zip
one cli to unite them all
Ash nazg thrakatulûk agh burzum-ishi krimpatul - refactor import-videos to use the youtubeDL helper - add very basic tests for the cli
Diffstat (limited to 'server/tools/peertube-auth.ts')
-rw-r--r--server/tools/peertube-auth.ts140
1 files changed, 140 insertions, 0 deletions
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
new file mode 100644
index 000000000..33438811e
--- /dev/null
+++ b/server/tools/peertube-auth.ts
@@ -0,0 +1,140 @@
1import * as program from 'commander'
2import * as prompt from 'prompt'
3const Table = require('cli-table')
4import { getSettings, writeSettings, netrc } from './cli'
5import { isHostValid } from '../helpers/custom-validators/servers'
6import { isUserUsernameValid } from '../helpers/custom-validators/users'
7
8function delInstance (url: string) {
9 return new Promise((res, rej): void => {
10 getSettings()
11 .then(async (settings) => {
12 settings.remotes.splice(settings.remotes.indexOf(url))
13 await writeSettings(settings)
14 delete netrc.machines[url]
15 netrc.save()
16 res()
17 })
18 .catch(err => rej(err))
19 })
20}
21
22async function setInstance (url: string, username: string, password: string) {
23 return new Promise((res, rej): void => {
24 getSettings()
25 .then(async settings => {
26 if (settings.remotes.indexOf(url) === -1) {
27 settings.remotes.push(url)
28 }
29 await writeSettings(settings)
30 netrc.machines[url] = { login: username, password }
31 netrc.save()
32 res()
33 })
34 .catch(err => rej(err))
35 })
36}
37
38function isURLaPeerTubeInstance (url: string) {
39 return isHostValid(url) || (url.includes('localhost'))
40}
41
42program
43 .name('auth')
44 .usage('[command] [options]')
45
46program
47 .command('add')
48 .description('remember your accounts on remote instances for easier use')
49 .option('-u, --url <url>', 'Server url')
50 .option('-U, --username <username>', 'Username')
51 .option('-p, --password <token>', 'Password')
52 .option('--default', 'add the entry as the new default')
53 .action(options => {
54 prompt.override = options
55 prompt.start()
56 prompt.get({
57 properties: {
58 url: {
59 description: 'instance url',
60 conform: (value) => isURLaPeerTubeInstance(value),
61 required: true
62 },
63 username: {
64 conform: (value) => isUserUsernameValid(value),
65 message: 'Name must be only letters, spaces, or dashes',
66 required: true
67 },
68 password: {
69 hidden: true,
70 replace: '*',
71 required: true
72 }
73 }
74 }, (_, result) => {
75 setInstance(result.url, result.username, result.password)
76 })
77 })
78
79program
80 .command('del <url>')
81 .description('unregisters a remote instance')
82 .action((url) => {
83 delInstance(url)
84 })
85
86program
87 .command('list')
88 .description('lists registered remote instances')
89 .action(() => {
90 getSettings()
91 .then(settings => {
92 const table = new Table({
93 head: ['instance', 'login'],
94 colWidths: [30, 30]
95 })
96 netrc.loadSync()
97 settings.remotes.forEach(element => {
98 table.push([
99 element,
100 netrc.machines[element].login
101 ])
102 })
103
104 console.log(table.toString())
105 })
106 })
107
108program
109 .command('set-default <url>')
110 .description('set an existing entry as default')
111 .action((url) => {
112 getSettings()
113 .then(settings => {
114 const instanceExists = settings.remotes.indexOf(url) !== -1
115
116 if (instanceExists) {
117 settings.default = settings.remotes.indexOf(url)
118 writeSettings(settings)
119 } else {
120 console.log('<url> is not a registered instance.')
121 process.exit(-1)
122 }
123 })
124 })
125
126program.on('--help', function () {
127 console.log(' Examples:')
128 console.log()
129 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
130 console.log(' $ peertube add -u peertube.cpy.re -U root')
131 console.log(' $ peertube list')
132 console.log(' $ peertube del peertube.cpy.re')
133 console.log()
134})
135
136if (!process.argv.slice(2).length) {
137 program.outputHelp()
138}
139
140program.parse(process.argv)