diff options
author | Chef Expeditor <chef-ci@chef.io> | 2021-03-11 17:45:48 +0000 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2021-03-26 14:47:07 -0700 |
commit | 2e21a2d477c78e8a2cd2f6534e90bb744d49d4b1 (patch) | |
tree | c635e7409812851504504d5f715e43b9ca5952b1 /knife/lib/chef/knife/config_list.rb | |
parent | 0c44408bb35cbcd2497a340ce2bc39e643bcaf3e (diff) | |
download | chef-2e21a2d477c78e8a2cd2f6534e90bb744d49d4b1.tar.gz |
Move knife to its own gem
This moves knife into /knife, in the same way that chef-utils and
chef-config are separated.
NOTES:
== File History ==
If you see this message as the first message in the history of
an knife file, you can see the complete history by using
'git log --follow', 'git config log.follow true' to make it the default
behavior in this repository, or 'git config --globa log.follow true' to
make it the global default.
== API Changes ==
At the API level, there is one breaking change:
CookbookSiteStreamingUploader has been moved out of chef and into
knife/core.
There were a combination of reasons we chose this path:
- CookbookSiteStreamingUploader (CSSU) is only used within Knife.
- CookbookSiteStreamingUploader (CSSU) references the
command Chef::Knife::CookbookMetadata in order to generate
a metadata file for the cookbook to upload
- Chef::Knife::CookbookMetadata is no longer available to Chef:: because
Knife has been moved to its own gem. Knife gem depends on the Chef gem,
so Chef can't depend on something in Knife.
A search for usage in related projects (berks, chef-cli) and the
Internet at large shows that there are no known external consumers of CSSU.
For now, we'll move this class into Knife::Core, as it's going to be
faster than splitting off the metadata generation and time is a concern. If we
find that we need the metadata generation in chef/ proper, we should evaluate
decoupling that functionality from Knife::CookbookMetadata and exposing
it via something like `Chef::Cookbook::Metadata#from_cookbook_files`
== spec changes ==
The specs are kept in their existing locations, though we have
separated out a `knife_spec_helper` so that we can ensure knife is not
directly accessing chef requires; and chef is relying on knife's at all.
We also now clear gem paths with each test, to force gem state to reset.
This works around a problem where a combination of tests
is corrupting the internal Gem state, causing failures in
rubygems_spec. See branch `mp/broken-gems` for many more details around
findings so far.
Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
Diffstat (limited to 'knife/lib/chef/knife/config_list.rb')
-rw-r--r-- | knife/lib/chef/knife/config_list.rb | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/knife/lib/chef/knife/config_list.rb b/knife/lib/chef/knife/config_list.rb new file mode 100644 index 0000000000..be80ded3b2 --- /dev/null +++ b/knife/lib/chef/knife/config_list.rb @@ -0,0 +1,139 @@ +# +# Copyright:: Copyright (c) 2018, Noah Kantrowitz +# 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_relative "../knife" + +class Chef + class Knife + class ConfigList < Knife + banner "knife config list (options)" + + TABLE_HEADER ||= [" Profile", "Client", "Key", "Server"].freeze + + deps do + require "chef/workstation_config_loader" unless defined?(Chef::WorkstationConfigLoader) + require "tty-screen" unless defined?(TTY::Screen) + require "tty-table" unless defined?(TTY::Table) + end + + option :ignore_knife_rb, + short: "-i", + long: "--ignore-knife-rb", + description: "Ignore the current config.rb/knife.rb configuration.", + default: false + + def configure_chef + apply_computed_config + end + + def run + credentials_data = self.class.config_loader.parse_credentials_file + if credentials_data.nil? || credentials_data.empty? + # Should this just show the ambient knife.rb config as "default" instead? + ui.fatal("No profiles found, #{self.class.config_loader.credentials_file_path} does not exist or is empty") + exit 1 + end + + current_profile = self.class.config_loader.credentials_profile(config[:profile]) + profiles = credentials_data.keys.map do |profile| + if config[:ignore_knife_rb] + # Don't do any fancy loading nonsense, just the raw data. + profile_data = credentials_data[profile] + { + profile: profile, + active: profile == current_profile, + client_name: profile_data["client_name"] || profile_data["node_name"], + client_key: profile_data["client_key"], + server_url: profile_data["chef_server_url"], + } + else + # Fancy loading nonsense so we get what the actual config would be. + # Note that this modifies the global config, after this, all bets are + # off as to whats in the config. + Chef::Config.reset + wcl = Chef::WorkstationConfigLoader.new(nil, Chef::Log, profile: profile) + wcl.load + { + profile: profile, + active: profile == current_profile, + client_name: Chef::Config[:node_name], + client_key: Chef::Config[:client_key], + server_url: Chef::Config[:chef_server_url], + } + end + end + + # Try to reset the config. + unless config[:ignore_knife_rb] + Chef::Config.reset + apply_computed_config + end + + if ui.interchange? + # Machine-readable output. + ui.output(profiles) + else + # Table output. + ui.output(render_table(profiles)) + end + end + + private + + 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 + + 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 "-" + end + renderer.padding = [0, padding, 0, 0] # pad right with 2 characters + end + end + end + + end + end +end |