<commit_before># encoding: UTF-8
#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2014-2015 <NAME>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

begin
  require 'ridley'
rescue LoadError => e
  raise "Missing gem or lib #{e}"
end

module Comrad
  # uploads / removes objects that changed from the chef server
  class Chef < Application
    def initialize(config, changes)
      @config = config
      @changes = changes
      @slack = Comrad::Slack.new(config)
    end

    def action_string(action, trailing_text)
      string = @config['flags']['dryrun'] ? " - I would #{action} " : " - #{action.capitalize.chomp('e')}ing "
      string + trailing_text
    end

    # a really horrible method to build knife commands based on item / action / item type (cookbook/role/environment/data_bag)
    def build_knife_cmd(type, action, item1, item2 = nil)
      if type == 'data_bags'
        action == 'delete' ? "knife data bag delete #{item1} #{item2}" : "knife data bag from file #{item1} data_bags/#{item1}/#{item2}"
      elsif action == 'delete'
        "knife #{type.chomp('s')} delete #{item1} #{item2}"
      elsif type == 'cookbooks'
        "knife cookbook #{action == 'update' ? 'upload' : 'delete'} #{item1}"
      else
        "knife #{type.chomp('s')} from file #{type}s/#{item1}"
      end
    end

    # Perform actual actions on chef server and log to slack
    def take_actions
      @changes.each_pair do |type, name|
        next if name.empty?
        case
        when type.to_s.match(/^['environments|roles']/)
          name.each_pair do |item, action|
            @slack.slack_put(action_string(action, "#{item}"))
            @slack.slack_put("   - #{build_knife_cmd(type, action, item)}")
          end
        when type.to_s.match(/^['cookbooks']/)
          name.each_pair do |item, action|
            @slack.slack_put(action_string(action, "#{item}"))
            @slack.slack_put("   - #{build_knife_cmd(type, action, item)}")
          end
        when type.to_s.match(/^['data_bags']/)
          name.each_pair do |bag, item|
            item.each_pair do |bag_item_name, action|
              @slack.slack_put(action_string(action, "#{bag}::#{bag_item_name} data bag item"))
              @slack.slack_put("   - #{build_knife_cmd(type, action, bag, bag_item_name)}")
            end
          end
        end
      end
    end

    # called by application to perform actions
    def run
      @slack.slack_put("Comrad action for chef repo build # #{ENV['BUILD_NUM'] || 5}:")
      take_actions
    end
  end # Chef class
end # Comrad Module
<commit_msg>Add a method for executing knife and remove regex's that weren't necessary<commit_after># encoding: UTF-8
#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2014-2015 <NAME>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

begin
  require 'ridley'
rescue LoadError => e
  raise "Missing gem or lib #{e}"
end

module Comrad
  # uploads / removes objects that changed from the chef server
  class Chef < Application
    def initialize(config, changes)
      @config = config
      @changes = changes
      @slack = Comrad::Slack.new(config)
    end

    def action_string(action, trailing_text)
      string = @config['flags']['dryrun'] ? " - I would #{action} " : " - #{action.capitalize.chomp('e')}ing "
      string + trailing_text
    end

    # a really horrible method to build knife commands based on item / action / item type (cookbook/role/environment/data_bag)
    def build_knife_cmd(type, action, item1, item2 = nil)
      if type == 'data_bags'
        action == 'delete' ? "knife data bag delete #{item1} #{item2}" : "knife data bag from file #{item1} data_bags/#{item1}/#{item2}"
      elsif action == 'delete'
        "knife #{type.chomp('s')} delete #{item1} #{item2}"
      elsif type == 'cookbooks'
        "knife cookbook #{action == 'update' ? 'upload' : 'delete'} #{item1}"
      else
        "knife #{type.chomp('s')} from file #{type}s/#{item1}"
      end
    end

    # run the provided knife command
    def excute_knife_cmd(cmd)
      if @config['flags']['dryrun']
       @slack.slack_put("    - I would be running #{cmd}")
     else
        @slack.slack_put('    - Non-dry mode is not implemented.  Doing nothing')
      end
    end

    # Perform actual actions on chef server and log to slack
    def take_actions
      @changes.each_pair do |type, name|
        next if name.empty?
        case
        when type.match(/^['environments|roles']/)
          name.each_pair do |item, action|
            @slack.slack_put(action_string(action, "#{item}"))
            excute_knife_cmd(build_knife_cmd(type, action, item))
          end
        when type == 'cookbooks'
          name.each_pair do |item, action|
            @slack.slack_put(action_string(action, "#{item}"))
            excute_knife_cmd(build_knife_cmd(type, action, item))
          end
        when type == 'data_bags'
          name.each_pair do |bag, item|
            item.each_pair do |bag_item_name, action|
              @slack.slack_put(action_string(action, "#{bag}::#{bag_item_name} data bag item"))
              excute_knife_cmd(build_knife_cmd(type, action, bag, bag_item_name))
            end
          end
        end
      end
    end

    # called by application to perform actions
    def run
      @slack.slack_put("Comrad action for chef repo build # #{ENV['BUILD_NUM'] || 5}:")
      take_actions
    end
  end # Chef class
end # Comrad Module

{'fraction_non_alphanumeric': 0.1192478214340315, 'fraction_numerical': 0.00825561840697141, 'mean_word_length': 2.8168028004667445, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3247113', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1988, 'n_words': 642}