summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2019-10-09 17:27:27 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2019-10-09 17:27:27 +0530
commita2311335336c857f2f72d0d7c844bc6e208d2d49 (patch)
treefb13b51e8ec0de13332f48868765a595e3753f17
parent68dfb74ac66ff8c4e78fda7d2d2d8dc1b9e5158b (diff)
downloadchef-a2311335336c857f2f72d0d7c844bc6e208d2d49.tar.gz
Enhance knife supermarket list & search
 - Increase items count 10 to 1000.    - Remove #sort from cookbook collection because data is already sorted.  - Add --sort-by & --owned-by options  - Update new_start count if total records is less so that it can prevent infinite loop. Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/knife/supermarket_list.rb31
-rw-r--r--lib/chef/knife/supermarket_search.rb4
2 files changed, 25 insertions, 10 deletions
diff --git a/lib/chef/knife/supermarket_list.rb b/lib/chef/knife/supermarket_list.rb
index 700d928af8..b0c87c3081 100644
--- a/lib/chef/knife/supermarket_list.rb
+++ b/lib/chef/knife/supermarket_list.rb
@@ -37,25 +37,40 @@ class Chef
default: "https://supermarket.chef.io",
proc: Proc.new { |supermarket| Chef::Config[:knife][:supermarket_site] = supermarket }
+ option :sort_by,
+ long: "--sort-by SORT",
+ description: "Use to sort the records",
+ in: %w{recently_updated recently_added most_downloaded most_followed}
+
+ option :owned_by,
+ short: "-o USER",
+ long: "--owned-by USER",
+ description: "Show cookbooks that are owned by the USER"
+
def run
+ url_params = []
+ url_params << "&order=#{config[:sort_by]}" if config[:sort_by]
+ url_params << "&user=#{config[:owned_by]}" if config[:owned_by]
+
+ query = url_params.join unless url_params.empty?
+ cookbooks_list = get_cookbook_list(query)
if config[:with_uri]
- cookbooks = {}
- get_cookbook_list.each { |k, v| cookbooks[k] = v["cookbook"] }
- ui.output(format_for_display(cookbooks))
+ ui.output(format_for_display(cookbooks_list))
else
- ui.msg(ui.list(get_cookbook_list.keys.sort, :columns_down))
+ ui.msg(ui.list(cookbooks_list.keys, :columns_down))
end
end
- def get_cookbook_list(items = 10, start = 0, cookbook_collection = {})
+ def get_cookbook_list(query, items = 1000, start = 0, cookbook_collection = {})
cookbooks_url = "#{config[:supermarket_site]}/api/v1/cookbooks?items=#{items}&start=#{start}"
+ cookbooks_url << query if query
cr = noauth_rest.get(cookbooks_url)
cr["items"].each do |cookbook|
- cookbook_collection[cookbook["cookbook_name"]] = cookbook
+ cookbook_collection[cookbook["cookbook_name"]] = cookbook["cookbook"]
end
- new_start = start + cr["items"].length
+ new_start = start + items
if new_start < cr["total"]
- get_cookbook_list(items, new_start, cookbook_collection)
+ get_cookbook_list(query, items, new_start, cookbook_collection)
else
cookbook_collection
end
diff --git a/lib/chef/knife/supermarket_search.rb b/lib/chef/knife/supermarket_search.rb
index 40614b4070..152effba28 100644
--- a/lib/chef/knife/supermarket_search.rb
+++ b/lib/chef/knife/supermarket_search.rb
@@ -35,13 +35,13 @@ class Chef
output(search_cookbook(name_args[0]))
end
- def search_cookbook(query, items = 10, start = 0, cookbook_collection = {})
+ def search_cookbook(query, items = 1000, start = 0, cookbook_collection = {})
cookbooks_url = "#{config[:supermarket_site]}/api/v1/search?q=#{query}&items=#{items}&start=#{start}"
cr = noauth_rest.get(cookbooks_url)
cr["items"].each do |cookbook|
cookbook_collection[cookbook["cookbook_name"]] = cookbook
end
- new_start = start + cr["items"].length
+ new_start = start + items
if new_start < cr["total"]
search_cookbook(query, items, new_start, cookbook_collection)
else