From c23c30e791595340385c5292edac8fec3485e2b8 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Thu, 3 Jun 2021 13:16:34 -0700 Subject: [PATCH] Allowing custom folder name for plugin installation Signed-off-by: Vacha Shah --- .../replies/test_plugin_custom_folder.zip | Bin 0 -> 6325 bytes src/cli_plugin/install/index.js | 1 + src/cli_plugin/install/install.js | 4 +++- .../install/opensearch_dashboards.js | 10 +++++++++ .../install/opensearch_dashboards.test.js | 2 ++ src/cli_plugin/install/pack.test.js | 20 ++++++++++++++++++ src/cli_plugin/install/settings.js | 1 + src/cli_plugin/install/settings.test.js | 3 +++ src/cli_plugin/install/zip.js | 1 + src/cli_plugin/install/zip.test.js | 1 + 10 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip diff --git a/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip b/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip new file mode 100644 index 0000000000000000000000000000000000000000..31feaf74bf0d93f9370e4dbdb34ab3604e62bb49 GIT binary patch literal 6325 zcmb_f2|QGL8=gfOTc!^~Ax-y|C1WX3vW+#y7!m2FYnd6_#U!J;v=E_&7?3eI@rcUM!_;^9lYZersXZ>7nbxFV#&k_^3~1 zt`FYlUIIQuCVa%#?OX8d?!f+wR@xD9%yYtpm)$Iym}bKI;PT_VM`cZ2wVgGc53IT_wMT@0=}@sjT;xLql-lTq?W^C){%bB7s3;Uw zByD9+62?^Sx?a%*Bwa`jF_ubBIq>m+@{~hs%2YDx!A!zt{}6BAKms)+n8^qrcrpBG z^ex;%{jR-+in4T^HQQc~SzsNU|L?mc8%=N?JtSo?6) zVL7xiRvsCoh>iRtFBgl7!{dqeOV->&EyS5Pmc?R^+dQmkB&n-ZST|N#t81td?8w&E z>efzkFtb&6{8hXhX&yG$==6OMil4| zkZvM<{(Fd$l18u2=+uu+)7CZ9i!)OT)i-WP8Io2kXdaBo9bNECXi2-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^19%QVSpetV4m>%C z{;57d3mC%{32A(hNlg9^1;jdF^CD?53kQ;+77P|O6d!1?N6#XpVN<$!IfcHM2%W;z6U;hJ8PEd*f literal 0 HcmV?d00001 diff --git a/src/cli_plugin/install/index.js b/src/cli_plugin/install/index.js index 6178563a9266..bf388e20001b 100644 --- a/src/cli_plugin/install/index.js +++ b/src/cli_plugin/install/index.js @@ -64,6 +64,7 @@ export function installCommand(program) { 'length of time before failing; 0 for never fail', parseMilliseconds ) + .option('-o, --override', 'override the folder name for plugin installation') .description( 'install a plugin', `Common examples: diff --git a/src/cli_plugin/install/install.js b/src/cli_plugin/install/install.js index e48c7a1c1349..c65d5aeb247d 100644 --- a/src/cli_plugin/install/install.js +++ b/src/cli_plugin/install/install.js @@ -59,11 +59,13 @@ export async function install(settings, logger) { del.sync(settings.tempArchiveFile, { force: true }); + const pluginFolder = settings.override ? settings.plugins[0].folderName : settings.plugins[0].id; + settings.plugins[0].pluginFolder = pluginFolder; existingInstall(settings, logger); assertVersion(settings); - const targetDir = path.join(settings.pluginDir, kebabCase(settings.plugins[0].id)); + const targetDir = path.join(settings.pluginDir, kebabCase(settings.plugins[0].pluginFolder)); await renamePlugin(settings.workingPath, targetDir); logger.log('Plugin installation complete'); diff --git a/src/cli_plugin/install/opensearch_dashboards.js b/src/cli_plugin/install/opensearch_dashboards.js index 1aaf6a5cdf56..69236151555a 100644 --- a/src/cli_plugin/install/opensearch_dashboards.js +++ b/src/cli_plugin/install/opensearch_dashboards.js @@ -47,6 +47,16 @@ export function existingInstall(settings, logger) { } catch (e) { if (e.code !== 'ENOENT') throw e; } + try { + statSync(path.join(settings.pluginDir, kebabCase(settings.plugins[0].folderName))); + + logger.error( + `Plugin ${settings.plugins[0].id} already exists with custom folder name, please remove before installing a new version` + ); + process.exit(70); + } catch (e) { + if (e.code !== 'ENOENT') throw e; + } } export function assertVersion(settings) { diff --git a/src/cli_plugin/install/opensearch_dashboards.test.js b/src/cli_plugin/install/opensearch_dashboards.test.js index 4b518ff79c0e..78ef49715b5c 100644 --- a/src/cli_plugin/install/opensearch_dashboards.test.js +++ b/src/cli_plugin/install/opensearch_dashboards.test.js @@ -55,6 +55,7 @@ describe('opensearchDashboards cli', function () { const settings = { workingPath: testWorkingPath, tempArchiveFile: tempArchiveFilePath, + override: false, plugin: 'test-plugin', version: '1.0.0', plugins: [{ id: 'foo' }], @@ -81,6 +82,7 @@ describe('opensearchDashboards cli', function () { const settings = { workingPath: testWorkingPath, tempArchiveFile: tempArchiveFilePath, + override: false, plugin: 'test-plugin', version: '5.0.0-SNAPSHOT', plugins: [ diff --git a/src/cli_plugin/install/pack.test.js b/src/cli_plugin/install/pack.test.js index 2bf6e4db57c2..5aedcd47aff7 100644 --- a/src/cli_plugin/install/pack.test.js +++ b/src/cli_plugin/install/pack.test.js @@ -120,6 +120,7 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -134,6 +135,7 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "5.0.1", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -148,16 +150,19 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "fungerPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/funger-plugin", }, Object { + "folderName": undefined, "id": "pdf", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/pdf", }, Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -166,6 +171,21 @@ describe('opensearchDashboards cli', function () { `); }); + it('populate settings.plugin.folderName', async () => { + await copyReplyFile('test_plugin_custom_folder.zip'); + await getPackData(settings, logger); + expect(settings.plugins).toMatchInlineSnapshot(` + Array [ + Object { + "folderName": "customPluginName", + "id": "testPlugin", + "opensearchDashboardsVersion": "1.0.0", + "stripPrefix": "opensearch-dashboards/test-plugin-custom-folder", + }, + ] + `); + }); + it('throw an error if there is no opensearch-dashboards plugin', async () => { await copyReplyFile('test_plugin_no_opensearch_dashboards.zip'); await expect(getPackData(settings, logger)).rejects.toThrowErrorMatchingInlineSnapshot( diff --git a/src/cli_plugin/install/settings.js b/src/cli_plugin/install/settings.js index 6a0b5751db5e..14b956782ad6 100644 --- a/src/cli_plugin/install/settings.js +++ b/src/cli_plugin/install/settings.js @@ -62,6 +62,7 @@ export function parse(command, options, osdPackage) { quiet: options.quiet || false, silent: options.silent || false, config: options.config || '', + override: options.override || false, plugin: command, version: osdPackage.version, pluginDir: fromRoot('plugins'), diff --git a/src/cli_plugin/install/settings.test.js b/src/cli_plugin/install/settings.test.js index aee23dd2dd6f..ef57384d3874 100644 --- a/src/cli_plugin/install/settings.test.js +++ b/src/cli_plugin/install/settings.test.js @@ -66,6 +66,7 @@ describe('parse function', function () { expect(parse(command, { ...defaultOptions }, osdPackage)).toMatchInlineSnapshot(` Object { "config": "", + "override": false, "plugin": "plugin name", "pluginDir": /plugins, "quiet": false, @@ -87,12 +88,14 @@ describe('parse function', function () { quiet: true, silent: true, config: 'foo bar baz', + override: true, ...defaultOptions, }; expect(parse(command, options, osdPackage)).toMatchInlineSnapshot(` Object { "config": "foo bar baz", + "override": true, "plugin": "plugin name", "pluginDir": /plugins, "quiet": true, diff --git a/src/cli_plugin/install/zip.js b/src/cli_plugin/install/zip.js index 22db10d0a3ce..a3ad488d2e8b 100644 --- a/src/cli_plugin/install/zip.js +++ b/src/cli_plugin/install/zip.js @@ -82,6 +82,7 @@ export function analyzeArchive(archive) { plugins.push({ id: manifest.id, stripPrefix: match[1], + folderName: manifest.folderName, // Plugins must specify their version, and by default that version in the plugin // manifest should match the version of opensearch-dashboards down to the patch level. If these diff --git a/src/cli_plugin/install/zip.test.js b/src/cli_plugin/install/zip.test.js index df5ecd015da5..4bc3f57d41f9 100644 --- a/src/cli_plugin/install/zip.test.js +++ b/src/cli_plugin/install/zip.test.js @@ -63,6 +63,7 @@ describe('opensearchDashboards cli', function () { expect(packages).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin",