lib/berkshelf.rb in berkshelf-0.4.0 vs lib/berkshelf.rb in berkshelf-0.5.0.rc1
- old
+ new
@@ -1,16 +1,18 @@
require 'forwardable'
require 'uri'
require 'pathname'
+require 'tmpdir'
require 'zlib'
require 'archive/tar/minitar'
require 'solve'
+require 'ridley'
require 'chef/knife'
-require 'chef/rest'
require 'chef/platform'
require 'chef/cookbook/metadata'
require 'chef/cookbook_version'
+require 'active_support/core_ext'
require 'berkshelf/version'
require 'berkshelf/core_ext'
require 'berkshelf/errors'
@@ -19,10 +21,11 @@
module Berkshelf
DEFAULT_CONFIG = File.expand_path(ENV["CHEF_CONFIG"] || "~/.chef/knife.rb")
DEFAULT_STORE_PATH = File.expand_path("~/.berkshelf").freeze
DEFAULT_FILENAME = 'Berksfile'.freeze
+ autoload :UI, 'berkshelf/ui'
autoload :Cli, 'berkshelf/cli'
autoload :Git, 'berkshelf/git'
autoload :Berksfile, 'berkshelf/berksfile'
autoload :Lockfile, 'berkshelf/lockfile'
autoload :BaseGenerator, 'berkshelf/base_generator'
@@ -41,16 +44,18 @@
attr_accessor :ui
attr_writer :config_path
attr_writer :cookbook_store
+ # @return [Pathname]
def root
@root ||= Pathname.new(File.expand_path('../', File.dirname(__FILE__)))
end
+ # @return [Berkshelf::UI]
def ui
- @ui ||= Chef::Knife::UI.new(null_stream, null_stream, STDIN, {})
+ @ui ||= Berkshelf::UI.new
end
# Returns the filepath to the location Berskhelf will use for
# storage; temp files will go here, Cookbooks will be downloaded
# to or uploaded from here. By default this is '~/.berkshelf' but
@@ -60,22 +65,34 @@
# @return [String]
def berkshelf_path
ENV["BERKSHELF_PATH"] || DEFAULT_STORE_PATH
end
+ # @return [String]
def tmp_dir
File.join(berkshelf_path, "tmp")
end
+ # Creates a temporary directory within the Berkshelf path
+ #
+ # @return [String]
+ # path to the created temporary directory
+ def mktmpdir
+ FileUtils.mkdir_p(File.join(berkshelf_path, "tmp"))
+ Dir.mktmpdir(nil, File.join(berkshelf_path, "tmp"))
+ end
+
def cookbooks_dir
File.join(berkshelf_path, "cookbooks")
end
+ # @return [Berkshelf::CookbookStore]
def cookbook_store
@cookbook_store ||= CookbookStore.new(cookbooks_dir)
end
+ # @return [String]
def config_path
@config_path ||= DEFAULT_CONFIG
end
# Load the config found at the given path as the Chef::Config. If no path is specified
@@ -106,29 +123,22 @@
# Get the appropriate Formatter object based on the formatter
# classes that have been registered.
#
# @return [~Formatter]
def formatter
- @formatter ||= (formatters.has_key?(@_format) ? formatters[@_format] : Formatters::HumanReadable).new
+ @formatter ||= Formatters::HumanReadable.new
end
- # Specify a formatter identifier
+ # Specify the format for output
#
- # @param [String] format
- # which formatter to use
+ # @param [#to_sym] format_id
+ # the ID of the registered formatter to use
#
- # @example Berkshelf.set_format "json"
- def set_format(format)
- @_format = format
- @formatter = nil
- end
-
- # Access the formatters map that links string symbols to Formatter
- # implementations
+ # @example Berkshelf.set_format :json
#
- # @return [Hash]
- def formatters
- @formatters ||= {}
+ # @return [~Formatter]
+ def set_format(format_id)
+ @formatter = Formatters[format_id].new
end
private
def null_stream