diff options
author | Vivek Singh <vivek.singh@msystechnologies.com> | 2020-08-26 06:12:14 +0530 |
---|---|---|
committer | Vivek Singh <vivek.singh@msystechnologies.com> | 2020-08-26 06:12:14 +0530 |
commit | 5e4a052efe6cda1783723354eb03dcb322921c25 (patch) | |
tree | 8b676eba1d51e9ab479bd5d740a7f425c33eb6bb /lib/chef | |
parent | 53b8ef7f15fdd705c65ffbee1cef9f1f98e4664f (diff) | |
download | chef-5e4a052efe6cda1783723354eb03dcb322921c25.tar.gz |
remove duplication of deprecated options
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/knife/config_get.rb | 106 | ||||
-rw-r--r-- | lib/chef/knife/config_get_profile.rb | 17 | ||||
-rw-r--r-- | lib/chef/knife/config_list.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/config_list_profiles.rb | 120 | ||||
-rw-r--r-- | lib/chef/knife/config_use_profile.rb | 32 |
5 files changed, 29 insertions, 248 deletions
diff --git a/lib/chef/knife/config_get.rb b/lib/chef/knife/config_get.rb index a467730054..91e6b7affd 100644 --- a/lib/chef/knife/config_get.rb +++ b/lib/chef/knife/config_get.rb @@ -18,113 +18,21 @@ # require_relative "../knife" +require_relative "./config_show" class Chef class Knife - class ConfigGet < Knife - banner "knife config get [OPTION...] (options)\nDisplays the value of Chef::Config[OPTION] (or all config values)" - category "deprecated" + class ConfigGet < ConfigShow - option :all, - short: "-a", - long: "--all", - description: "Include options that are not set in the configuration.", - default: false + # Handle the subclassing (knife doesn't do this :() + dependency_loaders.concat(superclass.dependency_loaders) - option :raw, - short: "-r", - long: "--raw", - description: "Display a each value with no formatting.", - default: false + banner "knife config get [OPTION...] (options)\nDisplays the value of Chef::Config[OPTION] (or all config values)" + category "deprecated" def run Chef::Log.warn("knife config get has been deprecated in favor of knife config show. This will be removed in the major release version!") - - if config[:format] == "summary" && !config[:raw] - # If using the default, human-readable output, also show which config files are being loaded. - # Some of this is a bit hacky since it duplicates - wcl = self.class.config_loader - if wcl.credentials_found - loading_from("credentials", ChefConfig::PathHelper.home(".chef", "credentials")) - end - if wcl.config_location - loading_from("configuration", wcl.config_location) - end - - if Chef::Config[:config_d_dir] - wcl.find_dot_d(Chef::Config[:config_d_dir]).each do |path| - loading_from(".d/ configuration", path) - end - end - end - - # Dump the whole config, including defaults is --all was given. - config_data = Chef::Config.save(config[:all]) - # Two special cases, these are set during knife startup but we don't usually care about them. - unless config[:all] - config_data.delete(:color) - # Only keep these if true, false is much less important because it's the default. - config_data.delete(:local_mode) unless config_data[:local_mode] - config_data.delete(:enforce_default_paths) unless config_data[:enforce_default_paths] - config_data.delete(:enforce_path_sanity) unless config_data[:enforce_path_sanity] - end - - # Extract the data to show. - output_data = {} - if @name_args.empty? - output_data = config_data - else - @name_args.each do |filter| - if filter =~ %r{^/(.*)/(i?)$} - # It's a regex. - filter_re = Regexp.new($1, $2 ? Regexp::IGNORECASE : 0) - config_data.each do |key, value| - output_data[key] = value if key.to_s&.match?(filter_re) - end - else - # It's a dotted path string. - filter_parts = filter.split(/\./) - extract = lambda do |memo, filter_part| - memo.is_a?(Hash) ? memo[filter_part.to_sym] : nil - end - # Check against both config_data and all of the data, so that even - # in non-all mode, if you ask for a key that isn't in the non-all - # data, it will check against the broader set. - output_data[filter] = filter_parts.inject(config_data, &extract) || filter_parts.inject(Chef::Config.save(true), &extract) - end - end - end - - # Fix up some values. - output_data.each do |key, value| - if value == STDOUT - output_data[key] = "STDOUT" - elsif value == STDERR - output_data[key] = "STDERR" - end - end - - # Show the data. - if config[:raw] - output_data.each_value do |value| - ui.msg(value) - end - else - ui.output(output_data) - end - end - - private - - # Display a banner about loading from a config file. - # - # @api private - # @param type_of_file [String] Description of the file for the banner. - # @param path [String] Path of the file. - # @return [nil] - def loading_from(type_of_file, path) - path = Pathname.new(path).realpath - ui.msg(ui.color("Loading from #{type_of_file} file #{path}", :yellow)) + super end end end diff --git a/lib/chef/knife/config_get_profile.rb b/lib/chef/knife/config_get_profile.rb index 9158dab6ac..a355c531fe 100644 --- a/lib/chef/knife/config_get_profile.rb +++ b/lib/chef/knife/config_get_profile.rb @@ -16,25 +16,22 @@ # require_relative "../knife" +require_relative "./config_use" class Chef class Knife - class ConfigGetProfile < Knife + class ConfigGetProfile < ConfigUse + + # Handle the subclassing (knife doesn't do this :() + dependency_loaders.concat(superclass.dependency_loaders) + banner "knife config get-profile" category "deprecated" - # Disable normal config loading since this shouldn't fail if the profile - # doesn't exist of the config is otherwise corrupted. - def configure_chef - apply_computed_config - end - def run Chef::Log.warn("knife config get-profiles has been deprecated in favor of knife config use. This will be removed in the major release version!") - - ui.msg(self.class.config_loader.credentials_profile(config[:profile])) + super end - end end end diff --git a/lib/chef/knife/config_list.rb b/lib/chef/knife/config_list.rb index 53b3bac57d..f76489a106 100644 --- a/lib/chef/knife/config_list.rb +++ b/lib/chef/knife/config_list.rb @@ -22,7 +22,7 @@ class Chef class ConfigList < Knife banner "knife config list (options)" - TABLE_HEADER = [" Profile", "Client", "Key", "Server"].freeze + TABLE_HEADER = [" Profile", "Client", "Key", "Server"].freeze unless const_defined?(:TABLE_HEADER) deps do require_relative "../workstation_config_loader" diff --git a/lib/chef/knife/config_list_profiles.rb b/lib/chef/knife/config_list_profiles.rb index 64768c8415..c037b0de53 100644 --- a/lib/chef/knife/config_list_profiles.rb +++ b/lib/chef/knife/config_list_profiles.rb @@ -16,128 +16,22 @@ # require_relative "../knife" +require_relative "./config_list" class Chef class Knife - class ConfigListProfiles < Knife - 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 + class ConfigListProfiles < ConfigList - option :ignore_knife_rb, - short: "-i", - long: "--ignore-knife-rb", - description: "Ignore the current config.rb/knife.rb configuration.", - default: false + # Handle the subclassing (knife doesn't do this :() + dependency_loaders.concat(superclass.dependency_loaders) - def configure_chef - apply_computed_config - end + banner "knife config list-profiles (options)" + category "deprecated" def run Chef::Log.warn("knife config list-profiles has been deprecated in favor of knife config list. This will be removed in the major release version!") - - 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 + super 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 "-" - style :green - end - renderer.padding = [0, padding, 0, 0] # pad right with 2 characters - end - end - end - end end end diff --git a/lib/chef/knife/config_use_profile.rb b/lib/chef/knife/config_use_profile.rb index 8af3ee4b7b..169bdbef30 100644 --- a/lib/chef/knife/config_use_profile.rb +++ b/lib/chef/knife/config_use_profile.rb @@ -16,22 +16,17 @@ # require_relative "../knife" +require_relative "./config_use" class Chef class Knife - class ConfigUseProfile < Knife - banner "knife config use-profile PROFILE" - category "deprecated" + class ConfigUseProfile < ConfigUse - deps do - require "fileutils" unless defined?(FileUtils) - end + # Handle the subclassing (knife doesn't do this :() + dependency_loaders.concat(superclass.dependency_loaders) - # Disable normal config loading since this shouldn't fail if the profile - # doesn't exist of the config is otherwise corrupted. - def configure_chef - apply_computed_config - end + banner "knife config use-profile PROFILE" + category "deprecated" def run Chef::Log.warn("knife config use-profile has been deprecated in favor of knife config use. This will be removed in the major release version!") @@ -45,21 +40,8 @@ class Chef exit 1 end - if credentials_data.nil? || credentials_data.empty? - ui.fatal("No profiles found, #{self.class.config_loader.credentials_file_path} does not exist or is empty") - exit 1 - end - - if credentials_data[profile].nil? - raise ChefConfig::ConfigurationError, "Profile #{profile} doesn't exist. Please add it to #{self.class.config_loader.credentials_file_path} and if it is profile with DNS name check that you are not missing single quotes around it as per docs https://docs.chef.io/workstation/knife_setup/#knife-profiles." - else - # Ensure the .chef/ folder exists. - FileUtils.mkdir_p(File.dirname(context_file)) - IO.write(context_file, "#{profile}\n") - ui.msg("Set default profile to #{profile}") - end + super end - end end end |