'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var readKey = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(keyPath) {
    var fileBuffer;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (keyPath) {
              _context.next = 2;
              break;
            }

            return _context.abrupt('return', '');

          case 2:
            _context.prev = 2;
            _context.next = 5;
            return (_fsExtra || _load_fsExtra()).default.readFile(keyPath);

          case 5:
            fileBuffer = _context.sent;
            return _context.abrupt('return', fileBuffer);

          case 9:
            _context.prev = 9;
            _context.t0 = _context['catch'](2);
            return _context.abrupt('return', '');

          case 12:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[2, 9]]);
  }));

  return function readKey(_x) {
    return _ref.apply(this, arguments);
  };
}();

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _utils;

function _load_utils() {
  return _utils = require('../../../utils');
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../../logger/logger'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(keyPath) {
    var sshFile;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (!keyPath) {
              _context2.next = 2;
              break;
            }

            return _context2.abrupt('return', readKey(keyPath));

          case 2:
            _context2.next = 4;
            return (0, (_utils || _load_utils()).identityFile)();

          case 4:
            sshFile = _context2.sent;

            (_logger || _load_logger()).default.debug(`ssh, reading ssh key at ${sshFile}`);
            return _context2.abrupt('return', readKey(sshFile));

          case 7:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  function keyGetter(_x2) {
    return _ref2.apply(this, arguments);
  }

  return keyGetter;
}();