This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
Extract Table Printing Logic #167
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64128c8
Relocate report classes to new namespace
danielsdeleo 90e985d
Add unit tests for Install report
danielsdeleo 4810a61
Extract table print logic to its own class
danielsdeleo 134fbe8
Add unit tests for upload report
danielsdeleo 7c4f55c
Use table printer in upload report
danielsdeleo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# | ||
# Copyright:: Copyright (c) 2014 Chef Software Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'chef-dk/policyfile/reports/table_printer' | ||
|
||
module ChefDK | ||
module Policyfile | ||
module Reports | ||
|
||
class Install | ||
|
||
attr_reader :ui | ||
attr_reader :policyfile_compiler | ||
|
||
def initialize(ui: nil, policyfile_compiler: nil) | ||
@ui = ui | ||
@policyfile_compiler = policyfile_compiler | ||
|
||
@fixed_version_install_table = nil | ||
@install_table = nil | ||
end | ||
|
||
def installing_fixed_version_cookbook(cookbook_spec) | ||
verb = cookbook_spec.installed? ? "Using " : "Installing" | ||
fixed_version_install_table.print_row(verb, cookbook_spec.name, cookbook_spec.version_constraint.to_s, "from #{cookbook_spec.source_type}") | ||
end | ||
|
||
def installing_cookbook(cookbook_spec) | ||
verb = cookbook_spec.installed? ? "Using " : "Installing" | ||
install_table.print_row(verb, cookbook_spec.name, cookbook_spec.version_constraint.version) | ||
end | ||
|
||
private | ||
|
||
def fixed_version_install_table | ||
@fixed_version_install_table ||= TablePrinter.new(ui) do |t| | ||
t.column(["Using", "Installing"]) | ||
t.column(policyfile_compiler.fixed_version_cookbooks_specs.keys) | ||
t.column | ||
t.column | ||
end | ||
end | ||
|
||
def install_table | ||
@install_table ||= TablePrinter.new(ui) do |t| | ||
t.column(["Using", "Installing"]) | ||
t.column(policyfile_compiler.graph_solution.keys) | ||
t.column(policyfile_compiler.graph_solution.values) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# Copyright:: Copyright (c) 2014 Chef Software Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
module ChefDK | ||
module Policyfile | ||
module Reports | ||
|
||
# Defines a table with a flexible number of columns and prints rows in | ||
# the table. Columns are defined ahead of time, by calling the #column | ||
# method, individual rows are printed by calling #print_row with the data | ||
# for each cell. | ||
class TablePrinter | ||
|
||
attr_reader :ui | ||
|
||
def initialize(ui) | ||
@ui = ui | ||
@column_widths = [] | ||
|
||
yield self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoa -- how is the caller using this? |
||
end | ||
|
||
# Defines a column. If a collection is given, it is mapped to an array | ||
# of strings and the longest string is used as the left justify width | ||
# for that column when rows are printed. | ||
def column(collection = []) | ||
@column_widths << (collection.map(&:to_s).map(&:size).max || 0) | ||
end | ||
|
||
# Print a row. | ||
def print_row(*cells) | ||
row = "" | ||
cells.each_with_index do |cell_data, i| | ||
row << cell_data.to_s.ljust(@column_widths[i]) | ||
row << " " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be personal preference, but when printing tables I think a tab (two spaces) between columns makes it more readable. Otherwise the row with the longest values in each column starts to read like a sentence. |
||
end | ||
ui.msg(row.strip) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# | ||
# Copyright:: Copyright (c) 2014 Chef Software Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'chef-dk/policyfile/reports/table_printer' | ||
|
||
module ChefDK | ||
module Policyfile | ||
module Reports | ||
class Upload | ||
|
||
attr_reader :reused_cbs | ||
attr_reader :uploaded_cbs | ||
attr_reader :ui | ||
|
||
def initialize(reused_cbs: [], uploaded_cbs: [], ui: nil) | ||
@reused_cbs = reused_cbs | ||
@uploaded_cbs = uploaded_cbs | ||
@ui = ui | ||
|
||
@justify_name_width = nil | ||
@justify_version_width = nil | ||
end | ||
|
||
def show | ||
reused_cbs.each do |cb_with_lock| | ||
lock = cb_with_lock.lock | ||
table.print_row("Using", lock.name, lock.version, "(#{lock.identifier[0,8]})") | ||
end | ||
|
||
uploaded_cbs.each do |cb_with_lock| | ||
lock = cb_with_lock.lock | ||
table.print_row("Uploaded", lock.name, lock.version, "(#{lock.identifier[0,8]})") | ||
end | ||
end | ||
|
||
def table | ||
@table ||= TablePrinter.new(ui) do |t| | ||
t.column(%w[ Using Uploaded ]) | ||
t.column(cookbook_names) | ||
t.column(cookbook_version_numbers) | ||
t.column | ||
end | ||
end | ||
|
||
def cookbook_names | ||
(reused_cbs + uploaded_cbs).map { |e| e.lock.name } | ||
end | ||
|
||
def cookbook_version_numbers | ||
(reused_cbs + uploaded_cbs).map { |e| e.lock.version } | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamedx down here, calling new() with a block that takes a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, nice. Hadn't really seen that use before, makes sense.