lib/chef/knife/github_base.rb in knife-github-0.0.7 vs lib/chef/knife/github_base.rb in knife-github-0.0.8

- old
+ new

@@ -27,10 +27,11 @@ includer.class_eval do deps do require 'chef/mixin/shell_out' require 'mixlib/versioning' + require 'chef/knife/github_config' end option :github_url, :long => "--github_url URL", :description => "URL of the github enterprise appliance" @@ -114,11 +115,21 @@ Chef::Log.debug("github_ssl_mode : " + @github_ssl_verify_mode.to_s) end def locate_config_value(key) key = key.to_sym - config[key] || Chef::Config[:knife][key] + central_config = "/etc/githubrc.rb" + if File.exists?(central_config) + begin + Github::Config.from_file(central_config) + rescue + Chef::Log.error("Something is wrong within your central config file: #{central_config}") + Chef::Log.error("You will need to fix or remove this file to continue!") + exit 1 + end + end + config[key] || Chef::Config[:knife][key] || Github::Config[key] end def get_repo_clone_link link = locate_config_value('github_link') repo_link = case link @@ -187,37 +198,39 @@ result = send_request(url, params) Time.parse(result['updated_at']) end def get_repos_github(org) + # Get all repo's from cache file + # Get all repo's for the org from github arr = [] page = 1 url = @github_url + "/api/" + @github_api_version + "/orgs/" + org + "/repos" while true params = {'response' => 'json', 'page' => page } result = send_request(url, params) break if result.nil? || result.count < 1 - result.each { |key| - if key['tags_url'] - tags = get_tags(key) - key['tags'] = tags unless tags.nil? || tags.empty? - key['latest_tag'] = get_latest_tag(tags) - arr << key - else - arr << key - end - } + result.each { |key| arr << key } page = page + 1 end + # WE WILL REMOVE THIS, GETTING TAGS FOR EVERY REPO IS VERY SLOW! + # if key['tags_url'] + # tags = get_tags(key) + # key['tags'] = tags unless tags.nil? || tags.empty? + # key['latest_tag'] = get_latest_tag(tags) + # arr << key + # else + # arr << key + # end + #} arr end def get_tags(repo) params = {'response' => 'json'} - tags = send_request(repo['tags_url'], params) - tags + send_request(repo['tags_url'], params) end def get_latest_tag(tags) return "" if tags.nil? || tags.empty? tags_arr =[] @@ -289,45 +302,53 @@ end return true end def add_tag(version) - cpath = cookbook_path_valid?(@cookbook_name, false) - Dir.chdir(cpath) + cookbook_path = get_cookbook_path(@cookbook_name) + Dir.chdir(cookbook_path) Chef::Log.debug "Adding tag" output = `git tag -a "#{version}" -m "Added tag #{version}" 2>&1` if $?.exitstatus != 0 Chef::Log.error("Could not add tag for: #{@cookbook_name}") FileUtils.remove_entry(@github_tmp) exit 1 end end - def cookbook_path_valid?(cookbook_name, check_exists) + def get_cookbook_path(cookbook_name) cookbook_path = config[:cookbook_path] || Chef::Config[:cookbook_path] if cookbook_path.nil? || cookbook_path.empty? Chef::Log.error("Please specify a cookbook path") exit 1 end + cookbook_path = [ cookbook_path ] if cookbook_path.is_a?(String) + unless File.exists?(cookbook_path.first) && File.directory?(cookbook_path.first) Chef::Log.error("Cannot find the directory: #{cookbook_path.first}") exit 1 end cookbook_path = File.join(cookbook_path.first,cookbook_name) - if check_exists - if File.exists?(cookbook_path) - ui.info("Processing [S] #{cookbook_name}") - Chef::Log.info("Path to #{cookbook_path} already exists, skipping.") - return nil - end - else - if ! File.exists?(cookbook_path) - return nil - end - end - return cookbook_path + end + + # Get the version number in the git version of the cookbook + # @param version [String] Version + def get_cookbook_version() + version = nil + cookbook_path = get_cookbook_path(@cookbook_name) + File.foreach("#{cookbook_path}/metadata.rb") do |line| + if line =~ /version.*['"](.*)['"]/i + version = $1 + break + end + end + if version.nil? + Chef::Log.error("Cannot get the version for cookbook #{@cookbook_name}") + exit 1 + end + version end end end end