Class: ChefCLI::Command::Diff

Inherits:
Base
  • Object
show all
Includes:
ChefCLI::Configurable, Policyfile::StorageConfigDelegation
Defined in:
lib/chef-cli/command/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Policyfile::StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Methods included from ChefCLI::Configurable

#chef_config, #chefcli_config, #config_loader, #default_chef_server_http_client, #generator_config, #knife_config, #reset_config!

Methods inherited from Base

#check_license_acceptance, #needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(*args) ⇒ Diff

Returns a new instance of Diff.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef-cli/command/diff.rb', line 107

def initialize(*args)
  super

  @ui = UI.new

  @old_base = nil
  @new_base = nil
  @policyfile_relative_path = nil
  @storage_config = nil
  @http_client = nil

  @old_lock = nil
  @new_lock = nil
end

Instance Attribute Details

#new_base ⇒ Object (readonly)

Returns the value of attribute new_base.



103
104
105
# File 'lib/chef-cli/command/diff.rb', line 103

def new_base
  @new_base
end

#old_base ⇒ Object (readonly)

Returns the value of attribute old_base.



102
103
104
# File 'lib/chef-cli/command/diff.rb', line 102

def old_base
  @old_base
end

#storage_config ⇒ Object (readonly)

Returns the value of attribute storage_config.



105
106
107
# File 'lib/chef-cli/command/diff.rb', line 105

def storage_config
  @storage_config
end

#ui ⇒ Object

Returns the value of attribute ui.



100
101
102
# File 'lib/chef-cli/command/diff.rb', line 100

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/chef-cli/command/diff.rb', line 199

def apply_params!(params)
  remaining_args = parse_options(params)

  if no_comparison_specified?(remaining_args)
    ui.err("No comparison specified")
    ui.err("")
    ui.err(opt_parser)
    false
  elsif conflicting_args_and_opts_given?(remaining_args)
    ui.err("Conflicting arguments and options: git and Policy Group comparisons cannot be mixed")
    ui.err("")
    ui.err(opt_parser)
    false
  elsif conflicting_git_options_given?
    ui.err("Conflicting git options: --head and --git are exclusive")
    ui.err("")
    ui.err(opt_parser)

    false
  elsif config[:head]
    set_policyfile_path_from_args(remaining_args)
    @old_base = Policyfile::ComparisonBase::Git.new("HEAD", policyfile_lock_relpath)
    @new_base = Policyfile::ComparisonBase::Local.new(policyfile_lock_relpath)
    true
  elsif config[:git]
    set_policyfile_path_from_args(remaining_args)
    parse_git_comparison(config[:git])
  else
    set_policyfile_path_from_args(remaining_args)
    parse_server_comparison(remaining_args)
  end
end

#comparing_policy_groups? ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/chef-cli/command/diff.rb', line 281

def comparing_policy_groups?
  !(config[:git] || config[:head])
end

#conflicting_args_and_opts_given?(args) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/chef-cli/command/diff.rb', line 273

def conflicting_args_and_opts_given?(args)
  (config[:git] || config[:head]) && policy_group_comparison?(args)
end

#conflicting_git_options_given? ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/chef-cli/command/diff.rb', line 277

def conflicting_git_options_given?
  config[:git] && config[:head]
end

#debug? ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/chef-cli/command/diff.rb', line 122

def debug?
  !!config[:debug]
end

#differ(ui = self.ui()) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/chef-cli/command/diff.rb', line 156

def differ(ui = self.ui())
  Policyfile::Differ.new(old_name: old_base.name,
                         old_lock: old_lock,
                         new_name: new_base.name,
                         new_lock: new_lock,
                         ui: ui)
end

#handle_error(error) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/chef-cli/command/diff.rb', line 135

def handle_error(error)
  ui.err("Error: #{error.message}")
  if error.respond_to?(:reason)
    ui.err("Reason: #{error.reason}")
    ui.err("")
    ui.err(error.extended_error_info) if debug?
    ui.err(error.cause.backtrace.join("\n")) if debug?
  end
end

#http_client ⇒ Object



164
165
166
167
168
# File 'lib/chef-cli/command/diff.rb', line 164

def http_client
  @http_client ||= Chef::ServerAPI.new(chef_config.chef_server_url,
                                       signing_key_filename: chef_config.client_key,
                                       client_name: chef_config.node_name)
end

#local_lock ⇒ Object



184
185
186
# File 'lib/chef-cli/command/diff.rb', line 184

def local_lock
  @local_lock ||= local_lock_comparison_base.lock
end

#local_lock_comparison_base ⇒ Object

ComparisonBase for the local lockfile. This is used to get the policy_name which is needed to query the server for the lockfile of a particular policy_group.



191
192
193
# File 'lib/chef-cli/command/diff.rb', line 191

def local_lock_comparison_base
  Policyfile::ComparisonBase::Local.new(policyfile_lock_relpath)
end

#materialize_locks ⇒ Object



308
309
310
311
# File 'lib/chef-cli/command/diff.rb', line 308

def materialize_locks
  @old_lock = old_base.lock
  @new_lock = new_base.lock
end

#new_lock ⇒ Object



175
176
177
178
# File 'lib/chef-cli/command/diff.rb', line 175

def new_lock
  materialize_locks unless @new_lock
  @new_lock
end

#no_comparison_specified?(args) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/chef-cli/command/diff.rb', line 269

def no_comparison_specified?(args)
  !policy_group_comparison?(args) && !config[:head] && !config[:git]
end

#old_lock ⇒ Object



170
171
172
173
# File 'lib/chef-cli/command/diff.rb', line 170

def old_lock
  materialize_locks unless @old_lock
  @old_lock
end

#parse_git_comparison(git_ref) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/chef-cli/command/diff.rb', line 251

def parse_git_comparison(git_ref)
  if git_ref.include?("...")
    old_ref, new_ref, *extra = git_ref.split("...")
    @old_base, @new_base = [old_ref, new_ref].map do |r|
      Policyfile::ComparisonBase::Git.new(r, policyfile_lock_relpath)
    end

    unless extra.empty?
      ui.err("Unable to parse git comparison `#{git_ref}`. Only 2 references can be specified.")
      return false
    end
  else
    @old_base = Policyfile::ComparisonBase::Git.new(git_ref, policyfile_lock_relpath)
    @new_base = Policyfile::ComparisonBase::Local.new(policyfile_lock_relpath)
  end
  true
end

#parse_server_comparison(args) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/chef-cli/command/diff.rb', line 232

def parse_server_comparison(args)
  comparison_string = args.last
  if comparison_string.include?("...")
    old_pgroup, new_pgroup, *extra = comparison_string.split("...")
    @old_base, @new_base = [old_pgroup, new_pgroup].map do |g|
      Policyfile::ComparisonBase::PolicyGroup.new(g, policy_name, http_client)
    end

    unless extra.empty?
      ui.err("Unable to parse policy group comparison `#{comparison_string}`. Only 2 references can be specified.")
      return false
    end
  else
    @old_base = Policyfile::ComparisonBase::PolicyGroup.new(comparison_string, policy_name, http_client)
    @new_base = Policyfile::ComparisonBase::Local.new(policyfile_lock_relpath)
  end
  true
end

#policy_group_comparison?(args) ⇒ Boolean

Try to detect if the only argument given is a policyfile path. This is necessary because we support an optional argument with the path to the ruby policyfile. It would be easier if we used an option like -f, but that would be inconsistent with other commands (chef install, chef push, etc.).

Returns:

  • (Boolean)


290
291
292
293
294
# File 'lib/chef-cli/command/diff.rb', line 290

def policy_group_comparison?(args)
  return false if args.empty?
  return true if args.size > 1
  !(args.first =~ /\.rb\Z/)
end

#policy_name ⇒ Object



180
181
182
# File 'lib/chef-cli/command/diff.rb', line 180

def policy_name
  local_lock["name"]
end

#policyfile_lock_relpath ⇒ Object



195
196
197
# File 'lib/chef-cli/command/diff.rb', line 195

def policyfile_lock_relpath
  storage_config.policyfile_lock_filename
end


145
146
147
148
149
150
151
152
153
154
# File 'lib/chef-cli/command/diff.rb', line 145

def print_diff
  # eagerly evaluate locks so we hit any errors before we've entered
  # pagerland. Also, git commands behave weirdly when run while the pager
  # is active, doing this eagerly also avoids that issue
  materialize_locks
  Pager.new(enable_pager: config[:pager]).with_pager do |pager|
    differ = differ(pager.ui)
    differ.run_report
  end
end

#run(params = []) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/chef-cli/command/diff.rb', line 126

def run(params = [])
  return 1 unless apply_params!(params)
  print_diff
  0
rescue PolicyfileServiceError => e
  handle_error(e)
  1
end

#set_policyfile_path_from_args(args) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/chef-cli/command/diff.rb', line 296

def set_policyfile_path_from_args(args)
  policyfile_relative_path =
    if !comparing_policy_groups?
      args.first || "Policyfile.rb"
    elsif args.size == 1
      "Policyfile.rb"
    else
      args.first
    end
  @storage_config = Policyfile::StorageConfig.new.use_policyfile(policyfile_relative_path)
end