Skip to content

Commit

Permalink
Refactor archive type checks to be more DRY
Browse files Browse the repository at this point in the history
This should be an in-place refactor without any additional behaviors or
side effects.

Fixes #6147
  • Loading branch information
epixa committed Feb 8, 2016
1 parent fc7a116 commit 74edb91
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
27 changes: 27 additions & 0 deletions src/cli/plugin/__tests__/type_from_filename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import expect from 'expect.js';
import typeFromFilename, { ZIP, TAR } from '../type_from_filename';

describe('kibana cli', function () {
describe('type_from_filename', function () {
it('returns ZIP for .zip filename', function () {
const type = typeFromFilename('wat.zip');
expect(type).to.equal(ZIP);
});
it('returns TAR for .tar.gz filename', function () {
const type = typeFromFilename('wat.tar.gz');
expect(type).to.equal(TAR);
});
it('returns undefined for unknown file type', function () {
const type = typeFromFilename('wat.unknown');
expect(type).to.equal(undefined);
});
it('accepts paths', function () {
const type = typeFromFilename('/some/path/to/wat.zip');
expect(type).to.equal(ZIP);
});
it('accepts urls', function () {
const type = typeFromFilename('http://example.com/wat.zip');
expect(type).to.equal(ZIP);
});
});
});
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 @@
const { createWriteStream, createReadStream, unlinkSync, statSync } = require('fs');
const getProgressReporter = require('../progress_reporter');
import typeFromFilename from '../type_from_filename';

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 = typeFromFilename(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 @@ const { fromNode: fn } = require('bluebird');
const { createWriteStream, unlinkSync } = require('fs');
const Wreck = require('wreck');
const getProgressReporter = require('../progress_reporter');
import typeFromFilename, { ZIP, TAR } from '../type_from_filename';

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 typeFromFilename(sourceUrl);
}
}

Expand Down
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 @@
const zipExtract = require('./extractors/zip');
const tarGzExtract = require('./extractors/tar_gz');
import { ZIP, TAR } from './type_from_filename';

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
11 changes: 11 additions & 0 deletions src/cli/plugin/type_from_filename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const TAR = '.tar.gz';
export const ZIP = '.zip';

export default function typeFromFilename(filename) {
if (/\.zip$/i.test(filename)) {
return ZIP;
}
if (/\.tar\.gz$/i.test(filename)) {
return TAR;
}
}

0 comments on commit 74edb91

Please sign in to comment.