<commit_before># Description:
#   Playing with Drush integration.
#
# Commands:
#   hubot drush cc <site alias> - Clears the cache for a given site alias.

util = require("util")
spawn = require("child_process").spawn

drush_interface = ->
  site_aliases = []
  allowed_commands = ["sa", "cc", "pml", "unif", "pmi", "ws", "vget", "rq"]

  # Our allowed commands
  commands =
    drush_sa: (msg) ->
      msg.send "Aliases we have in memory, if this list is empty or is unexpected update aliases."
      msg.send site_aliases.join("\n")

  # parsing the user input after "drush "
  parse_command = (user_command) ->
    args = user_command.split(' ')
    command = "drush_" + args.shift()
    if typeof commands[command] is "function"
      return (
        cmnd: command
        extra_args: args
      )
      command
    else
      `undefined`

  # Begin our public facing methods
  update_aliases: (msg) ->
    output = ''
    raw_aliases = ''
    fetch_aliases = spawn("drush", ["sa"])
    fetch_aliases.stdout.on "data", (data) ->
      raw_aliases += data
    fetch_aliases.stderr.on "data", (data) ->
      output = "Update experienced an error: " + data
    fetch_aliases.on "exit", (code) ->
      if code is 0
        site_aliases = raw_aliases.split('\n')
        output = "drush aliases updated"
      unless msg is `undefined`
        msg.send output

  # The main method, fire this when we recieve a "drush " command.
  execute: (msg) ->
    msg.send "we got args: " + msg.match[1]
    command = parse_command(msg.match[1])
    unless command is `undefined`
      commands[command.cmnd](msg)
    else
      msg.send "'drush " + msg.match[1] + "' is and invalid command. Please try again."

drush = drush_interface()
drush.update_aliases()

module.exports = (robot) ->
  robot.respond /drush (.*)$/i, (msg) ->
    msg.send "drush fired!"
    drush.execute(msg)

#module.exports = (robot) ->
#  robot.respond /drush sa$/i, (msg) ->
#    #    if not process.env.DRUSH_UID?
#    # msg.send "DRUSH_UID is not set. Cannot complete this action."
#    #  return
#
#    sitelist = 'Site Aliases Available: \n'
#    sitealias = spawn("drush", ["sa"])
#    sitealias.stdout.on "data", (data) ->
#      sitelist = sitelist + data
#
#    sitealias.on "exit", (code) ->
#      msg.send sitelist
#      msg.send 'sa command complete.'
#
#  robot.respond /drush cc (.*)$/i, (msg) ->
#    ccout = ''
#    clearcache = spawn("drush", [msg.match[1], "cc", "all"])<commit_msg>furhter work on the parser, beginning of extra arg handling<commit_after># Description:
#   Playing with Drush integration.
#
# Commands:
#   hubot drush cc <site alias> - Clears the cache for a given site alias.

util = require("util")
spawn = require("child_process").spawn

drush_interface = ->
  site_aliases = []
  allowed_commands = ["sa", "cc", "pml", "unif", "pmi", "ws", "vget", "rq"]

  # helper method to propigate the site aliases in memory
  update_aliases = (msg) ->
    output = ''
    raw_aliases = ''
    fetch_aliases = spawn("drush", ["sa"])
    fetch_aliases.stdout.on "data", (data) ->
      raw_aliases += data
    fetch_aliases.stderr.on "data", (data) ->
      output = "Update experienced an error: " + data
    fetch_aliases.on "exit", (code) ->
      if code is 0
        site_aliases = raw_aliases.split('\n')
        output = "Alias update complete."
      unless msg is `undefined`
        msg.send output

  # run the update script upon construction
  update_aliases()

  # Our allowed commands
  commands =
    # site aliases, the only command that does not need an alias prefix
    drush_sa: (msg, args) ->
      #msg.send "extra arguments: " + args.join(' ')
      if args.indexOf('--update-aliases') is -1
        msg.send "If this list is empty or has unexpected results update aliases ('drush sa --update-aliases')."
        msg.send "Aliases we have in memory:\n" + site_aliases.join("\n")
      else
        msg.send "Updating aliases..."
        update_aliases(msg)

  # parsing the user input after "drush "
  parse_command = (user_command) ->
    args = user_command.split(' ')
    command = "drush_" + args.shift()
    if typeof commands[command] is "function"
      return (
        cmnd: command
        extra_args: args
      )
      command
    else
      `undefined`

  # BEGIN public facing methods

  # The main method, fire this when we recieve a "drush " command.
  execute: (msg) ->
    #msg.send "we got args: " + msg.match[1]
    command = parse_command(msg.match[1])
    unless command is `undefined`
      commands[command.cmnd](msg, command.extra_args)
    else
      msg.send "'drush " + msg.match[1] + "' is and invalid command. Please try again."

# Instanciate the drush interface
drush = drush_interface()

# Hook in with hobot
module.exports = (robot) ->
  robot.respond /drush (.*)$/i, (msg) ->
    msg.send "drush fired!"
    drush.execute(msg)

#module.exports = (robot) ->
#  robot.respond /drush sa$/i, (msg) ->
#    #    if not process.env.DRUSH_UID?
#    # msg.send "DRUSH_UID is not set. Cannot complete this action."
#    #  return
#
#    sitelist = 'Site Aliases Available: \n'
#    sitealias = spawn("drush", ["sa"])
#    sitealias.stdout.on "data", (data) ->
#      sitelist = sitelist + data
#
#    sitealias.on "exit", (code) ->
#      msg.send sitelist
#      msg.send 'sa command complete.'
#
#  robot.respond /drush cc (.*)$/i, (msg) ->
#    ccout = ''
#    clearcache = spawn("drush", [msg.match[1], "cc", "all"])
{'fraction_non_alphanumeric': 0.14421669106881405, 'fraction_numerical': 0.002013177159590044, 'mean_word_length': 2.776779543883898, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '7357972', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1810, 'n_words': 527}