From 74edb91f6c88a4569ea44a13a4058d9a0522b1c3 Mon Sep 17 00:00:00 2001 From: Court Ewing Date: Wed, 3 Feb 2016 09:07:55 -0500 Subject: [PATCH] Refactor archive type checks to be more DRY This should be an in-place refactor without any additional behaviors or side effects. Fixes #6147 --- .../plugin/__tests__/type_from_filename.js | 27 +++++++++++++++++++ src/cli/plugin/downloaders/file.js | 12 ++------- src/cli/plugin/downloaders/http.js | 13 +++------ src/cli/plugin/plugin_extractor.js | 5 ++-- src/cli/plugin/type_from_filename.js | 11 ++++++++ 5 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 src/cli/plugin/__tests__/type_from_filename.js create mode 100644 src/cli/plugin/type_from_filename.js diff --git a/src/cli/plugin/__tests__/type_from_filename.js b/src/cli/plugin/__tests__/type_from_filename.js new file mode 100644 index 000000000000..84ac09991af8 --- /dev/null +++ b/src/cli/plugin/__tests__/type_from_filename.js @@ -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); + }); + }); +}); diff --git a/src/cli/plugin/downloaders/file.js b/src/cli/plugin/downloaders/file.js index 22911b28e452..bfd0e80bd324 100644 --- a/src/cli/plugin/downloaders/file.js +++ b/src/cli/plugin/downloaders/file.js @@ -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 { @@ -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 */ @@ -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); diff --git a/src/cli/plugin/downloaders/http.js b/src/cli/plugin/downloaders/http.js index f4aecd53b2f6..3750cd9f71a3 100644 --- a/src/cli/plugin/downloaders/http.js +++ b/src/cli/plugin/downloaders/http.js @@ -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. @@ -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); } } diff --git a/src/cli/plugin/plugin_extractor.js b/src/cli/plugin/plugin_extractor.js index a8e3d740aa26..981f9db57eef 100644 --- a/src/cli/plugin/plugin_extractor.js +++ b/src/cli/plugin/plugin_extractor.js @@ -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: diff --git a/src/cli/plugin/type_from_filename.js b/src/cli/plugin/type_from_filename.js new file mode 100644 index 000000000000..b6e0e4617f1b --- /dev/null +++ b/src/cli/plugin/type_from_filename.js @@ -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; + } +}