Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the Berkshelf.ui output Vagrant info #131

Merged
merged 1 commit into from Sep 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 4 additions & 13 deletions lib/berkshelf.rb
Expand Up @@ -119,26 +119,17 @@ def find_metadata(path = Dir.pwd)
#
# @return [~Formatter]
def formatter
@formatter ||= (formatters.has_key?(@_format) ? formatters[@_format] : Formatters::HumanReadable).new
@formatter ||= Formatters::HumanReadable.new
end

# Specify a formatter identifier
#
# @param [String] format
# @param [#to_sym] format
# which 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
#
# @return [Hash]
def formatters
@formatters ||= {}
def set_format(format_id)
@formatter = Formatters[format_id].new
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/berkshelf/cli.rb
Expand Up @@ -37,6 +37,7 @@ def initialize(*)
banner: "PATH"
class_option :format,
type: :string,
default: "human",
desc: "Output format to use.",
aliases: "-F",
banner: "FORMAT"
Expand Down
66 changes: 66 additions & 0 deletions lib/berkshelf/formatters.rb
@@ -1,12 +1,74 @@
module Berkshelf
# @author Michael Ivey <ivey@gweezlebur.com>
# @author Jamie Winsor <jamie@vialstudios.com>
module Formatters
class << self
@@formatters = Hash.new

# Access the formatters map that links string symbols to Formatter
# implementations
#
# @return [Hash]
def formatters
@@formatters
end

# @param [#to_sym] id
# @param [Constant] klass
#
# @raise [Berkshelf::InternalError] if an ID that has already been registered is attempted
# to be registered again
#
# @return [Hash]
# a hash of registered formatters
def register(id, klass)
unless id.respond_to?(:to_sym)
raise ArgumentError, "Invalid Formatter ID: must respond to #to_sym. You gave: #{id}"
end

id = id.to_sym
if self.formatters.has_key?(id)
raise Berkshelf::InternalError, "Formatter ID '#{id}' already registered"
end

self.formatters[id] = klass
end

# @param [#to_sym] id
#
# @return [~AbstractFormatter, nil]
def get(id)
unless id.respond_to?(:to_sym)
raise ArgumentError, "Invalid Formatter ID: must respond to #to_sym. You gave: #{id}"
end

self.formatters.fetch(id.to_sym, nil)
end
alias_method :[], :get
end

# @author Michael Ivey <ivey@gweezlebur.com>
#
# @abstract Include and override {#install} {#use} {#upload}
# {#msg} {#error} to implement.
#
# Implement {#cleanup_hook} to run any steps required to run after the task is finished
module AbstractFormatter
extend ActiveSupport::Concern

module ClassMethods
# @param [Symbol] id
#
# @raise [Berkshelf::InternalError] if an ID that has already been registered is attempted
# to be registered again
#
# @return [Hash]
# a hash of registered formatters
def register_formatter(id)
Formatters.register(id, self)
end
end

def cleanup_hook
# run after the task is finished
end
Expand All @@ -30,6 +92,10 @@ def msg(message)
def error(message)
raise AbstractFunction, "#error must be implemented on #{self.class}"
end

private

attr_reader :args
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/berkshelf/formatters/human_readable.rb
Expand Up @@ -4,7 +4,7 @@ module Formatters
class HumanReadable
include AbstractFormatter

Berkshelf.formatters["human"] = self
register_formatter :human

# Output a Cookbook installation message using {Berkshelf.ui}
#
Expand Down
2 changes: 1 addition & 1 deletion lib/berkshelf/formatters/json.rb
Expand Up @@ -4,7 +4,7 @@ module Formatters
class JSON
include AbstractFormatter

Berkshelf.formatters["json"] = self
register_formatter :json

def initialize
@output = {
Expand Down
12 changes: 6 additions & 6 deletions lib/berkshelf/vagrant.rb
Expand Up @@ -9,6 +9,7 @@ module Action
autoload :Install, 'berkshelf/vagrant/action/install'
autoload :Upload, 'berkshelf/vagrant/action/upload'
autoload :Clean, 'berkshelf/vagrant/action/clean'
autoload :SetUI, 'berkshelf/vagrant/action/set_ui'
end

autoload :Config, 'berkshelf/vagrant/config'
Expand All @@ -19,12 +20,6 @@ def shelf_for(env)
File.join(Berkshelf.berkshelf_path, "vagrant", env[:global_config].vm.host_name)
end

# @param [String] msg
# @param [Vagrant::Action::Environment] env
def info(msg, env)
env[:ui].info("[Berkshelf] #{msg}")
end

# @param [Symbol] shortcut
# @param [Vagrant::Config::Top] config
#
Expand Down Expand Up @@ -61,12 +56,17 @@ def chef_client?(config)
}

install = Vagrant::Action::Builder.new {
use Berkshelf::Vagrant::Action::SetUI
use Berkshelf::Vagrant::Action::Install
}

upload = Vagrant::Action::Builder.new {
use Berkshelf::Vagrant::Action::SetUI
use Berkshelf::Vagrant::Action::Upload
}

clean = Vagrant::Action::Builder.new {
use Berkshelf::Vagrant::Action::SetUI
use Berkshelf::Vagrant::Action::Clean
}

Expand Down
2 changes: 1 addition & 1 deletion lib/berkshelf/vagrant/action/clean.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(app, env)

def call(env)
if Berkshelf::Vagrant.chef_solo?(env[:global_config])
Berkshelf::Vagrant.info("cleaning Vagrant's shelf", env)
Berkshelf.formatter.msg "cleaning Vagrant's shelf"
FileUtils.remove_dir(self.shelf, fore: true)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/berkshelf/vagrant/action/install.rb
Expand Up @@ -12,9 +12,9 @@ def initialize(app, env)
@app = app
@shelf = Berkshelf::Vagrant.shelf_for(env)
@config = env[:global_config].berkshelf
@berksfile = Berksfile.from_file(@config.berksfile_path)

Berkshelf.config_path = @config.config_path
Berkshelf.load_config
@berksfile = Berksfile.from_file(@config.berksfile_path)
end

def call(env)
Expand All @@ -29,7 +29,7 @@ def call(env)
private

def install(env)
Berkshelf::Vagrant.info("installing cookbooks", env)
Berkshelf.formatter.msg "installing cookbooks..."
opts = {
path: self.shelf
}.merge(self.config.to_hash).symbolize_keys!
Expand Down
17 changes: 17 additions & 0 deletions lib/berkshelf/vagrant/action/set_ui.rb
@@ -0,0 +1,17 @@
module Berkshelf
module Vagrant
module Action
# @author Jamie Winsor <jamie@vialstudios.com>
class SetUI
def initialize(app, env)
@app = app
end

def call(env)
Berkshelf.ui = ::Vagrant::UI::Colored.new("Berkshelf")
@app.call(env)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/berkshelf/vagrant/action/upload.rb
Expand Up @@ -26,7 +26,7 @@ def call(env)

def upload(env)
Berkshelf::Vagrant.provisioners(:chef_client, env[:global_config]).each do |provisioner|
Berkshelf::Vagrant.info("uploading cookbooks to '#{provisioner.config.chef_server_url}'", env)
Berkshelf.formatter.msg "uploading cookbooks to '#{provisioner.config.chef_server_url}'"
berksfile.upload(
provisioner.config.chef_server_url,
node_name: self.node_name,
Expand Down
99 changes: 99 additions & 0 deletions spec/unit/berkshelf/formatters_spec.rb
@@ -1,7 +1,106 @@
require 'spec_helper'

module Berkshelf
describe Formatters do
before(:each) do
@original = Formatters.class_variable_get :@@formatters
Formatters.class_variable_set :@@formatters, Hash.new
end

after(:each) do
Formatters.class_variable_set :@@formatters, @original
end

describe "ClassMethods" do
subject { Formatters }
let(:format_id) { :rspec }
let(:format_klass) { Class.new }

describe "::register" do
it "adds the class of the includer to the list of registered formatters with the id" do
subject.register(format_id, format_klass)

subject.formatters.should have_key(format_id)
subject.formatters[format_id].should eql(format_klass)
end

context "when given a string instead of a symbol as the ID" do
it "converts the string to a symbol and registers it" do
subject.register("rspec", format_klass)

subject.formatters.should have_key(:rspec)
subject.formatters[:rspec].should eql(format_klass)
end
end

context "when a formatter of the given ID has already been registered" do
it "raises an InternalError" do
subject.register(format_id, format_klass)

lambda {
subject.register(format_id, format_klass)
}.should raise_error(Berkshelf::InternalError)
end
end
end

describe "::formatters" do
before(:each) do
subject.register(format_id, format_klass)
end

it "returns a hash where formatter ID's are keys and values are formatter classes" do
subject.formatters.should be_a(Hash)
subject.formatters.should have(1).item
subject.formatters.keys.first.should eql(format_id)
subject.formatters.values.first.should eql(format_klass)
end
end

describe "::get" do
before(:each) do
subject.register(format_id, format_klass)
end

it "returns the class constant of the given formatter ID" do
subject[format_id].should eql(format_klass)
end

context "when the ID has not been registered" do
it "returns nil" do
subject[:not_there].should be_nil
end
end
end
end
end

describe Formatters::AbstractFormatter do
before(:each) do
@original = Formatters.class_variable_get :@@formatters
Formatters.class_variable_set :@@formatters, Hash.new
end

after(:each) do
Formatters.class_variable_set :@@formatters, @original
end

describe "ClassMethods" do
subject do
Class.new do
include Formatters::AbstractFormatter
end
end

describe "::register_formatter" do
it "delegates to Formatters" do
Formatters.should_receive(:register).with(:rspec, subject)

subject.register_formatter(:rspec)
end
end
end

subject do
Class.new do
include Formatters::AbstractFormatter
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/berkshelf_spec.rb
Expand Up @@ -60,11 +60,11 @@

class CustomFormatter
include Berkshelf::Formatters::AbstractFormatter
Berkshelf.formatters["custom"] = self
register_formatter :custom
end

before do
Berkshelf.set_format "custom"
Berkshelf.set_format :custom
end

it "should be the custom class" do
Expand Down