summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-08-26 10:23:49 -0700
committerGitHub <noreply@github.com>2020-08-26 10:23:49 -0700
commit098111dc326e4509935e539304baad9a73fbb1b9 (patch)
tree4c34196370473be8031b57ba3991c6428eb07066
parent768c317d48f8060db0d68b4c2f15677fca568e70 (diff)
parentc9018f96d82b470043b00e8040a36362b83c78be (diff)
downloadchef-098111dc326e4509935e539304baad9a73fbb1b9.tar.gz
Merge pull request #10351 from chef/VSingh/knife-list-deprecation-update
more config specs cleanup & remove deprecated from knife config list
-rw-r--r--lib/chef/knife/config_get.rb106
-rw-r--r--lib/chef/knife/config_get_profile.rb17
-rw-r--r--lib/chef/knife/config_list.rb6
-rw-r--r--lib/chef/knife/config_list_profiles.rb120
-rw-r--r--lib/chef/knife/config_use_profile.rb32
-rw-r--r--spec/integration/knife/config_get_profile_spec.rb114
-rw-r--r--spec/integration/knife/config_list_spec.rb12
-rw-r--r--spec/integration/knife/config_show_spec.rb (renamed from spec/integration/knife/config_get_spec.rb)6
-rw-r--r--spec/integration/knife/config_use_spec.rb50
9 files changed, 88 insertions, 375 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 0a5d493273..c9f821e2a8 100644
--- a/lib/chef/knife/config_list.rb
+++ b/lib/chef/knife/config_list.rb
@@ -21,9 +21,8 @@ class Chef
class Knife
class ConfigList < Knife
banner "knife config list (options)"
- category "deprecated"
- TABLE_HEADER = [" Profile", "Client", "Key", "Server"].freeze
+ TABLE_HEADER ||= [" Profile", "Client", "Key", "Server"].freeze
deps do
require_relative "../workstation_config_loader"
@@ -128,8 +127,7 @@ class Chef
else
table.render do |renderer|
renderer.border do
- mid "-"
- style :green
+ mid "-"
end
renderer.padding = [0, padding, 0, 0] # pad right with 2 characters
end
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
diff --git a/spec/integration/knife/config_get_profile_spec.rb b/spec/integration/knife/config_get_profile_spec.rb
deleted file mode 100644
index 0cfa118efb..0000000000
--- a/spec/integration/knife/config_get_profile_spec.rb
+++ /dev/null
@@ -1,114 +0,0 @@
-#
-# Copyright 2018, Noah Kantrowitz
-#
-# 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 "spec_helper"
-require "support/shared/integration/integration_helper"
-require "support/shared/context/config"
-
-describe "knife config get-profile", :workstation do
- include IntegrationSupport
- include KnifeSupport
-
- include_context "default config options"
-
- let(:cmd_args) { [] }
-
- when_the_repository("has a custom env") do
- subject do
- cmd = knife("config", "get-profile", *cmd_args, instance_filter: lambda { |instance|
- # Fake the failsafe check because this command doesn't actually process knife.rb.
- $__KNIFE_INTEGRATION_FAILSAFE_CHECK << " ole"
- })
- cmd.stdout
- end
-
- around do |ex|
- # Store and reset the value of some env vars.
- old_chef_home = ENV["CHEF_HOME"]
- old_knife_home = ENV["KNIFE_HOME"]
- old_home = ENV["HOME"]
- old_wd = Dir.pwd
- ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME"
- # Clear these out because they are cached permanently.
- ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) }
- Chef::Knife::ConfigGetProfile.reset_config_loader!
- begin
- ex.run
- ensure
- ENV["CHEF_HOME"] = old_chef_home
- ENV["KNIFE_HOME"] = old_knife_home
- ENV["HOME"] = old_home
- Dir.chdir(old_wd)
- ENV[ChefUtils.windows? ? "CD" : "PWD"] = Dir.pwd
- ChefConfig::PathHelper.per_tool_home_environment = nil
- end
- end
-
- before do
- # Always run from the temp folder. This can't be in the `around` block above
- # because it has to run after the before set in the "with a chef repo" shared context.
- directory("repo")
- Dir.chdir(path_to("repo"))
- ENV[ChefUtils.windows? ? "CD" : "PWD"] = Dir.pwd
- ENV["HOME"] = path_to(".")
- end
-
- context "with no configuration" do
- it { is_expected.to eq "default\n" }
- end
-
- context "with --profile" do
- let(:cmd_args) { %w{--profile production} }
- it { is_expected.to eq "production\n" }
- end
-
- context "with an environment variable" do
- around do |ex|
- old_chef_profile = ENV["CHEF_PROFILE"]
- begin
- ENV["CHEF_PROFILE"] = "staging"
- ex.run
- ensure
- ENV["CHEF_PROFILE"] = old_chef_profile
- end
- end
-
- it { is_expected.to eq "staging\n" }
- end
-
- context "with a context file" do
- before { file(".chef/context", "development\n") }
- it { is_expected.to eq "development\n" }
- end
-
- context "with a context file under $CHEF_HOME" do
- before do
- file("chefhome/.chef/context", "other\n")
- ENV["CHEF_HOME"] = path_to("chefhome")
- end
-
- it { is_expected.to eq "other\n" }
- end
-
- context "with a context file under $KNIFE_HOME" do
- before do
- file("knifehome/.chef/context", "other\n")
- ENV["KNIFE_HOME"] = path_to("knifehome")
- end
-
- it { is_expected.to eq "other\n" }
- end
- end
-end
diff --git a/spec/integration/knife/config_list_spec.rb b/spec/integration/knife/config_list_spec.rb
index 03409e4983..b05350ed87 100644
--- a/spec/integration/knife/config_list_spec.rb
+++ b/spec/integration/knife/config_list_spec.rb
@@ -40,7 +40,7 @@ describe "knife config list", :workstation do
old_wd = Dir.pwd
# Clear these out because they are cached permanently.
ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) }
- Chef::Knife::ConfigListProfiles.reset_config_loader!
+ Chef::Knife::ConfigList.reset_config_loader!
begin
ex.run
ensure
@@ -84,7 +84,7 @@ describe "knife config list", :workstation do
EOH
it { is_expected.to eq <<~EOH.delete("#") }
Profile Client Key Server #
- \e[32m--------------------------------------------------------------------------------\e[0m#
+ --------------------------------------------------------------------------------#
*default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg #
EOH
end
@@ -108,7 +108,7 @@ describe "knife config list", :workstation do
EOH
it { is_expected.to eq <<~EOH.delete("#") }
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 #
@@ -135,7 +135,7 @@ describe "knife config list", :workstation do
EOH
it { is_expected.to eq <<~EOH.delete("#") }
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 #
@@ -162,7 +162,7 @@ describe "knife config list", :workstation do
EOH
it { is_expected.to eq <<~EOH.delete("#") }
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 #
@@ -185,7 +185,7 @@ describe "knife config list", :workstation do
EOH
it { is_expected.to eq <<~EOH.delete("#") }
Profile Client Key Server #
- \e[32m--------------------------------------------------------------\e[0m#
+ --------------------------------------------------------------#
*default https://example.com/organizations/testorg #
EOH
end
diff --git a/spec/integration/knife/config_get_spec.rb b/spec/integration/knife/config_show_spec.rb
index a0a5df1a8d..9e6ff73aa1 100644
--- a/spec/integration/knife/config_get_spec.rb
+++ b/spec/integration/knife/config_show_spec.rb
@@ -17,7 +17,7 @@ require "spec_helper"
require "support/shared/integration/integration_helper"
require "support/shared/context/config"
-describe "knife config get", :workstation do
+describe "knife config show", :workstation do
include IntegrationSupport
include KnifeSupport
@@ -27,7 +27,7 @@ describe "knife config get", :workstation do
when_the_repository("has a custom env") do
subject do
- cmd = knife("config", "get", *cmd_args, instance_filter: lambda { |instance|
+ cmd = knife("config", "show", *cmd_args, instance_filter: lambda { |instance|
# Clear the stub set up in KnifeSupport.
allow(File).to receive(:file?).and_call_original
# Lies, damn lies, and config files. We need to allow normal config loading
@@ -47,7 +47,7 @@ describe "knife config get", :workstation do
ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME"
# Clear these out because they are cached permanently.
ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) }
- Chef::Knife::ConfigGet.reset_config_loader!
+ Chef::Knife::ConfigShow.reset_config_loader!
begin
ex.run
ensure
diff --git a/spec/integration/knife/config_use_spec.rb b/spec/integration/knife/config_use_spec.rb
index 41d575669c..0431729b25 100644
--- a/spec/integration/knife/config_use_spec.rb
+++ b/spec/integration/knife/config_use_spec.rb
@@ -45,7 +45,7 @@ describe "knife config use", :workstation do
ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME"
# Clear these out because they are cached permanently.
ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) }
- Chef::Knife::ConfigUseProfile.reset_config_loader!
+ Chef::Knife::ConfigUse.reset_config_loader!
begin
ex.run
ensure
@@ -67,6 +67,54 @@ describe "knife config use", :workstation do
ENV["HOME"] = path_to(".")
end
+ context "with no argument" do
+ context "with no configuration" do
+ it { is_expected.to eq "default\n" }
+ end
+
+ context "with --profile" do
+ let(:cmd_args) { %w{--profile production} }
+ it { is_expected.to eq "production\n" }
+ end
+
+ context "with an environment variable" do
+ around do |ex|
+ old_chef_profile = ENV["CHEF_PROFILE"]
+ begin
+ ENV["CHEF_PROFILE"] = "staging"
+ ex.run
+ ensure
+ ENV["CHEF_PROFILE"] = old_chef_profile
+ end
+ end
+
+ it { is_expected.to eq "staging\n" }
+ end
+
+ context "with a context file" do
+ before { file(".chef/context", "development\n") }
+ it { is_expected.to eq "development\n" }
+ end
+
+ context "with a context file under $CHEF_HOME" do
+ before do
+ file("chefhome/.chef/context", "other\n")
+ ENV["CHEF_HOME"] = path_to("chefhome")
+ end
+
+ it { is_expected.to eq "other\n" }
+ end
+
+ context "with a context file under $KNIFE_HOME" do
+ before do
+ file("knifehome/.chef/context", "other\n")
+ ENV["KNIFE_HOME"] = path_to("knifehome")
+ end
+
+ it { is_expected.to eq "other\n" }
+ end
+ end
+
context "with an argument" do
let(:cmd_args) { %w{production} }
before { file(".chef/credentials", <<~EOH) }