mirror of
https://github.com/inspec/inspec
synced 2024-11-23 21:23:29 +00:00
Add stack probe for detecting when run as CLI by Thor
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
parent
e90c6e35c4
commit
e96ddbf2cb
1 changed files with 34 additions and 0 deletions
34
lib/inspec/utils/telemetry/run_context.rb
Normal file
34
lib/inspec/utils/telemetry/run_context.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
module Inspec
|
||||
module Telemetry
|
||||
# Guesses the run context of InSpec - how were we invoked?
|
||||
class RunContextProbe
|
||||
def self.guess_run_context
|
||||
stack = caller_locations(4)
|
||||
return "cli" if run_by_thor?(stack)
|
||||
# audit-cookbook
|
||||
# kitchen-inspec
|
||||
# "unknown"
|
||||
end
|
||||
|
||||
|
||||
def self.run_by_thor?(stack)
|
||||
stack_match(stack: stack[-14..-9], path: "thor/command", label: "run") &&
|
||||
stack_match(stack: stack[-14..-9], path: "thor/invocation", label: "invoke_command")
|
||||
end
|
||||
|
||||
def self.stack_match(stack: nil, label: nil, path: nil)
|
||||
stack.any? do |frame|
|
||||
if label && path
|
||||
frame.label == label && frame.absolute_path.include?(path)
|
||||
elsif label
|
||||
frame.label == label
|
||||
elsif path
|
||||
frame.absolute_path.include?(path)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue