<commit_before>module Lightning::Commands
  protected
  desc '(list [--command=SHELL_COMMAND] [--bolt=BOLT] | create SHELL_COMMAND BOLT [function] | delete FUNCTION)',
    'Commands for managing functions. Defaults to listing them.'
  def function(argv)
    subcommand = argv.shift || 'list'
    subcommand = %w{create delete list}.find {|e| e[/^#{subcommand}/]} || subcommand
    function_subcommand(subcommand, argv) if subcommand_has_required_args(subcommand, argv)
  end

  def function_subcommand(subcommand, argv)
    case subcommand
    when 'list'    then list_function(argv)
    when 'create'  then create_function_and_bolt(argv)
    when 'delete'  then delete_function argv.shift
    else puts "Invalid subcommand '#{subcommand}'", command_usage
    end
  end

  def list_function(argv)
    args, options = parse_args argv
    functions = if options[:bolt]
      Lightning.functions.values.select {|e| e.bolt.name == options[:bolt] }.map {|e| e.name}
    elsif options[:command]
      Lightning.functions.values.select {|e| e.shell_command == options[:command] }.map {|e| e.name}
    else
      Lightning.functions.keys
    end
    puts functions.sort
  end

  def create_function_and_bolt(argv)
    bolt = config.unalias_bolt(argv[1])
    (config.bolts[bolt] || Lightning::Generator.run(bolt, :once=>bolt)) &&
      create_function(argv[0], bolt, :name=>argv[2])
  end

  def create_function(scmd, bolt, options={})
    options[:name] ||= Lightning.bolts[bolt].function_name(scmd) unless global_commands.include?(scmd)
    function = options[:name] || Lightning.bolts[bolt].function_name(scmd)

    if find_function(config.bolts[bolt]['functions'], 'shell_command', scmd)
      puts("Function '#{function}' already exists")
    else
      if_bolt_found(bolt) do |bolt|<commit_msg>Fixed bug preventing creation of new functions.<commit_after>module Lightning::Commands
  protected
  desc '(list [--command=SHELL_COMMAND] [--bolt=BOLT] | create SHELL_COMMAND BOLT [function] | delete FUNCTION)',
    'Commands for managing functions. Defaults to listing them.'
  def function(argv)
    subcommand = argv.shift || 'list'
    subcommand = %w{create delete list}.find {|e| e[/^#{subcommand}/]} || subcommand
    function_subcommand(subcommand, argv) if subcommand_has_required_args(subcommand, argv)
  end

  def function_subcommand(subcommand, argv)
    case subcommand
    when 'list'    then list_function(argv)
    when 'create'  then create_function_and_bolt(argv)
    when 'delete'  then delete_function argv.shift
    else puts "Invalid subcommand '#{subcommand}'", command_usage
    end
  end

  def list_function(argv)
    args, options = parse_args argv
    functions = if options[:bolt]
      Lightning.functions.values.select {|e| e.bolt.name == options[:bolt] }.map {|e| e.name}
    elsif options[:command]
      Lightning.functions.values.select {|e| e.shell_command == options[:command] }.map {|e| e.name}
    else
      Lightning.functions.keys
    end
    puts functions.sort
  end

  def create_function_and_bolt(argv)
    bolt = config.unalias_bolt(argv[1])
    (config.bolts[bolt] || Lightning::Generator.run(bolt, :once=>bolt)) &&
      create_function(argv[0], bolt, :name=>argv[2])
  end

  def create_function(scmd, bolt, options={})
    options[:name] ||= Lightning.bolts[bolt].function_name(scmd) unless config.global_commands.include?(scmd)
    function = options[:name] || Lightning.bolts[bolt].function_name(scmd)

    if find_function(config.bolts[bolt]['functions'], 'shell_command', scmd)
      puts("Function '#{function}' already exists")
    else
      if_bolt_found(bolt) do |bolt|
{'fraction_non_alphanumeric': 0.1297267457907811, 'fraction_numerical': 0.0016560861164780568, 'mean_word_length': 4.306002928257687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1989361', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1145, 'n_words': 265}