summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilan Šťastný <mistastn@cisco.com>2023-05-02 21:46:14 +0200
committerGitHub <noreply@github.com>2023-05-02 12:46:14 -0700
commit0a7c1a55bb316479bc57768eac243becfd1d2eec (patch)
tree53f4dbdd04540f822a606d6c1942d71ba8ea6160
parent4a54274d9c21bb9500989346d0d0ee22ba6076b3 (diff)
downloadchef-0a7c1a55bb316479bc57768eac243becfd1d2eec.tar.gz
Fix output of cookbook list/show (#13654)
This PR turns the output from the following: ```json $ knife cookbook list -a -F json [ "7-zip 1.0.2", "api_docs 0.1.0", "apt 1.1.1", "ark 4.0.0 3.1.1 0.9.0 0.3.0", "audit-cis 0.4.0", "authenticator 0.1.5 0.1.4", "aws-static-ip-ha 0.1.0", "aws-tools 0.0.5" ] ``` to this ```json $ knife cookbook list -a -F json { "7-zip": [ "1.0.2" ], "api_docs": [ "0.1.0" ], "apt": [ "1.1.1" ], "ark": [ "4.0.0", "3.1.1", "0.9.0", "0.3.0" ], "audit-cis": [ "0.4.0" ], "authenticator": [ "0.1.5", "0.1.4" ], "aws-static-ip-ha": [ "0.1.0" ], "aws-tools": [ "0.0.5" ] } ``` This has a potential of breaking peoples script if they adjusted to the broken format, that is why this is marked as a breaking change. This brings the output of the following in line with other listings coming out of chef ### Examples of the affected outputs: #### knife cookbook show summary(default) ``` $ knife cookbook show ark ark: 4.0.0 3.1.1 0.9.0 0.3.0 ``` text ``` $ knife cookbook show ark -F text ark: 4.0.0 3.1.1 0.9.0 0.3.0 ``` json ```json $ knife cookbook show ark -F json { "ark": [ "4.0.0", "3.1.1", "0.9.0", "0.3.0" ] } ``` yaml ```yaml $ knife cookbook show ark -F yaml --- ark: - 4.0.0 - 3.1.1 - 0.9.0 - 0.3.0 ``` pp ``` $ knife cookbook show ark -F pp {"ark"=>["4.0.0", "3.1.1", "0.9.0", "0.3.0"]} ``` #### knife cookbook list summary(default) ``` $ knife cookbook list -a 7-zip: 1.0.2 api_docs: 0.1.0 apt: 1.1.1 ark: 4.0.0 3.1.1 0.9.0 0.3.0 audit-cis: 0.4.0 auditbeat: 0.0.42 authenticator: 0.1.5 0.1.4 aws-static-ip-ha: 0.1.0 aws-tools: 0.0.5 ``` text ``` $ knife cookbook list -a -F text 7-zip: 1.0.2 api_docs: 0.1.0 apt: 1.1.1 ark: 4.0.0 3.1.1 0.9.0 0.3.0 audit-cis: 0.4.0 auditbeat: 0.0.42 authenticator: 0.1.5 0.1.4 aws-static-ip-ha: 0.1.0 aws-tools: 0.0.5 ``` json ```json $ knife cookbook list -a -F json { "7-zip": [ "1.0.2" ], "api_docs": [ "0.1.0" ], "apt": [ "1.1.1" ], "ark": [ "4.0.0", "3.1.1", "0.9.0", "0.3.0" ], "audit-cis": [ "0.4.0" ], "authenticator": [ "0.1.5", "0.1.4" ], "aws-static-ip-ha": [ "0.1.0" ], "aws-tools": [ "0.0.5" ], } ``` yaml ```yaml $ knife cookbook list -a -F yaml --- 7-zip: - 1.0.2 api_docs: - 0.1.0 apt: - 1.1.1 ark: - 4.0.0 - 3.1.1 - 0.9.0 - 0.3.0 audit-cis: - 0.4.0 authenticator: - 0.1.5 - 0.1.4 aws-static-ip-ha: - 0.1.0 aws-tools: - 0.0.5 ``` pp ``` $ knife cookbook list -a -F pp {"7-zip"=>["1.0.2"], "api_docs"=>["0.1.0"], "apt"=>["1.1.1"], "ark"=>["4.0.0", "3.1.1", "0.9.0", "0.3.0"], "audit-cis"=>["0.4.0"], "authenticator"=>["0.1.5", "0.1.4"], "aws-static-ip-ha"=>["0.1.0"], "aws-tools"=>["0.0.5"], } ``` Closes #13653 Signed-off-by: Milan Stastny <mistastn@cisco.com>
-rw-r--r--knife/lib/chef/knife/core/generic_presenter.rb28
-rw-r--r--knife/spec/integration/cookbook_bulk_delete_spec.rb4
-rw-r--r--knife/spec/integration/cookbook_list_spec.rb12
-rw-r--r--knife/spec/integration/cookbook_show_spec.rb2
-rw-r--r--knife/spec/unit/knife/cookbook_list_spec.rb4
-rw-r--r--knife/spec/unit/knife/core/ui_spec.rb4
6 files changed, 30 insertions, 24 deletions
diff --git a/knife/lib/chef/knife/core/generic_presenter.rb b/knife/lib/chef/knife/core/generic_presenter.rb
index 214ffd340b..f3ffb0a58b 100644
--- a/knife/lib/chef/knife/core/generic_presenter.rb
+++ b/knife/lib/chef/knife/core/generic_presenter.rb
@@ -206,26 +206,32 @@ class Chef
end
def format_cookbook_list_for_display(item)
- if config[:with_uri]
- item.inject({}) do |collected, (cookbook, versions)|
+ versions_by_cookbook = item.inject({}) do |collected, ( cookbook, versions )|
+ if config[:with_uri]
collected[cookbook] = {}
versions["versions"].each do |ver|
collected[cookbook][ver["version"]] = ver["url"]
end
- collected
- end
- else
- versions_by_cookbook = item.inject({}) do |collected, ( cookbook, versions )|
+ else
collected[cookbook] = versions["versions"].map { |v| v["version"] }
- collected
end
- key_length = versions_by_cookbook.empty? ? 0 : versions_by_cookbook.keys.map(&:size).max + 2
- versions_by_cookbook.sort.map do |cookbook, versions|
- "#{cookbook.ljust(key_length)} #{versions.join(" ")}"
+ collected.sort.to_h
+ end
+ if config[:with_uri]
+ versions_by_cookbook
+ else
+ case parse_format_option
+ when :summary
+ cookbooks = {}
+ versions_by_cookbook.map do |cookbook, versions|
+ cookbooks[cookbook] = versions.join(" ")
+ end
+ cookbooks
+ else
+ versions_by_cookbook
end
end
end
-
end
end
end
diff --git a/knife/spec/integration/cookbook_bulk_delete_spec.rb b/knife/spec/integration/cookbook_bulk_delete_spec.rb
index 0e791f5a1e..7177e4c418 100644
--- a/knife/spec/integration/cookbook_bulk_delete_spec.rb
+++ b/knife/spec/integration/cookbook_bulk_delete_spec.rb
@@ -55,8 +55,8 @@ describe "knife cookbook bulk delete", :workstation do
knife("cookbook bulk delete ^fo.*", input: "Y").should_succeed(stderr: stderr, stdout: stdout)
knife("cookbook list -a").should_succeed <<~EOM
- fax 0.6.0
- zfa 0.6.5
+ fax: 0.6.0
+ zfa: 0.6.5
EOM
end
# rubocop:enable Layout/TrailingWhitespace
diff --git a/knife/spec/integration/cookbook_list_spec.rb b/knife/spec/integration/cookbook_list_spec.rb
index e712ae3235..8b6c614e65 100644
--- a/knife/spec/integration/cookbook_list_spec.rb
+++ b/knife/spec/integration/cookbook_list_spec.rb
@@ -37,17 +37,17 @@ describe "knife cookbook list", :workstation do
it "knife cookbook list shows all the cookbooks" do
knife("cookbook list").should_succeed <<~EOM
- x 1.0.0
- y 0.6.5
- z 0.6.5
+ x: 1.0.0
+ y: 0.6.5
+ z: 0.6.5
EOM
end
it "knife cookbook list -a shows all the versions of all the cookbooks" do
knife("cookbook list -a").should_succeed <<~EOM
- x 1.0.0 0.6.5 0.6.0
- y 0.6.5 0.6.0
- z 0.6.5
+ x: 1.0.0 0.6.5 0.6.0
+ y: 0.6.5 0.6.0
+ z: 0.6.5
EOM
end
diff --git a/knife/spec/integration/cookbook_show_spec.rb b/knife/spec/integration/cookbook_show_spec.rb
index d8c2e38f64..e2e6ca2b38 100644
--- a/knife/spec/integration/cookbook_show_spec.rb
+++ b/knife/spec/integration/cookbook_show_spec.rb
@@ -32,7 +32,7 @@ describe "knife cookbook show", :workstation do
end
it "knife cookbook show x shows all the versions" do
- knife("cookbook show x").should_succeed "x 1.0.0 0.6.5\n"
+ knife("cookbook show x").should_succeed "x: 1.0.0 0.6.5\n"
end
# rubocop:disable Layout/TrailingWhitespace
diff --git a/knife/spec/unit/knife/cookbook_list_spec.rb b/knife/spec/unit/knife/cookbook_list_spec.rb
index 42c3ef1bfd..f3ec89544d 100644
--- a/knife/spec/unit/knife/cookbook_list_spec.rb
+++ b/knife/spec/unit/knife/cookbook_list_spec.rb
@@ -41,7 +41,7 @@ describe Chef::Knife::CookbookList do
.and_return(@cookbook_data)
@knife.run
@cookbook_names.each do |item|
- expect(@stdout.string).to match(/#{item}\s+1\.0\.1/)
+ expect(@stdout.string).to match(/#{item}:\s+1\.0\.1/)
end
end
@@ -79,7 +79,7 @@ describe Chef::Knife::CookbookList do
.and_return(@cookbook_data)
@knife.run
@cookbook_names.each do |item|
- expect(@stdout.string).to match(/#{item}\s+1\.0\.1\s+1\.0\.0/)
+ expect(@stdout.string).to match(/#{item}:\s+1\.0\.1\s+1\.0\.0/)
end
end
end
diff --git a/knife/spec/unit/knife/core/ui_spec.rb b/knife/spec/unit/knife/core/ui_spec.rb
index d5d09c0fdf..1c0e9a59b5 100644
--- a/knife/spec/unit/knife/core/ui_spec.rb
+++ b/knife/spec/unit/knife/core/ui_spec.rb
@@ -459,8 +459,8 @@ describe Chef::Knife::UI do
}
end
- it "should return an array of the cookbooks with versions" do
- expected_response = [ "cookbook_name 3.0.0 2.0.0 1.0.0" ]
+ it "should return an hash of the cookbooks with versions" do
+ expected_response = { "cookbook_name" => "3.0.0 2.0.0 1.0.0" }
response = @ui.format_cookbook_list_for_display(@item)
expect(response).to eq(expected_response)
end