<commit_before>module Vagrant
  module Provisioners
    class ChefClient < Chef
      class Config < Chef::Config
        attr_accessor :knife_config
        def knife_config; @knife_config || "#{ENV['HOME']}/.chef/knife.rb"; end
      end

      def cleanup_chef_server(chef_server_url, node_name)
        chef_api = ::Chef::REST.new(chef_server_url)
        %w(node client).each do |resource|
          env[:ui].info "Removing Chef #{resource} \"#{node_name}\"..."
          begin
            chef_api.delete_rest("#{resource}s/#{node_name}")
          rescue Exception => e
            env[:ui].warn "Could not remove #{resource} #{node_name}: #{e.message}"
          end
        end
      end

      def cleanup
        require 'pry-debugger';binding.pry
        conf = env[:vm].config.vm

        chef_provisioner = conf.provisioners.find { |p| p.provisioner.name == "Vagrant::Provisioners::ChefClient" }
        chef_server_url = chef_provisioner.config.chef_server_url

        # Gets client_key_path from knife.rb unless set on the Vagrantfile
        ::Chef::Config.from_file(config.knife_config) unless chef_provisioner.config.respond_to? :client_key_path
        
        # If a node_name is given to the chef-client provisioner, use it
        victim = chef_provisioner.config.node_name

        # If no node_name, fall back to host_name or box
        if not victim then
          env[:ui].info "No chef.node_name set, falling back to vm.host_name or vm.box."
          victim ||= conf.host_name || conf.box
        end
        
        cleanup_chef_server(chef_server_url, victim)
      end
    end
  end
end
<commit_msg>Refactored for readability and simplicity<commit_after>module Vagrant
  module Provisioners
    class ChefClient < Chef
      class Config < Chef::Config
        attr_accessor :knife_config
        def knife_config; @knife_config || "#{ENV['HOME']}/.chef/knife.rb"; end
      end

      attr_writer :vm_config, :chef_api, :victim, :chef_provisioner
      def vm_config; @vm_config ||= env[:vm].config.vm;                                                 end
      def chef_api ; @chef_api  ||= ::Chef::REST.new(chef_provisioner.chef_server_url);                 end
      def victim   ; @victim    ||= chef_provisioner.node_name || vm_config.host_name || vm_config.box; end

      def chef_provisioner
        @chef_provisioner ||= vm_config.provisioners.find do |p|
          p.provisioner == Vagrant::Provisioners::ChefClient
        end.config
      end

      def delete_resource(resource)
        env[:ui].info "Removing Chef #{resource} \"#{victim}\"..."
        begin
          chef_api.delete_rest("#{resource}s/#{victim}")
        rescue Exception => e
          env[:ui].warn "Could not remove #{resource} #{victim}: #{e.message}"
        end
      end

      def cleanup
        ::Chef::Config.from_file(chef_provisioner.knife_config)
        %w(node client).each { |resource| delete_resource(resource) }
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10879785090664876, 'fraction_numerical': 0.0, 'mean_word_length': 2.4399538106235568, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3285273', 'n_tokens_mistral': 945, 'n_tokens_neox': 933, 'n_words': 205}