Class: Berkshelf::Formatters::JSON

Inherits:
Object
  • Object
show all
Includes:
AbstractFormatter
Defined in:
lib/berkshelf/formatters/json.rb

Instance Method Summary collapse

Constructor Details

#initializeJSON

Returns a new instance of JSON.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/berkshelf/formatters/json.rb', line 8

def initialize
  Berkshelf.ui.mute!

  @output = {
    cookbooks: Array.new,
    errors: Array.new,
    messages: Array.new
  }
  @cookbooks = Hash.new
  super
end

Instance Method Details

#cleanup_hookObject



20
21
22
23
24
25
26
27
# File 'lib/berkshelf/formatters/json.rb', line 20

def cleanup_hook
  cookbooks.each do |name, details|
    details[:name] = name
    output[:cookbooks] << details
  end

  print ::JSON.pretty_generate(output)
end

#error(message) ⇒ Object

Add an error message entry to delayed output

Parameters:



92
93
94
# File 'lib/berkshelf/formatters/json.rb', line 92

def error(message)
  output[:errors] << message
end

#install(cookbook, version, location) ⇒ Object

Add a Cookbook installation entry to delayed output

Parameters:



34
35
36
37
38
# File 'lib/berkshelf/formatters/json.rb', line 34

def install(cookbook, version, location)
  cookbooks[cookbook] ||= {}
  cookbooks[cookbook][:version] = version
  cookbooks[cookbook][:location] = location.to_s
end

#msg(message) ⇒ Object

Add a generic message entry to delayed output

Parameters:



85
86
87
# File 'lib/berkshelf/formatters/json.rb', line 85

def msg(message)
  output[:messages] << message
end

#package(cookbook, destination) ⇒ Object

Add a Cookbook package entry to delayed output

Parameters:



70
71
72
73
# File 'lib/berkshelf/formatters/json.rb', line 70

def package(cookbook, destination)
  cookbooks[cookbook] ||= {}
  cookbooks[cookbook][:destination] = destination
end

#show(cookbook) ⇒ Object

Output Cookbook info entry to delayed output

Parameters:



78
79
80
# File 'lib/berkshelf/formatters/json.rb', line 78

def show(cookbook)
  cookbooks[cookbook.cookbook_name] = cookbook.pretty_hash
end

#upload(cookbook, version, chef_api_url) ⇒ Object

Add a Cookbook upload entry to delayed output

Parameters:



60
61
62
63
64
# File 'lib/berkshelf/formatters/json.rb', line 60

def upload(cookbook, version, chef_api_url)
  cookbooks[cookbook] ||= {}
  cookbooks[cookbook][:version] = version
  cookbooks[cookbook][:uploaded_to] = chef_api_url
end

#use(cookbook, version, location = nil) ⇒ Object

Add a Cookbook use entry to delayed output

Parameters:



45
46
47
48
49
50
51
52
53
# File 'lib/berkshelf/formatters/json.rb', line 45

def use(cookbook, version, location = nil)
  cookbooks[cookbook] ||= {}
  cookbooks[cookbook][:version] = version

  if location && location.is_a?(PathLocation)
    cookbooks[cookbook][:metadata] = true if location.metadata?
    cookbooks[cookbook][:location] = location.relative_path
  end
end