Skip to content

Commit

Permalink
show warning message when BuildID not found
Browse files Browse the repository at this point in the history
  • Loading branch information
david942j committed Jun 23, 2017
1 parent f8cdc9c commit ec68f4b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
20 changes: 13 additions & 7 deletions lib/one_gadget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ class << self
# OneGadget.gadgets(build_id: '60131540dadc6796cab33388349e6e4e68692053')
def gadgets(file: nil, build_id: nil, details: false, force_file: false)
if build_id
OneGadget::Fetcher.from_build_id(build_id, details: details)
OneGadget::Fetcher.from_build_id(build_id, details: details) || OneGadget::Logger.not_found(build_id)
elsif file
file = OneGadget::Helper.abspath(file)
build_id = OneGadget::Helper.build_id_of(file)
if !force_file && build_id
gadgets = OneGadget::Fetcher.from_build_id(build_id, details: details, remote: false)
return gadgets unless gadgets.empty?
end
OneGadget::Fetcher.from_file(file, details: details)
gadgets = try_from_build(file, details) unless force_file
gadgets || OneGadget::Fetcher.from_file(file, details: details)
end
end

private

def try_from_build(file, details)
build_id = OneGadget::Helper.build_id_of(file)
return unless build_id
gadgets = OneGadget::Fetcher.from_build_id(build_id, details: details, remote: false)
return if gadgets.nil?
gadgets
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/one_gadget/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ module ClassMethods
# If needs to return the gadgets' constraints.
# @param [Boolean] remote
# When local not found, try search in latest version?
# @return [Array<Integer>, Array<OneGadget::Gadget::Gadget>]
# If +details+ is +false+, +Array<Integer>+ is returned, which
# contains offset only.
# @return [Array<Integer>, Array<OneGadget::Gadget::Gadget>, nil]
# +nil+ is returned if cannot find target id in database.
# If +details+ is +false+, +Array<Integer>+ is returned, which contains offset only.
# Otherwise, array of gadgets is returned.
def from_build_id(build_id, details: false, remote: true)
if (build_id =~ /\A#{OneGadget::Helper::BUILD_ID_FORMAT}\Z/).nil?
raise ArgumentError, format('invalid BuildID format: %p', build_id)
end
gadgets = OneGadget::Gadget.builds(build_id, remote: remote)
return if gadgets.nil?
return gadgets if details
gadgets.map(&:offset)
end
Expand Down
10 changes: 7 additions & 3 deletions lib/one_gadget/gadget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ module ClassMethods
# @param [String] build_id Desired build id.
# @param [Boolean] remote
# When local not found, try search in latest version?
# @return [Array<Gadget::Gadget>] Gadgets.
# @return [Array<Gadget::Gadget>?] Gadgets.
def builds(build_id, remote: true)
require_all if BUILDS.empty?
return BUILDS[build_id] if BUILDS.key?(build_id)
return [] unless remote
return build_not_found unless remote
# fetch remote builds
table = OneGadget::Helper.remote_builds.find { |c| c.include?(build_id) }
return [] if table.nil? # remote doesn't have this one either.
return build_not_found if table.nil? # remote doesn't have this one either.
# builds found in remote! Ask update gem and download remote gadgets.
OneGadget::Helper.ask_update(msg: 'The desired one-gadget can be found in lastest version!')
tmp_file = OneGadget::Helper.download_build(table)
Expand All @@ -80,6 +80,10 @@ def require_all
require dic
end
end

def build_not_found
nil
end
end
extend ClassMethods
end
Expand Down
1 change: 1 addition & 0 deletions lib/one_gadget/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def enable_color
normal_s: "\e[38;5;203m", # red
integer: "\e[38;5;189m", # light purple
reg: "\e[38;5;82m", # light green
warn: "\e[38;5;230m" # light yellow
}.freeze

# Wrapper color codes for pretty inspect.
Expand Down
28 changes: 20 additions & 8 deletions lib/one_gadget/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,33 @@ module OneGadget
# A logger for internal usage.
module Logger
@logger = ::Logger.new(STDOUT)
@logger.formatter = proc do |_severity, _datetime, _progname, msg|
@logger.formatter = proc do |severity, _datetime, _progname, msg|
prep = ' ' * 12
message = msg.lines.map.with_index do |str, i|
next str if i.zero?
prep + str
end
"[#{OneGadget::Helper.colorize('OneGadget', sev: :reg)}] #{message.join}"
color = case severity
when 'WARN' then :warn
when 'INFO' then :reg
end
"[#{OneGadget::Helper.colorize('OneGadget', sev: color)}] #{message.join}"
end

# The logger info.
# @param [String] msg
# Message to be logged.
# @return [void]
def self.info(msg)
@logger.info(msg)
module_function

# Show warning message of no such build id in database.
# @param [String] build_id
# Build ID.
def not_found(build_id)
warn("Cannot find BuildID [#{build_id}]")
[]
end

%i[info warn].each do |sym|
define_method(sym) do |msg|
@logger.send(sym, msg)
end
end
end
end
5 changes: 5 additions & 0 deletions spec/one_gadget_amd64_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@
OneGadget::Gadget::ClassMethods::BUILDS.delete(:a)
OneGadget::Gadget::ClassMethods::BUILDS[@build_id] = entry unless entry.nil?
end

it 'not found' do
expect(OneGadget.gadgets(build_id: @build_id.reverse)).to be_empty
expect(OneGadget::Gadget.builds(@build_id.reverse)).to be_nil
end
end
end

0 comments on commit ec68f4b

Please sign in to comment.