Class: BaseExecutor

Inherits:
Object
  • Object
show all
Extended by:
FormatHelpers
Defined in:
lib/veye/base_executor.rb

Overview

Base class that will bring many helpers into command classes.

Class Method Summary collapse

Methods included from FormatHelpers

format_exists?, formats_attached?, supported_format?

Class Method Details

.catch_request_error(response, msg) ⇒ Object

OBSOLETE: use valid_response?



33
34
35
# File 'lib/veye/base_executor.rb', line 33

def self.catch_request_error(response, msg)
  valid_response?(response, msg)
end

.filter_dependencies(deps, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/veye/base_executor.rb', line 66

def self.filter_dependencies(deps, options = {})
  return deps if ( options[:all] == true )

  deps.keep_if {|d| d['outdated'] == true}
 
  #if any of filter flags are not selected then return only outdated deps

  if (options[:major] or options[:minor] or options[:patch]) == false
    return deps
  end

  filtered_deps = []
  if options.fetch(:major, false) == true
    deps.each {|d| filtered_deps << d if d[:upgrade][:dv_major] > 0}
  end

  #add only package which has minor change and may have patch changes
  if options.fetch(:minor, false) == true
    deps.each do |d|
      if d[:upgrade][:dv_minor] > 0 and d[:upgrade][:dv_major] == 0
        filtered_deps << d
      end
    end
  end

  #add only packages which has only patches, and skip all the minor and major changes
  if options.fetch(:patch, false)  == true
    deps.each do |d|
      if d[:upgrade][:dv_patch] > 0 and d[:upgrade][:dv_minor] == 0 and d[:upgrade][:dv_major] == 0
        filtered_deps << d
      end
    end
  end

  #remove duplicates if user attached multiple filter flags
  already_seen_keys = Set.new
  filtered_deps.reduce([]) do |acc, dep|
    next unless dep.has_key?('prod_key')

    unless already_seen_keys.include?(dep['prod_key'])
      acc << dep
      already_seen_keys << dep['prod_key']
    end

    acc
  end
end

.get_formatter(output_formats, options) ⇒ Object



137
138
139
140
141
142
# File 'lib/veye/base_executor.rb', line 137

def self.get_formatter(output_formats, options)
  format = options[:format]
  format ||= 'pretty'
  #return if format == 'json' #dependecy json format is handled by results formatter
  output_formats[format] if supported_format?(output_formats, format)
end

.process_dependencies(proj_deps, options) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/veye/base_executor.rb', line 114

def self.process_dependencies(proj_deps, options)
  proj_deps.to_a.map do |dep|
    dep[:upgrade] = Veye::Project.calc_upgrade_heuristics(dep['version_requested'], dep['version_current'])
    dep
  end

  proj_deps = filter_dependencies(proj_deps, options)
  proj_deps = sort_dependencies_by_upgrade_complexity(proj_deps)

  proj_deps.to_a
end

.show_bulk_dependencies(output_formats, results, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/veye/base_executor.rb', line 48

def self.show_bulk_dependencies(output_formats, results, options)
  formatter = get_formatter(output_formats, options)
  return if formatter.nil?

  formatter.before
  results.each do |filename, project|
    sorted_deps = process_dependencies(project['dependencies'].to_a, options)

    formatter.format(sorted_deps.to_a, filename)
  end

  formatter.after
end

.show_dependencies(output_formats, proj_deps, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/veye/base_executor.rb', line 37

def self.show_dependencies(output_formats, proj_deps, options)
  formatter = get_formatter(output_formats, options)
  return if formatter.nil?

  sorted_deps = process_dependencies(proj_deps.to_a, options)
  
  formatter.before
  formatter.format sorted_deps.to_a
  formatter.after
end

.show_message(results, success_msg, fail_msg) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/veye/base_executor.rb', line 127

def self.show_message(results, success_msg, fail_msg)
  if results.success
    printf "#{success_msg}\n".color(:green)
  else
    printf("Error: %s\n%s\n",
           fail_msg.color(:red),
           response.data['error'])
  end
end

.show_results(output_formats, results, options = {}, paging = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/veye/base_executor.rb', line 7

def self.show_results(output_formats, results, options = {}, paging = nil)
  formatter = get_formatter(output_formats, options)
  return if formatter.nil?

  formatter.before

  #if command uses s.o windowed output aka show only part of the items list
  if options.has_key?(:n) or options.has_key?(:from)
    formatter.format(results, options[:n].to_i, options[:from].to_i)
  else
    formatter.format(results)
  end
  formatter.after(paging, options[:pagination])
end

.sort_dependencies_by_upgrade_complexity(deps) ⇒ Object



62
63
64
# File 'lib/veye/base_executor.rb', line 62

def self.sort_dependencies_by_upgrade_complexity(deps)
  deps.to_a.sort {|a, b| b[:upgrade][:dv_score] <=> a[:upgrade][:dv_score]}
end

.valid_response?(response, msg) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/veye/base_executor.rb', line 22

def self.valid_response?(response, msg)
  if response.nil? || response.success != true
    printf "#{msg.to_s.color(:red)}: #{response.data}\n"
    Veye.logger.error "API returned error - #{response.code}, #{response.data}"
    return false
  end

  response.success
end