Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
require:
- chefstyle
- cookstyle

AllCops:
TargetRubyVersion: 3.1
Expand All @@ -9,6 +9,15 @@ AllCops:
Exclude:
- "vendor/**/*"
- "spec/**/*"
- "lib/chef-cli/skeletons/code_generator/**/*"
- "spec/unit/fixtures/chef-runner-cookbooks/**/*"
- "spec/unit/fixtures/cookbook_cache/**/*"
- "spec/unit/fixtures/example_cookbook/**/*"
- "spec/unit/fixtures/example_cookbook_metadata_json_only/**/*"
- "spec/unit/fixtures/example_cookbook_no_metadata/**/*"
- "spec/unit/fixtures/local_path_cookbooks/**/*"
- "my-cookbook/**/*"
- "test123/**/*"
Style/StringLiterals:
Exclude:
- "spec/unit/fixtures/**/*"
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ group :test do
gem "rspec-expectations", "~> 3.8"
gem "rspec-mocks", "~> 3.8"
gem "cookstyle"
gem "chefstyle"
gem "faraday_middleware"
gem "chef-test-kitchen-enterprise", git: "https://github.com/chef/chef-test-kitchen-enterprise", branch: "main"
gem "simplecov", require: false
Expand All @@ -30,4 +29,4 @@ group :profile do
gem "stackprof-webnav"
gem "memory_profiler"
end
end
end
27 changes: 4 additions & 23 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,20 @@ require "bundler/gem_tasks"

namespace :style do
begin
require "rubocop/rake_task"

desc "Run Cookbook Ruby style checks"
RuboCop::RakeTask.new(:cookstyle) do |t|
t.requires = ["cookstyle"]
t.patterns = ["lib/chef-cli/skeletons/code_generator"]
t.options = ["--display-cop-names"]
task :cookstyle do
sh "find lib/chef-cli/skeletons/code_generator -name '*.rb' -print0 | xargs -0 cookstyle --display-cop-names"
end
rescue LoadError => e
puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"]
end

begin
require "rubocop/rake_task"

ignore_dirs = Regexp.union(%w{
lib/chef-cli/skeletons/code_generator
spec/unit/fixtures/chef-runner-cookbooks
spec/unit/fixtures/cookbook_cache
spec/unit/fixtures/example_cookbook
spec/unit/fixtures/example_cookbook_metadata_json_only
spec/unit/fixtures/example_cookbook_no_metadata
spec/unit/fixtures/local_path_cookbooks
})

desc "Run Chef Ruby style checks"
RuboCop::RakeTask.new(:chefstyle) do |t|
t.requires = ["chefstyle"]
t.patterns = `rubocop --list-target-files`.split("\n").reject { |f| f =~ ignore_dirs }
t.options = ["--display-cop-names"]
task :chefstyle do
sh "cookstyle --chefstyle --display-cop-names"
end
rescue LoadError => e
puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"]
end
end

1 change: 0 additions & 1 deletion chef-cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Gem::Specification.new do |gem|
Dir.glob("*.gemspec") +
Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) }
gem.executables = %w{ chef-cli }
gem.test_files = gem.files.grep(%r{^spec/})
gem.require_paths = ["lib"]

gem.add_dependency "mixlib-cli", ">= 1.7", "< 3.0"
Expand Down
5 changes: 0 additions & 5 deletions coverage/.last_run.json

This file was deleted.

10 changes: 5 additions & 5 deletions lib/chef-cli/builtin_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

ChefCLI.commands do |c|
c.builtin "exec", :Exec, require_path: "chef-cli/command/exec",
desc: "Runs the command in context of the embedded ruby"
desc: "Runs the command in context of the embedded ruby"

c.builtin "env", :Env, require_path: "chef-cli/command/env",
desc: "Prints environment variables used by #{ChefCLI::Dist::PRODUCT}"
desc: "Prints environment variables used by #{ChefCLI::Dist::PRODUCT}"

c.builtin "gem", :GemForwarder, require_path: "chef-cli/command/gem",
desc: "Runs the `gem` command in context of the embedded Ruby"
desc: "Runs the `gem` command in context of the embedded Ruby"

c.builtin "generate", :Generate, desc: "Generate a new repository, cookbook, or other component"

Expand Down Expand Up @@ -56,7 +56,7 @@
c.builtin "undelete", :Undelete, desc: "Undo a delete command"

c.builtin "describe-cookbook", :DescribeCookbook, require_path: "chef-cli/command/describe_cookbook",
desc: "Prints cookbook checksum information used for cookbook identifier"
desc: "Prints cookbook checksum information used for cookbook identifier"
c.builtin "license", :License, require_path: "chef-cli/command/license",
desc: "Create & install a new license on the system or view installed license(s)."
desc: "Create & install a new license on the system or view installed license(s)."
end
4 changes: 2 additions & 2 deletions lib/chef-cli/command/delete_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def run(params)
def rm_policy_service
@rm_policy_service ||=
PolicyfileServices::RmPolicy.new(config: chef_config,
ui:,
policy_name:)
ui:,
policy_name:)
end

def debug?
Expand Down
4 changes: 2 additions & 2 deletions lib/chef-cli/command/delete_policy_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def run(params)
def rm_policy_group_service
@rm_policy_group_service ||=
PolicyfileServices::RmPolicyGroup.new(config: chef_config,
ui:,
policy_group:)
ui:,
policy_group:)
end

def debug?
Expand Down
8 changes: 4 additions & 4 deletions lib/chef-cli/command/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ def print_diff

def differ(ui = self.ui)
Policyfile::Differ.new(old_name: old_base.name,
old_lock:,
new_name: new_base.name,
new_lock:,
ui:)
old_lock:,
new_name: new_base.name,
new_lock:,
ui:)
end

def http_client
Expand Down
2 changes: 1 addition & 1 deletion lib/chef-cli/command/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GemForwarder < ChefCLI::Command::Base

def run(params)
retval = Gem::GemRunner.new.run( params.clone )
retval.nil? ? true : retval
retval.nil? || retval
rescue Gem::SystemExitException => e
exit( e.exit_code )
end
Expand Down
8 changes: 4 additions & 4 deletions lib/chef-cli/command/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def debug?

def push
@push ||= PolicyfileServices::Push.new(policyfile: policyfile_relative_path,
ui:,
policy_group:,
config: chef_config,
root_dir: Dir.pwd)
ui:,
policy_group:,
config: chef_config,
root_dir: Dir.pwd)
end

def handle_error(error)
Expand Down
12 changes: 6 additions & 6 deletions lib/chef-cli/command/show_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def run(params)
def show_policy_service
@policy_list_service ||=
PolicyfileServices::ShowPolicy.new(config: chef_config,
ui:,
policy_name:,
policy_group:,
show_orphans: show_orphans?,
summary_diff: show_summary_diff?,
pager: enable_pager?)
ui:,
policy_name:,
policy_group:,
show_orphans: show_orphans?,
summary_diff: show_summary_diff?,
pager: enable_pager?)
end

def debug?
Expand Down
4 changes: 2 additions & 2 deletions lib/chef-cli/command/undelete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def run(params)
def undelete_service
@undelete_service ||=
PolicyfileServices::Undelete.new(config: chef_config,
ui:,
undo_record_id:)
ui:,
undo_record_id:)
end

def debug?
Expand Down
4 changes: 2 additions & 2 deletions lib/chef-cli/cookbook_omnifetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
end

ChefCLI::ChefServerAPIMulti.new(Chef::Config.chef_server_url,
signing_key_filename: Chef::Config.client_key,
client_name: Chef::Config.node_name)
signing_key_filename: Chef::Config.client_key,
client_name: Chef::Config.node_name)
end
end
60 changes: 30 additions & 30 deletions lib/chef-cli/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,38 +156,38 @@ def git_windows_bin_dir
#
def habitat_env(show_warning: false)
@habitat_env ||=
begin
if habitat_chef_dke?
bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
end
versioned_pkg_prefix = fetch_chef_cli_version_pkg if ENV["CHEF_CLI_VERSION"]
begin
if habitat_chef_dke?
bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
end
versioned_pkg_prefix = fetch_chef_cli_version_pkg if ENV["CHEF_CLI_VERSION"]

if show_warning && ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix
ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.")
end
# Use the first available package for bin_pkg_prefix
bin_pkg_prefix ||= versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
raise "Error: Could not determine the Habitat package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless bin_pkg_prefix

# Determine vendor_dir by prioritizing the versioned package first
vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix

vendor_dir = File.join(vendor_pkg_prefix, "vendor")
# Construct PATH
path = [
File.join(bin_pkg_prefix, "bin"),
File.join(vendor_dir, "bin"),
ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
].flatten.uniq

if show_warning && ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix
ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.")
{
"PATH" => path.join(File::PATH_SEPARATOR),
"GEM_ROOT" => Gem.default_dir, # Default directory for gems
"GEM_HOME" => vendor_dir, # Set only if vendor_dir exists
"GEM_PATH" => vendor_dir, # Set only if vendor_dir exists
}
end
# Use the first available package for bin_pkg_prefix
bin_pkg_prefix ||= versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
raise "Error: Could not determine the Habitat package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless bin_pkg_prefix

# Determine vendor_dir by prioritizing the versioned package first
vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix

vendor_dir = File.join(vendor_pkg_prefix, "vendor")
# Construct PATH
path = [
File.join(bin_pkg_prefix, "bin"),
File.join(vendor_dir, "bin"),
ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
].flatten.uniq

{
"PATH" => path.join(File::PATH_SEPARATOR),
"GEM_ROOT" => Gem.default_dir, # Default directory for gems
"GEM_HOME" => vendor_dir, # Set only if vendor_dir exists
"GEM_PATH" => vendor_dir, # Set only if vendor_dir exists
}
end
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/chef-cli/policyfile/git_lock_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def lock_data
opt["tag"] = tag unless opt.key?("tag") || branch.nil?
opt["ref"] = ref unless opt.key?("ref") || ref.nil?

path_keys = %w{path rel}.map { |path_key| path_key if opt.key?(path_key) }.compact
path_keys = %w{path rel}.select { |path_key| opt.key?(path_key) }

path_keys.each do |name|
# We can safely grab the entire cookbook when the Policyfile defines a cookbook path of itself (".")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This is a Chef Infra Client recipe file. It can be used to specify resources
# which will apply configuration to a server.

log "Welcome to Chef Infra Client, #{node["example"]["name"]}!" do
log "Welcome to Chef Infra Client, #{node['example']['name']}!" do
level :info
end

Expand Down
10 changes: 5 additions & 5 deletions lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@

file "#{cookbook_dir}/.vscode/extensions.json" do
content <<~CONTENT
{
"recommendations": [
"chef-software.chef"
]
}
{
"recommendations": [
"chef-software.chef"
]
}
CONTENT
end
end
2 changes: 1 addition & 1 deletion lib/chef-cli/skeletons/code_generator/recipes/profile.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context = ChefCLI::Generator.context
cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
profile_dir = File.join(cookbook_dir, 'compliance', 'profiles', "#{context.new_file_basename}")
profile_dir = File.join(cookbook_dir, 'compliance', 'profiles', context.new_file_basename.to_s)
control_dir = File.join(profile_dir, 'controls')

directory control_dir do
Expand Down
2 changes: 1 addition & 1 deletion lib/kitchen/provisioner/chef_zero_capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_sandbox
def default_config_rb
cfg = super
# Need to tell chef-zero about our additional config.
root = config[:root_path].gsub("$env:TEMP", "\#{ENV['TEMP']\}")
root = config[:root_path].gsub("$env:TEMP", "\#{ENV['TEMP']}")
cfg[:policies_path] = remote_path_join(root, config[:policies_path])
cfg[:policy_groups_path] = remote_path_join(root, config[:policy_groups_path])
cfg[:cookbook_artifacts_path] = remote_path_join(root, config[:cookbook_artifacts_path])
Expand Down
4 changes: 2 additions & 2 deletions lib/kitchen/provisioner/policyfile_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def policy_exporter
# Must force this because TK by default copies the current cookbook to the sandbox
# See ChefCLI::PolicyfileServices::ExportRepo#assert_export_dir_clean!
@policy_exporter ||= ChefCLI::PolicyfileServices::ExportRepo.new(policyfile: config[:policyfile],
export_dir: sandbox_path,
force: true)
export_dir: sandbox_path,
force: true)
end

# Writes a fake (but valid) validation.pem into the sandbox directory.
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/command/generator_commands/repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def generator_context
let(:file) { "cookbooks/example/recipes/default.rb" }

it "has the right contents" do
expect(file_contents).to match(/log "Welcome to Chef Infra Client, \#\{node\["example"\]\["name"\]\}!" do/)
expect(file_contents).to match(/log "Welcome to Chef Infra Client, \#\{node\['example'\]\['name'\]\}!" do/)
end
end
end
Expand Down
Loading