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