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 000000000000..31feaf74bf0d Binary files /dev/null and b/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip differ diff --git a/src/cli_plugin/install/index.js b/src/cli_plugin/install/index.js index 6178563a9266..d75c0258d0f8 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, --overrideFolderName', '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..9d437fd5a796 100644 --- a/src/cli_plugin/install/install.js +++ b/src/cli_plugin/install/install.js @@ -63,7 +63,10 @@ export async function install(settings, logger) { assertVersion(settings); - const targetDir = path.join(settings.pluginDir, kebabCase(settings.plugins[0].id)); + settings.plugins[0].folderName = settings.overrideFolderName + ? settings.plugins[0].folderName + : kebabCase(settings.plugins[0].id); + const targetDir = path.join(settings.pluginDir, settings.plugins[0].folderName); 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..831a7f5ce745 100644 --- a/src/cli_plugin/install/opensearch_dashboards.js +++ b/src/cli_plugin/install/opensearch_dashboards.js @@ -47,6 +47,18 @@ export function existingInstall(settings, logger) { } catch (e) { if (e.code !== 'ENOENT') throw e; } + if (settings.plugins[0].folderName) { + try { + statSync(path.join(settings.pluginDir, 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..5ed283db82e3 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 || '', + overrideFolderName: options.overrideFolderName || 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..47b36568e34d 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": "", + "overrideFolderName": false, "plugin": "plugin name", "pluginDir": /plugins, "quiet": false, @@ -87,12 +88,14 @@ describe('parse function', function () { quiet: true, silent: true, config: 'foo bar baz', + overrideFolderName: true, ...defaultOptions, }; expect(parse(command, options, osdPackage)).toMatchInlineSnapshot(` Object { "config": "foo bar baz", + "overrideFolderName": 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",