]>
Commit | Line | Data |
---|---|---|
1 | import { registerTSPaths } from '../helpers/register-ts-paths' | |
2 | registerTSPaths() | |
3 | ||
4 | import * as program from 'commander' | |
5 | import { PluginType } from '../../shared/models/plugins/plugin.type' | |
6 | import { getAccessToken } from '../../shared/extra-utils/users/login' | |
7 | import { getMyUserInformation } from '../../shared/extra-utils/users/users' | |
8 | import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins' | |
9 | import { getServerCredentials } from './cli' | |
10 | import { User, UserRole } from '../../shared/models/users' | |
11 | import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model' | |
12 | import { isAbsolute } from 'path' | |
13 | ||
14 | const Table = require('cli-table') | |
15 | ||
16 | program | |
17 | .name('plugins') | |
18 | .usage('[command] [options]') | |
19 | ||
20 | program | |
21 | .command('list') | |
22 | .description('List installed plugins') | |
23 | .option('-u, --url <url>', 'Server url') | |
24 | .option('-U, --username <username>', 'Username') | |
25 | .option('-p, --password <token>', 'Password') | |
26 | .option('-t, --only-themes', 'List themes only') | |
27 | .option('-P, --only-plugins', 'List plugins only') | |
28 | .action(() => pluginsListCLI()) | |
29 | ||
30 | program | |
31 | .command('install') | |
32 | .description('Install a plugin or a theme') | |
33 | .option('-u, --url <url>', 'Server url') | |
34 | .option('-U, --username <username>', 'Username') | |
35 | .option('-p, --password <token>', 'Password') | |
36 | .option('-P --path <path>', 'Install from a path') | |
37 | .option('-n, --npm-name <npmName>', 'Install from npm') | |
38 | .action((options) => installPluginCLI(options)) | |
39 | ||
40 | program | |
41 | .command('update') | |
42 | .description('Update a plugin or a theme') | |
43 | .option('-u, --url <url>', 'Server url') | |
44 | .option('-U, --username <username>', 'Username') | |
45 | .option('-p, --password <token>', 'Password') | |
46 | .option('-P --path <path>', 'Update from a path') | |
47 | .option('-n, --npm-name <npmName>', 'Update from npm') | |
48 | .action((options) => updatePluginCLI(options)) | |
49 | ||
50 | program | |
51 | .command('uninstall') | |
52 | .description('Uninstall a plugin or a theme') | |
53 | .option('-u, --url <url>', 'Server url') | |
54 | .option('-U, --username <username>', 'Username') | |
55 | .option('-p, --password <token>', 'Password') | |
56 | .option('-n, --npm-name <npmName>', 'NPM plugin/theme name') | |
57 | .action(options => uninstallPluginCLI(options)) | |
58 | ||
59 | if (!process.argv.slice(2).length) { | |
60 | program.outputHelp() | |
61 | } | |
62 | ||
63 | program.parse(process.argv) | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | async function pluginsListCLI () { | |
68 | const { url, username, password } = await getServerCredentials(program) | |
69 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
70 | ||
71 | let pluginType: PluginType | |
72 | if (program['onlyThemes']) pluginType = PluginType.THEME | |
73 | if (program['onlyPlugins']) pluginType = PluginType.PLUGIN | |
74 | ||
75 | const res = await listPlugins({ | |
76 | url, | |
77 | accessToken, | |
78 | start: 0, | |
79 | count: 100, | |
80 | sort: 'name', | |
81 | pluginType | |
82 | }) | |
83 | const plugins: PeerTubePlugin[] = res.body.data | |
84 | ||
85 | const table = new Table({ | |
86 | head: ['name', 'version', 'homepage'], | |
87 | colWidths: [ 50, 10, 50 ] | |
88 | }) | |
89 | ||
90 | for (const plugin of plugins) { | |
91 | const npmName = plugin.type === PluginType.PLUGIN | |
92 | ? 'peertube-plugin-' + plugin.name | |
93 | : 'peertube-theme-' + plugin.name | |
94 | ||
95 | table.push([ | |
96 | npmName, | |
97 | plugin.version, | |
98 | plugin.homepage | |
99 | ]) | |
100 | } | |
101 | ||
102 | console.log(table.toString()) | |
103 | process.exit(0) | |
104 | } | |
105 | ||
106 | async function installPluginCLI (options: any) { | |
107 | if (!options['path'] && !options['npmName']) { | |
108 | console.error('You need to specify the npm name or the path of the plugin you want to install.\n') | |
109 | program.outputHelp() | |
110 | process.exit(-1) | |
111 | } | |
112 | ||
113 | if (options['path'] && !isAbsolute(options['path'])) { | |
114 | console.error('Path should be absolute.') | |
115 | process.exit(-1) | |
116 | } | |
117 | ||
118 | const { url, username, password } = await getServerCredentials(options) | |
119 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
120 | ||
121 | try { | |
122 | await installPlugin({ | |
123 | url, | |
124 | accessToken, | |
125 | npmName: options['npmName'], | |
126 | path: options['path'] | |
127 | }) | |
128 | } catch (err) { | |
129 | console.error('Cannot install plugin.', err) | |
130 | process.exit(-1) | |
131 | return | |
132 | } | |
133 | ||
134 | console.log('Plugin installed.') | |
135 | process.exit(0) | |
136 | } | |
137 | ||
138 | async function updatePluginCLI (options: any) { | |
139 | if (!options['path'] && !options['npmName']) { | |
140 | console.error('You need to specify the npm name or the path of the plugin you want to update.\n') | |
141 | program.outputHelp() | |
142 | process.exit(-1) | |
143 | } | |
144 | ||
145 | if (options['path'] && !isAbsolute(options['path'])) { | |
146 | console.error('Path should be absolute.') | |
147 | process.exit(-1) | |
148 | } | |
149 | ||
150 | const { url, username, password } = await getServerCredentials(options) | |
151 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
152 | ||
153 | try { | |
154 | await updatePlugin({ | |
155 | url, | |
156 | accessToken, | |
157 | npmName: options['npmName'], | |
158 | path: options['path'] | |
159 | }) | |
160 | } catch (err) { | |
161 | console.error('Cannot update plugin.', err) | |
162 | process.exit(-1) | |
163 | return | |
164 | } | |
165 | ||
166 | console.log('Plugin updated.') | |
167 | process.exit(0) | |
168 | } | |
169 | ||
170 | async function uninstallPluginCLI (options: any) { | |
171 | if (!options['npmName']) { | |
172 | console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n') | |
173 | program.outputHelp() | |
174 | process.exit(-1) | |
175 | } | |
176 | ||
177 | const { url, username, password } = await getServerCredentials(options) | |
178 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
179 | ||
180 | try { | |
181 | await uninstallPlugin({ | |
182 | url, | |
183 | accessToken, | |
184 | npmName: options[ 'npmName' ] | |
185 | }) | |
186 | } catch (err) { | |
187 | console.error('Cannot uninstall plugin.', err) | |
188 | process.exit(-1) | |
189 | return | |
190 | } | |
191 | ||
192 | console.log('Plugin uninstalled.') | |
193 | process.exit(0) | |
194 | } | |
195 | ||
196 | async function getAdminTokenOrDie (url: string, username: string, password: string) { | |
197 | const accessToken = await getAccessToken(url, username, password) | |
198 | const resMe = await getMyUserInformation(url, accessToken) | |
199 | const me: User = resMe.body | |
200 | ||
201 | if (me.role !== UserRole.ADMINISTRATOR) { | |
202 | console.error('Cannot list plugins if you are not administrator.') | |
203 | process.exit(-1) | |
204 | } | |
205 | ||
206 | return accessToken | |
207 | } |