Skip to content

Commit

Permalink
Merge pull request #6083 from epixa/6074-tgz-plugins
Browse files Browse the repository at this point in the history
Plugin installer support for .tgz file types
  • Loading branch information
epixa committed Feb 8, 2016
2 parents 93b891b + 906611e commit 5aab99f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 21 deletions.
31 changes: 31 additions & 0 deletions src/cli/plugin/__tests__/file_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import expect from 'expect.js';
import fileType, { ZIP, TAR } from '../file_type';

describe('kibana cli', function () {
describe('file_type', function () {
it('returns ZIP for .zip filename', function () {
const type = fileType('wat.zip');
expect(type).to.equal(ZIP);
});
it('returns TAR for .tar.gz filename', function () {
const type = fileType('wat.tar.gz');
expect(type).to.equal(TAR);
});
it('returns TAR for .tgz filename', function () {
const type = fileType('wat.tgz');
expect(type).to.equal(TAR);
});
it('returns undefined for unknown file type', function () {
const type = fileType('wat.unknown');
expect(type).to.equal(undefined);
});
it('accepts paths', function () {
const type = fileType('/some/path/to/wat.zip');
expect(type).to.equal(ZIP);
});
it('accepts urls', function () {
const type = fileType('http://example.com/wat.zip');
expect(type).to.equal(ZIP);
});
});
});
19 changes: 19 additions & 0 deletions src/cli/plugin/__tests__/plugin_downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ describe('kibana cli', function () {
});
});

it('should consider .tgz files as archive type .tar.gz', function () {
const filePath = join(__dirname, 'replies/test_plugin_master.tar.gz');

const couchdb = nock('http://www.files.com')
.defaultReplyHeaders({
'content-length': '10'
})
.get('/plugin.tgz')
.replyWithFile(200, filePath);

const sourceUrl = 'http://www.files.com/plugin.tgz';

return downloader._downloadSingle(sourceUrl)
.then(function (data) {
expect(data.archiveType).to.be('.tar.gz');
expectWorkingPathNotEmpty();
});
});

it('should download a zip from a valid http url', function () {
const filePath = join(__dirname, 'replies/test_plugin_master.zip');

Expand Down
12 changes: 2 additions & 10 deletions src/cli/plugin/downloaders/file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getProgressReporter from '../progress_reporter';
import { createWriteStream, createReadStream, unlinkSync, statSync } from 'fs';
import fileType from '../file_type';

function openSourceFile({ sourcePath }) {
try {
Expand Down Expand Up @@ -36,15 +37,6 @@ async function copyFile({ readStream, writeStream, progressReporter }) {
});
}

function getArchiveTypeFromFilename(path) {
if (/\.zip$/i.test(path)) {
return '.zip';
}
if (/\.tar\.gz$/i.test(path)) {
return '.tar.gz';
}
}

/*
// Responsible for managing local file transfers
*/
Expand All @@ -67,7 +59,7 @@ export default async function copyLocalFile(logger, sourcePath, targetPath) {
}

// all is well, return our archive type
const archiveType = getArchiveTypeFromFilename(sourcePath);
const archiveType = fileType(sourcePath);
return { archiveType };
} catch (err) {
logger.error(err);
Expand Down
13 changes: 4 additions & 9 deletions src/cli/plugin/downloaders/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Wreck from 'wreck';
import getProgressReporter from '../progress_reporter';
import { fromNode as fn } from 'bluebird';
import { createWriteStream, unlinkSync } from 'fs';
import fileType, { ZIP, TAR } from '../file_type';

function sendRequest({ sourceUrl, timeout }) {
const maxRedirects = 11; //Because this one goes to 11.
Expand Down Expand Up @@ -49,18 +50,12 @@ function getArchiveTypeFromResponse(resp, sourceUrl) {
const contentType = (resp.headers['content-type'] || '');

switch (contentType.toLowerCase()) {
case 'application/zip': return '.zip';
case 'application/x-gzip': return '.tar.gz';
case 'application/zip': return ZIP;
case 'application/x-gzip': return TAR;
default:
//If we can't infer the archive type from the content-type header,
//fall back to checking the extension in the url
if (/\.zip$/i.test(sourceUrl)) {
return '.zip';
}
if (/\.tar\.gz$/i.test(sourceUrl)) {
return '.tar.gz';
}
break;
return fileType(sourceUrl);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/cli/plugin/file_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const TAR = '.tar.gz';
export const ZIP = '.zip';

export default function fileType(filename) {
if (/\.zip$/i.test(filename)) {
return ZIP;
}
if (/\.tar\.gz$/i.test(filename)) {
return TAR;
}
if (/\.tgz$/i.test(filename)) {
return TAR;
}
}
5 changes: 3 additions & 2 deletions src/cli/plugin/plugin_extractor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import zipExtract from './extractors/zip';
import tarGzExtract from './extractors/tar_gz';
import { ZIP, TAR } from './file_type';

export default function extractArchive(settings, logger, archiveType) {
switch (archiveType) {
case '.zip':
case ZIP:
return zipExtract(settings, logger);
break;
case '.tar.gz':
case TAR:
return tarGzExtract(settings, logger);
break;
default:
Expand Down

0 comments on commit 5aab99f

Please sign in to comment.