<commit_before>const yaml = require('js-yaml');
const utils = require('./utils');
const _ = require('underscore');
const docker = require('./docker');

function generate(data) {
  return yaml.dump(data);
}

function isRunning(path, callback) {
  const command = `cd ${path}; docker-compose ps -q`;
  utils.cmd(command, (err, data) => {
    if (err) callback(err);
    else if (data === '') callback(null, false);
    else callback(null, true);
  });
}

function getContainerID(path, service, callback) {
  return utils.cmd(`cd ${path}; docker-compose ps -q ${service}`, (err, data) => {
    if (err)
  { callback(err, data); }
    else {
      let containerID = data;
      // Replace newlines
      containerID = containerID.replace(/(\r\n|\n|\r)/gm, '');
      callback(null, containerID);
    }
  });
}

// Call docker-compose up in path and returns the process pid
function up(path, callback, dataNotify) {
  const pid = utils.cmd(`cd ${path}; docker-compose  up -d`, callback);
  // TODO Check if callback is defined
  utils.docker_logs(pid, dataNotify);
  return pid;
}
// Call docker-compose down in path and returns the process pid
function down(path, callback, dataNotify) {
  const pid = utils.cmd(`cd ${path}; docker-compose down`, callback);
  utils.docker_logs(pid, dataNotify);

  return pid;
}

function cp(path, service, src, dst, callback) {
  getContainerID(path, service, (err, data) => {
  // No err : get containerID = data
    if (!err) {
      const containerID = data;
      // Cal docker exec
      return docker.cp(containerID, src, dst, callback);
    }
    else {
      callback(err);
      return null;
    }
  });
}

function exec(path, service, command, callback, paramsInput, notifyCallback) {
  const paramsProto = {
    detached: false,
  };
  let pid;
  // Get the params
  const params = _.extend({}, paramsProto, paramsInput);
  if (!params.detached)
    {
    pid = utils.cmd(`cd ${path}; docker-compose exec ${service} ${command}`, callback);
    utils.docker_logs_stdout(pid, notifyCallback);
  }
  // Workaround for detached mode docker-compose
  else {
    getContainerID(path, service, (err, data) => {
      // No err : get containerID = data
      if (!err) {
        const containerID = data;
      // Cal docker exec
        pid = docker.exec(containerID, command, callback, params);
        utils.docker_logs_stdout(pid, notifyCallback);<commit_msg>Update but of callback docker-compose<commit_after>const yaml = require('js-yaml');
const utils = require('./utils');
const _ = require('underscore');
const docker = require('./docker');

function generate(data) {
  return yaml.dump(data);
}

function isRunning(path, callback) {
  const command = `cd ${path}; docker-compose ps -q`;
  utils.cmd(command, (err, data) => {
    if (err) callback(err);
    else if (data === '') callback(null, false);
    else callback(null, true);
  });
}

function getContainerID(path, service, callback) {
  return utils.cmd(`cd ${path}; docker-compose ps -q ${service}`, (err, data) => {
    if (err)
  { callback(err, data); }
    else {
      let containerID = data;
      // Replace newlines
      containerID = containerID.replace(/(\r\n|\n|\r)/gm, '');
      callback(null, containerID);
    }
  });
}

// Call docker-compose up in path and returns the process pid
function up(path, callback, dataNotify) {
  const pid = utils.cmd(`cd ${path}; docker-compose  up -d`, callback);
  // TODO Check if callback is defined
  utils.docker_logs(pid, dataNotify);
  return pid;
}
// Call docker-compose down in path and returns the process pid
function down(path, callback, dataNotify) {
  const pid = utils.cmd(`cd ${path}; docker-compose down`, callback);
  utils.docker_logs(pid, dataNotify);

  return pid;
}

function cp(path, service, src, dst, callback) {
  getContainerID(path, service, (err, data) => {
  // No err : get containerID = data
    if (!err) {
      const containerID = data;
      // Cal docker exec
      return docker.cp(containerID, src, dst, callback);
    }
    else {
      callback(err);
      return null;
    }
  });
}

function exec(path, service, command, callback, paramsInput, notifyCallback) {
  // Used for store possible errors
  let dataLine = '';
  const paramsProto = {
    detached: false,
  };
  let pid;
  // Get the params
  const params = _.extend({}, paramsProto, paramsInput);
  if (!params.detached)
    {
    pid = utils.cmd(`cd ${path}; docker-compose exec ${service} ${command}`, (err, data) => {
      if (err)
      {
        // Override old existent error
        let stringErr = `Error occurred: ${err.message}.`;
        if (dataLine !== '') {
          stringErr += `DOCKER GIVES: ${dataLine}`;
        }
        callback(new Error(stringErr));
      }
      else callback(null, data);
    });
    // Capture possible errors
    utils.docker_logs_stdout(pid, (dl) => {
      dataLine += dl;
    });
    utils.docker_logs_stdout(pid, notifyCallback);
  }
  // Workaround for detached mode docker-compose
  else {
    getContainerID(path, service, (err, data) => {
      // No err : get containerID = data
      if (!err) {
        const containerID = data;
      // Cal docker exec
        pid = docker.exec(containerID, command, callback, params);
        utils.docker_logs_stdout(pid, notifyCallback);
{'fraction_non_alphanumeric': 0.1465305350727926, 'fraction_numerical': 0.0, 'mean_word_length': 3.088098918083462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721474', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1628, 'n_words': 476}