module ChefDK::Helpers

Public Instance Methods

chefdk_home() click to toggle source
# File lib/chef-dk/helpers.rb, line 78
def chefdk_home
  @chefdk_home ||= begin
                     chefdk_home_set = !([nil, ""].include? ENV["CHEFDK_HOME"])
                     if chefdk_home_set
                       ENV["CHEFDK_HOME"]
                     else
                       default_chefdk_home
                     end
                   end
end
err(message) click to toggle source
# File lib/chef-dk/helpers.rb, line 34
def err(message)
  stderr.print("#{message}\n")
end
git_bin_dir() click to toggle source

Unix users do not want git on their path if they already have it installed. Because we put `embedded/bin` on the path we must move the git binaries somewhere else that we can append to the end of the path. This is only a temporary solution - see github.com/chef/chef-dk/issues/854 for a better proposed solution.

# File lib/chef-dk/helpers.rb, line 106
def git_bin_dir
  @git_bin_dir ||= File.expand_path(File.join(omnibus_root, "gitbin"))
end
git_windows_bin_dir() click to toggle source

In our Windows ChefDK omnibus package we include Git For Windows, which has a bunch of helpful unix utilties (like ssh, scp, etc.) bundled with it

# File lib/chef-dk/helpers.rb, line 112
def git_windows_bin_dir
  @git_windows_bin_dir ||= File.expand_path(File.join(omnibus_root, "embedded", "git", "usr", "bin"))
end
msg(message) click to toggle source
# File lib/chef-dk/helpers.rb, line 38
def msg(message)
  stdout.print("#{message}\n")
end
omnibus_apps_dir() click to toggle source
# File lib/chef-dk/helpers.rb, line 62
def omnibus_apps_dir
  @ominbus_apps_dir ||= omnibus_expand_path(omnibus_root, "embedded", "apps")
end
omnibus_bin_dir() click to toggle source
# File lib/chef-dk/helpers.rb, line 66
def omnibus_bin_dir
  @omnibus_bin_dir ||= omnibus_expand_path(omnibus_root, "bin")
end
omnibus_chefdk_location() click to toggle source
# File lib/chef-dk/helpers.rb, line 74
def omnibus_chefdk_location
  @omnibus_chefdk_location ||= File.expand_path("embedded/apps/chef-dk", expected_omnibus_root)
end
omnibus_embedded_bin_dir() click to toggle source
# File lib/chef-dk/helpers.rb, line 70
def omnibus_embedded_bin_dir
  @omnibus_embedded_bin_dir ||= omnibus_expand_path(omnibus_root, "embedded", "bin")
end
omnibus_env() click to toggle source

environment vars for omnibus

# File lib/chef-dk/helpers.rb, line 119
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
omnibus_install?() click to toggle source

Locates the omnibus directories

# File lib/chef-dk/helpers.rb, line 54
def omnibus_install?
  File.exist?(omnibus_chefdk_location)
end
omnibus_root() click to toggle source
# File lib/chef-dk/helpers.rb, line 58
def omnibus_root
  @omnibus_root ||= omnibus_expand_path(expected_omnibus_root)
end
stderr() click to toggle source
# File lib/chef-dk/helpers.rb, line 46
def stderr
  $stderr
end
stdout() click to toggle source
# File lib/chef-dk/helpers.rb, line 42
def stdout
  $stdout
end
system_command(*command_args) click to toggle source

Runs given commands using mixlib-shellout

# File lib/chef-dk/helpers.rb, line 28
def system_command(*command_args)
  cmd = Mixlib::ShellOut.new(*command_args)
  cmd.run_command
  cmd
end
usr_bin_path(command) click to toggle source

Returns the full path to the given command under usr_bin_prefix

# File lib/chef-dk/helpers.rb, line 97
def usr_bin_path(command)
  File.join(usr_bin_prefix, command)
end
usr_bin_prefix() click to toggle source

Returns the directory that contains our main symlinks. On Mac we place all of our symlinks under /usr/local/bin on other platforms they are under /usr/bin

# File lib/chef-dk/helpers.rb, line 92
def usr_bin_prefix
  @usr_bin_prefix ||= os_x? ? "/usr/local/bin" : "/usr/bin"
end

Private Instance Methods

default_chefdk_home() click to toggle source
# File lib/chef-dk/helpers.rb, line 148
def default_chefdk_home
  if Chef::Platform.windows?
    File.join(ENV["LOCALAPPDATA"], "chefdk")
  else
    File.expand_path("~/.chefdk")
  end
end
expected_omnibus_root() click to toggle source
# File lib/chef-dk/helpers.rb, line 144
def expected_omnibus_root
  File.expand_path(File.join(Gem.ruby, "..", "..", ".."))
end
omnibus_expand_path(*paths) click to toggle source
# File lib/chef-dk/helpers.rb, line 137
def omnibus_expand_path(*paths)
  dir = File.expand_path(File.join(paths))
  raise OmnibusInstallNotFound.new unless dir && File.directory?(dir)

  dir
end
os_x?() click to toggle source

Returns true if we are on Mac OS X. Otherwise false

# File lib/chef-dk/helpers.rb, line 173
def os_x?
  !!(RUBY_PLATFORM =~ /darwin/)
end
reset!() click to toggle source

@api private This method resets all the instance variables used. It should only be used for testing

# File lib/chef-dk/helpers.rb, line 166
def reset!
  instance_variables.each do |ivar|
    instance_variable_set(ivar, nil)
  end
end
with_file(path, mode = "wb+", &block) click to toggle source

Open a file. By default, the mode is for read+write, and binary so that windows writes out what we tell it, as this is the most common case we have.

# File lib/chef-dk/helpers.rb, line 159
def with_file(path, mode = "wb+", &block)
  File.open(path, mode, &block)
end