summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-08-25 10:07:21 -0700
committerGitHub <noreply@github.com>2020-08-25 10:07:21 -0700
commit6681194a118bbf6a11ee4bd4cde3339b57fa365c (patch)
tree2b4d4caa40575c7a4066dc5fb528f0f18d44738f
parent196902f1db7ad8682c344ebda12b9ebe548f115c (diff)
parentde848c5ff06d1c7829af5635a90817ec31a99116 (diff)
downloadchef-6681194a118bbf6a11ee4bd4cde3339b57fa365c.tar.gz
Merge pull request #10341 from chef/VSingh/add-tty-table
knife config list-profiles UI with tty-table
-rw-r--r--Gemfile.lock2
-rw-r--r--chef.gemspec1
-rw-r--r--lib/chef/knife/config_list_profiles.rb65
-rw-r--r--spec/integration/knife/config_list_profiles_spec.rb45
4 files changed, 65 insertions, 48 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index f52a5f13a4..abd5c1592b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -61,6 +61,7 @@ PATH
train-winrm (>= 0.2.5)
tty-prompt (~> 0.21)
tty-screen (~> 0.6)
+ tty-table (~> 0.11.0)
uuidtools (~> 2.1.5)
chef (16.5.9-universal-mingw32)
addressable
@@ -97,6 +98,7 @@ PATH
train-winrm (>= 0.2.5)
tty-prompt (~> 0.21)
tty-screen (~> 0.6)
+ tty-table (~> 0.11.0)
uuidtools (~> 2.1.5)
win32-api (~> 1.5.3)
win32-certstore (~> 0.3)
diff --git a/chef.gemspec b/chef.gemspec
index b6931cc4ef..78b1f0c3c2 100644
--- a/chef.gemspec
+++ b/chef.gemspec
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
s.add_dependency "highline", ">= 1.6.9", "< 3"
s.add_dependency "tty-prompt", "~> 0.21" # knife ui.ask prompt
s.add_dependency "tty-screen", "~> 0.6" # knife list
+ s.add_dependency "tty-table", "~> 0.11.0" # knife render table output
s.add_dependency "pastel" # knife ui.color
s.add_dependency "erubis", "~> 2.7"
s.add_dependency "diff-lcs", ">= 1.2.4", "< 1.4.0" # 1.4 breaks output
diff --git a/lib/chef/knife/config_list_profiles.rb b/lib/chef/knife/config_list_profiles.rb
index df5a08c86f..64768c8415 100644
--- a/lib/chef/knife/config_list_profiles.rb
+++ b/lib/chef/knife/config_list_profiles.rb
@@ -23,8 +23,12 @@ class Chef
banner "knife config list-profiles (options)"
category "deprecated"
+ TABLE_HEADER = [" Profile", "Client", "Key", "Server"].freeze
+
deps do
require_relative "../workstation_config_loader"
+ require "tty-screen" unless defined?(TTY::Screen)
+ require "tty-table" unless defined?(TTY::Table)
end
option :ignore_knife_rb,
@@ -93,36 +97,45 @@ class Chef
private
- def render_table(profiles, padding: 2)
- # Replace the home dir in the client key path with ~.
+ def render_table(profiles, padding: 1)
+ rows = []
+ # Render the data to a 2D array that will be used for the table.
profiles.each do |profile|
+ # Replace the home dir in the client key path with ~.
profile[:client_key] = profile[:client_key].to_s.gsub(/^#{Regexp.escape(Dir.home)}/, "~") if profile[:client_key]
+ profile[:profile] = "#{profile[:active] ? "*" : " "}#{profile[:profile]}"
+ rows << profile.values_at(:profile, :client_name, :client_key, :server_url)
end
- # Render the data to a 2D array that will be used for the table.
- table_data = [["", "Profile", "Client", "Key", "Server"]] + profiles.map do |profile|
- [profile[:active] ? "*" : ""] + profile.values_at(:profile, :client_name, :client_key, :server_url).map(&:to_s)
- end
- # Compute column widths.
- column_widths = Array.new(table_data.first.length) do |i|
- table_data.map { |row| row[i].length + padding }.max
- end
- # Special case, the first col gets no padding (because indicator) and last
- # get no padding because last.
- column_widths[0] -= padding
- column_widths[-1] -= padding
- # Build the format string for each row.
- format_string = column_widths.map { |w| "%-#{w}.#{w}s" }.join("")
- format_string << "\n"
- # Print the header row and a separator.
- table = ui.color(format_string % table_data.first, :green)
- table << "-" * column_widths.sum
- table << "\n"
- # Print the rest of the table.
- table_data.drop(1).each do |row|
- table << format_string % row
+
+ table = TTY::Table.new(header: TABLE_HEADER, rows: rows)
+
+ # Rotate the table to vertical if the screen width is less than table width.
+ if table.width > TTY::Screen.width
+ table.orientation = :vertical
+ table.rotate
+ # Add a new line after each profile record.
+ table.render do |renderer|
+ renderer.border do
+ separator ->(row) { (row + 1) % TABLE_HEADER.size == 0 }
+ end
+ # Remove the leading space added of the first column.
+ renderer.filter = Proc.new do |val, row_index, col_index|
+ if col_index == 1 || (row_index) % TABLE_HEADER.size == 0
+ val.strip
+ else
+ val
+ end
+ end
+ end
+ else
+ table.render do |renderer|
+ renderer.border do
+ mid "-"
+ style :green
+ end
+ renderer.padding = [0, padding, 0, 0] # pad right with 2 characters
+ end
end
- # Trim the last newline because ui.output adds one.
- table.chomp!
end
end
diff --git a/spec/integration/knife/config_list_profiles_spec.rb b/spec/integration/knife/config_list_profiles_spec.rb
index 0f4fd80498..08372e646a 100644
--- a/spec/integration/knife/config_list_profiles_spec.rb
+++ b/spec/integration/knife/config_list_profiles_spec.rb
@@ -57,6 +57,7 @@ describe "knife config list-profiles", :workstation do
Dir.chdir(path_to("repo"))
ENV[ChefUtils.windows? ? "CD" : "PWD"] = Dir.pwd
ENV["HOME"] = path_to(".")
+ allow(TTY::Screen).to receive(:width).and_return(200)
end
# NOTE: The funky formatting with # at the end of the line of some of the
@@ -82,9 +83,9 @@ describe "knife config list-profiles", :workstation do
chef_server_url = "https://example.com/organizations/testorg"
EOH
it { is_expected.to eq <<~EOH.delete("#") }
- Profile Client Key Server #
- ----------------------------------------------------------------------------------#
- *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg#
+ Profile Client Key Server #
+ \e[32m--------------------------------------------------------------------------------\e[0m#
+ *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg #
EOH
end
@@ -106,11 +107,11 @@ describe "knife config list-profiles", :workstation do
chef_server_url = "https://example.com/organizations/testorg"
EOH
it { is_expected.to eq <<~EOH.delete("#") }
- Profile Client Key Server #
- ----------------------------------------------------------------------------------#
- *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg#
- prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
- qa qauser ~/src/qauser.pem https://example.com/organizations/testorg#
+ Profile Client Key Server #
+ \e[32m--------------------------------------------------------------------------------\e[0m#
+ *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg #
+ prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
+ qa qauser ~/src/qauser.pem https://example.com/organizations/testorg #
EOH
end
@@ -133,11 +134,11 @@ describe "knife config list-profiles", :workstation do
chef_server_url = "https://example.com/organizations/testorg"
EOH
it { is_expected.to eq <<~EOH.delete("#") }
- Profile Client Key Server #
- ----------------------------------------------------------------------------------#
- default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg#
- *prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
- qa qauser ~/src/qauser.pem https://example.com/organizations/testorg#
+ Profile Client Key Server #
+ \e[32m--------------------------------------------------------------------------------\e[0m#
+ default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg #
+ *prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
+ qa qauser ~/src/qauser.pem https://example.com/organizations/testorg #
EOH
end
@@ -160,11 +161,11 @@ describe "knife config list-profiles", :workstation do
chef_server_url = "https://example.com/organizations/testorg"
EOH
it { is_expected.to eq <<~EOH.delete("#") }
- Profile Client Key Server #
- ---------------------------------------------------------------------------------#
- default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg#
- prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
- qa qauser ~/src/qauser.pem https://example.com/organizations/testorg#
+ Profile Client Key Server #
+ \e[32m--------------------------------------------------------------------------------\e[0m#
+ default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg #
+ prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod #
+ qa qauser ~/src/qauser.pem https://example.com/organizations/testorg #
EOH
end
@@ -173,7 +174,7 @@ describe "knife config list-profiles", :workstation do
[default]
chef_server_url = "https://example.com/organizations/testorg"
EOH
- it { is_expected.to match %r{^*default .*? https://example.com/organizations/testorg$} }
+ it { is_expected.to match %r{^*default .*? https://example.com/organizations/testorg} }
end
context "with -i" do
@@ -183,9 +184,9 @@ describe "knife config list-profiles", :workstation do
chef_server_url = "https://example.com/organizations/testorg"
EOH
it { is_expected.to eq <<~EOH.delete("#") }
- Profile Client Key Server #
- ----------------------------------------------------------------#
- *default https://example.com/organizations/testorg#
+ Profile Client Key Server #
+ \e[32m--------------------------------------------------------------\e[0m#
+ *default https://example.com/organizations/testorg #
EOH
end