All files / lib/cli view.js

100% Statements 81/81
100% Branches 13/13
100% Functions 9/9
100% Lines 81/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
"use strict";
import _ from 'lodash';
import cli from 'bagofcli';
import fs from 'fs';
 
/**
 * Get a handler that calls Jenkins API to create a view with specific configuration.
 * Success view creation message will be logged when there's no error.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function create(cb) {
  return function (command, args) {
    const name = _.get(args, '[0]');
    const configFile = _.get(args, '[1]');
 
    function resultCb(result) {
      console.log(`View ${name} was created successfully`);
    }
    function jenkinsCb(jenkins) {
      const config = fs.readFileSync(configFile).toString();
      jenkins.createView(name, config, cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to update a view with specific configuration.
 * Success view update message will be logged when there's no error.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function update(cb) {
  return function (command, args) {
    const name = _.get(args, '[0]');
    const configFile = _.get(args, '[1]');
 
    function resultCb(result) {
      console.log(`View ${name} was updated successfully`);
    }
    function jenkinsCb(jenkins) {
      const config = fs.readFileSync(configFile).toString();
      jenkins.updateView(name, config, cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to fetch a view configuration.
 * Jenkins view config.xml will be logged when there's no error.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function fetchConfig(cb) {
  return function (command, args) {
    const name = _.get(args, '[0]');
 
    function resultCb(result) {
      console.log(result);
    }
    function jenkinsCb(jenkins) {
      jenkins.fetchViewConfig(name, cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
const exports = {
  create: create,
  update: update,
  fetchConfig: fetchConfig
};
 
export {
  exports as default
};