Class: KnifeGithubSearch::GithubSearch

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/github_search.rb

Instance Method Summary collapse

Instance Method Details

#github_search_repos(query, request = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/knife/github_search.rb', line 74

def github_search_repos(query, request = {})
  # once the new search function is available, we can use these params
  request['sort'] = 'stars'
  request['order'] = 'desc'
  request['response'] = 'json'
  request_arr = []
  request.sort.each { |elem|
    request_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
  }
  data = request_arr.join('&')
  params = {}
  params[:url]  = @github_url + "/api/" + @github_api_version + "/legacy/repos/search/" + query
  params[:action] = "GET"
  params[:token] = get_github_token
  params[:request_uri] = "?#{data}"
  connection.request(params) 
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/knife/github_search.rb', line 41

def run

  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Get the name_args from the command line
  query = name_args.join(' ')

  if query.nil? || query.empty? 
    Chef::Log.error("Please specify a search query")
    exit 1
  end 

  result = github_search_repos(query)

  if config[:link]
    columns = [ 'score,Score', 'name,Name', "url,URL" ]
  else
    columns = [ 'score,Score', 'name,Name', 'description,Description' ]
  end

  if result['repositories'].nil? || result['repositories'].empty?
    Chef::Log.error("No results when searching for: " + query)
  else
    items = []
    result['repositories'].each { |n| items << [ "#{n['name']}", n ] } 
    display_info(items, columns )
  end
end