Skip to content

Commit

Permalink
Allowing custom folder name for plugin installation
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <[email protected]>
  • Loading branch information
VachaShah committed Jun 8, 2021
1 parent 433165a commit 8a8327a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 1 deletion.
Binary file not shown.
1 change: 1 addition & 0 deletions src/cli_plugin/install/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/cli_plugin/install/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions src/cli_plugin/install/opensearch_dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/cli_plugin/install/opensearch_dashboards.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand All @@ -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: [
Expand Down
20 changes: 20 additions & 0 deletions src/cli_plugin/install/pack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/cli_plugin/install/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 3 additions & 0 deletions src/cli_plugin/install/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('parse function', function () {
expect(parse(command, { ...defaultOptions }, osdPackage)).toMatchInlineSnapshot(`
Object {
"config": "",
"overrideFolderName": false,
"plugin": "plugin name",
"pluginDir": <absolute path>/plugins,
"quiet": false,
Expand All @@ -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": <absolute path>/plugins,
"quiet": true,
Expand Down
1 change: 1 addition & 0 deletions src/cli_plugin/install/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/cli_plugin/install/zip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8a8327a

Please sign in to comment.