Class: Berkshelf::JsonFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/berkshelf/formatters/json.rb

Instance Method Summary collapse

Methods inherited from BaseFormatter

formatter_method

Constructor Details

#initializeJsonFormatter

Returns a new instance of JsonFormatter.



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

def initialize
  @output = {
    cookbooks: [],
    errors: [],
    messages: [],
    warnings: [],
  }
  @cookbooks = {}

  Berkshelf.ui.mute { 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

  puts ::JSON.pretty_generate(output)
end

#deprecation(message) ⇒ Object



182
183
184
# File 'lib/berkshelf/formatters/json.rb', line 182

def deprecation(message)
  output[:warnings] << "DEPRECATED: #{message}"
end

#error(message) ⇒ Object

Add an error message entry to delayed output

Parameters:



171
172
173
# File 'lib/berkshelf/formatters/json.rb', line 171

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

#fetch(dependency) ⇒ Object

Parameters:



30
31
32
33
34
# File 'lib/berkshelf/formatters/json.rb', line 30

def fetch(dependency)
  cookbooks[dependency] ||= {}
  cookbooks[dependency][:version]  = dependency.locked_version.to_s
  cookbooks[dependency][:location] = dependency.location
end

#info(cookbook) ⇒ Object

Output Cookbook info entry to delayed output.

Parameters:



109
110
111
112
# File 'lib/berkshelf/formatters/json.rb', line 109

def info(cookbook)
  path = File.expand_path(cookbook.path)
  cookbooks[cookbook.cookbook_name] = { path: path }
end

#install(source, cookbook) ⇒ Object

Add a Cookbook installation entry to delayed output

Parameters:

  • source (Source)

    the source the dependency is being downloaded from

  • cookbook (RemoteCookbook)

    the cookbook to be downloaded



42
43
44
45
46
47
48
49
50
# File 'lib/berkshelf/formatters/json.rb', line 42

def install(source, cookbook)
  cookbooks[cookbook.name] ||= {}
  cookbooks[cookbook.name][:version] = cookbook.version

  unless source.default?
    cookbooks[cookbook.name][:api_source]    = source.uri
    cookbooks[cookbook.name][:location_path] = cookbook.location_path
  end
end

#list(dependencies) ⇒ Object

Output a list of cookbooks to delayed output

Parameters:



124
125
126
127
128
129
130
131
132
# File 'lib/berkshelf/formatters/json.rb', line 124

def list(dependencies)
  dependencies.each do |dependency, cookbook|
    cookbooks[dependency.name] ||= {}
    cookbooks[dependency.name][:version] = dependency.locked_version.to_s
    if dependency.location
      cookbooks[dependency.name][:location] = dependency.location
    end
  end
end

#msg(message) ⇒ Object

Add a generic message entry to delayed output

Parameters:



164
165
166
# File 'lib/berkshelf/formatters/json.rb', line 164

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

#outdated(hash) ⇒ Object

Output a list of outdated cookbooks and the most recent version to delayed output

Parameters:

  • hash (Hash)

    the list of outdated cookbooks in the format { ‘cookbook’ => { ‘supermarket.chef.io’ => #<Cookbook> } }



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/berkshelf/formatters/json.rb', line 93

def outdated(hash)
  hash.each do |name, info|
    info["remote"].each do |remote_source, remote_version|
      source = remote_source.uri.to_s

      cookbooks[name] ||= {}
      cookbooks[name][:local] = info["local"].to_s
      cookbooks[name][:remote] ||= {}
      cookbooks[name][:remote][source] = remote_version.to_s
    end
  end
end

#package(destination) ⇒ Object

Output a package message using

Parameters:



117
118
119
# File 'lib/berkshelf/formatters/json.rb', line 117

def package(destination)
  output[:messages] << "Cookbook(s) packaged to #{destination}"
end

#search(results) ⇒ Object

Ouput Cookbook search results to delayed output

Parameters:



145
146
147
148
149
150
# File 'lib/berkshelf/formatters/json.rb', line 145

def search(results)
  results.sort_by(&:name).each do |remote_cookbook|
    cookbooks[remote_cookbook.name] ||= {}
    cookbooks[remote_cookbook.name][:version] = remote_cookbook.version
  end
end

#show(cookbook) ⇒ Object

Output Cookbook path entry to delayed output

Parameters:



137
138
139
140
# File 'lib/berkshelf/formatters/json.rb', line 137

def show(cookbook)
  path = File.expand_path(cookbook.path)
  cookbooks[cookbook.cookbook_name] = { path: path }
end

#skipping(cookbook, conn) ⇒ Object

Add a Cookbook skip entry to delayed output

Parameters:



80
81
82
83
84
85
# File 'lib/berkshelf/formatters/json.rb', line 80

def skipping(cookbook, conn)
  name = cookbook.cookbook_name
  cookbooks[name] ||= {}
  cookbooks[name][:version] = cookbook.version
  cookbooks[name][:skipped] = true
end

#uploaded(cookbook, conn) ⇒ Object

Add a Cookbook upload entry to delayed output

Parameters:



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

def uploaded(cookbook, conn)
  name = cookbook.cookbook_name
  cookbooks[name] ||= {}
  cookbooks[name][:version] = cookbook.version
  cookbooks[name][:uploaded_to] = conn.server_url
end

#use(dependency) ⇒ Object

Add a Cookbook use entry to delayed output

Parameters:



55
56
57
58
59
60
61
62
63
# File 'lib/berkshelf/formatters/json.rb', line 55

def use(dependency)
  cookbooks[dependency.name] ||= {}
  cookbooks[dependency.name][:version] = dependency.cached_cookbook.version

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

#vendor(cookbook, destination) ⇒ Object

Add a vendor message to delayed output

Parameters:



156
157
158
159
# File 'lib/berkshelf/formatters/json.rb', line 156

def vendor(cookbook, destination)
  cookbook_destination = File.join(destination, cookbook.cookbook_name)
  msg("Vendoring #{cookbook.cookbook_name} (#{cookbook.version}) to #{cookbook_destination}")
end

#versionObject

Output the version of Berkshelf



4
5
6
# File 'lib/berkshelf/formatters/json.rb', line 4

def version
  @output = { version: Berkshelf::VERSION }
end

#warn(message) ⇒ Object

Add a warning message entry to delayed output

Parameters:



178
179
180
# File 'lib/berkshelf/formatters/json.rb', line 178

def warn(message)
  output[:warnings] << message
end