<commit_before>/**
 *
 */
package com.github.maybeec.oomph.task.cli.core.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import com.github.maybeec.oomph.task.cli.core.CommandLineUtil;
import com.github.maybeec.oomph.task.cli.core.SetupTaskLogger;
import com.github.maybeec.oomph.task.cli.core.exceptions.CommandLineUtilException;

/**
 * @author sholzer
 *
 */
public class CommandLineUtilImpl implements CommandLineUtil {

  static CommandLineUtil instance;

  public static CommandLineUtil getInstance() {

    if (instance == null) {
      instance = new CommandLineUtilImpl();
    }
    return instance;
  }

  /**
  *
  */
  CommandLineUtilImpl() {
    SetupTaskLogger.getLogger().logInfo("Operating on " + OsUtil.getOsName());
  }

  /*
   * (non-Javadoc)
   *
   * @see cmd.core.CommandLineUtil#execute(java.lang.String)
   */
  public void execute(String dir, String cmd, Iterable<String> args) throws CommandLineUtilException {

    if (dir == null || !new File(dir).exists()) {
      throw new CommandLineUtilException("Please provide a valid working directory. " + dir + " is none");
    }

    if (!new File(dir).isDirectory()) {
      throw new CommandLineUtilException(dir + " is not a directory");
    }

    SetupTaskLogger.getLogger().logInfo("Execute: " + cmd + " " + stringListToLine(args) + " @ " + dir);

    List<String> commands = new LinkedList<String>();

    if (OsUtil.isWindows()) {
      commands.add("CMD");
      commands.add("/C");
    } else {
      // commands.add("sh");
      // commands.add("-c");
    }

    commands.add(cmd);

    for (String s : args) {
      commands.add(s);
    }

    try {
      ProcessBuilder pb = new ProcessBuilder(commands);
      pb.redirectErrorStream(true);
      pb.directory(new File(dir));
      Process process = pb.start();
      BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

      String line;
      while ((line = inStreamReader.readLine()) != null) {
        SetupTaskLogger.getLogger().log(line);
      }
    } catch (Exception ex) {
      throw new CommandLineUtilException(ex.getMessage(), ex);
    }

  }

  public static String stringListToLine(Iterable<String> list) {

    String result = "";
    for (String s : list) {
      if (result.equals("")) {
        result = s;
      } else {
        result = result + " " + s;
      }
    }
    return result;
  }

}<commit_msg>Fixed #11 by adding a 'if null then initialize' block before args is used<commit_after>/**
 *
 */
package com.github.maybeec.oomph.task.cli.core.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import com.github.maybeec.oomph.task.cli.core.CommandLineUtil;
import com.github.maybeec.oomph.task.cli.core.SetupTaskLogger;
import com.github.maybeec.oomph.task.cli.core.exceptions.CommandLineUtilException;

/**
 * @author sholzer
 *
 */
public class CommandLineUtilImpl implements CommandLineUtil {

  static CommandLineUtil instance;

  public static CommandLineUtil getInstance() {

    if (instance == null) {
      instance = new CommandLineUtilImpl();
    }
    return instance;
  }

  /**
  *
  */
  CommandLineUtilImpl() {
    SetupTaskLogger.getLogger().logInfo("Operating on " + OsUtil.getOsName());
  }

  /*
   * (non-Javadoc)
   *
   * @see cmd.core.CommandLineUtil#execute(java.lang.String)
   */
  public void execute(String dir, String cmd, Iterable<String> args) throws CommandLineUtilException {

    if (dir == null || !new File(dir).exists()) {
      throw new CommandLineUtilException("Please provide a valid working directory. " + dir + " is none");
    }

    if (!new File(dir).isDirectory()) {
      throw new CommandLineUtilException(dir + " is not a directory");
    }
    // Fix for Github Issue #11
    if (args == null) {
      args = new LinkedList<String>();
    }

    SetupTaskLogger.getLogger().logInfo("Execute: " + cmd + " " + stringListToLine(args) + " @ " + dir);

    List<String> commands = new LinkedList<String>();

    if (OsUtil.isWindows()) {
      commands.add("CMD");
      commands.add("/C");
    } else {
      // commands.add("sh");
      // commands.add("-c");
    }

    commands.add(cmd);

    for (String s : args) {
      commands.add(s);
    }

    try {
      ProcessBuilder pb = new ProcessBuilder(commands);
      pb.redirectErrorStream(true);
      pb.directory(new File(dir));
      Process process = pb.start();
      BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

      String line;
      while ((line = inStreamReader.readLine()) != null) {
        SetupTaskLogger.getLogger().log(line);
      }
    } catch (Exception ex) {
      throw new CommandLineUtilException(ex.getMessage(), ex);
    }

  }

  /**
   *
   * @param list not null
   * @return String containing space separated list entries. If list were empty the empty String is retured.
   */
  public static String stringListToLine(Iterable<String> list) {

    String result = "";
    for (String s : list) {
      if (result.equals("")) {
        result = s;
      } else {
        result = result + " " + s;
      }
    }
    return result;
  }

}
{'fraction_non_alphanumeric': 0.1368835552222014, 'fraction_numerical': 0.0007500468779298706, 'mean_word_length': 2.9657992565055764, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3231980', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1619, 'n_words': 356}