module ChefCLI::Helpers
def default_package_home
def default_package_home if Chef::Platform.windows? File.join(ENV["LOCALAPPDATA"], "chef-workstation") else File.expand_path("~/.chef-workstation") end end
def err(message)
def err(message) stderr.print("#{message}\n") end
def expected_omnibus_root
def expected_omnibus_root File.expand_path(File.join(Gem.ruby, "..", "..", "..")) end
def git_bin_dir
This is only a temporary solution - see https://github.com/chef/chef-cli/issues/854
somewhere else that we can append to the end of the path.
Because we put `embedded/bin` on the path we must move the git binaries
Unix users do not want git on their path if they already have it installed.
def git_bin_dir @git_bin_dir ||= File.expand_path(File.join(omnibus_root, "gitbin")) end
def git_windows_bin_dir
In our Windows ChefCLI omnibus package we include Git For Windows, which
def git_windows_bin_dir @git_windows_bin_dir ||= File.expand_path(File.join(omnibus_root, "embedded", "git", "usr", "bin")) end
def msg(message)
def msg(message) stdout.print("#{message}\n") end
def omnibus_bin_dir
def omnibus_bin_dir @omnibus_bin_dir ||= omnibus_expand_path(omnibus_root, "bin") end
def omnibus_embedded_bin_dir
def omnibus_embedded_bin_dir @omnibus_embedded_bin_dir ||= omnibus_expand_path(omnibus_root, "embedded", "bin") end
def omnibus_env
environment vars for omnibus
def omnibus_env @omnibus_env ||= begin user_bin_dir = File.expand_path(File.join(Gem.user_dir, "bin")) path = [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"] ] path << git_bin_dir if Dir.exist?(git_bin_dir) path << git_windows_bin_dir if Dir.exist?(git_windows_bin_dir) { "PATH" => path.join(File::PATH_SEPARATOR), "GEM_ROOT" => Gem.default_dir, "GEM_HOME" => Gem.user_dir, "GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR), } end end
def omnibus_expand_path(*paths)
def omnibus_expand_path(*paths) dir = File.expand_path(File.join(paths)) raise OmnibusInstallNotFound.new unless dir && File.directory?(dir) dir end
def omnibus_install?
Locates the omnibus directories
def omnibus_install? # We also checck if the location we're running from (omnibus_root is relative to currently-running ruby) # includes the version manifest that omnibus packages ship with. If it doesn't then we're running locally or # out of a gem - so not as an 'omnibus install' File.exist?(expected_omnibus_root) && File.exist?(File.join(expected_omnibus_root, "version-manifest.json")) end
def omnibus_root
def omnibus_root @omnibus_root ||= omnibus_expand_path(expected_omnibus_root) end
def os_x?
def os_x? !!(RUBY_PLATFORM =~ /darwin/) end
def package_home
def package_home @package_home ||= begin package_home_set = !([nil, ""].include? ENV["CHEF_WORKSTATION_HOME"]) if package_home_set ENV["CHEF_WORKSTATION_HOME"] else default_package_home end end end
def reset!
- Api: - private
def reset! instance_variables.each do |ivar| instance_variable_set(ivar, nil) end end
def stderr
def stderr $stderr end
def stdout
def stdout $stdout end
def system_command(*command_args)
Runs given commands using mixlib-shellout
def system_command(*command_args) cmd = Mixlib::ShellOut.new(*command_args) cmd.run_command cmd end
def usr_bin_path(command)
def usr_bin_path(command) File.join(usr_bin_prefix, command) end
def usr_bin_prefix
On Mac we place all of our symlinks under /usr/local/bin on other
Returns the directory that contains our main symlinks.
def usr_bin_prefix @usr_bin_prefix ||= os_x? ? "/usr/local/bin" : "/usr/bin" end
def with_file(path, mode = "wb+", &block)
and binary so that windows writes out what we tell it,
Open a file. By default, the mode is for read+write,
def with_file(path, mode = "wb+", &block) File.open(path, mode, &block) end