Module: Headdesk::CliCommands::Analyze

Includes:
Headdesk::CliCommand
Defined in:
lib/headdesk/cli_commands/analyze.rb

Overview

Analyze an APK/IPA file

Class Method Summary collapse

Methods included from Headdesk::CliCommand

each

Class Method Details

.included(thor) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/headdesk/cli_commands/analyze.rb', line 15

def self.included(thor)
  thor.class_eval do
    desc 'analyze [FILE]', 'Analyze an APK or IPA'
    method_option :path, type: :string
    method_option :json, type: :boolean
    def analyze(file = nil)
      # Make sure input file exsts, if specified
      unless !file || File.exist?(file)
        STDERR.puts "Could not find input file: #{file}"
        CLI.command_help(Thor::Base.shell.new, 'analyze')
        exit 1
      end

      # Unpack APK if needed
      path = options[:path]
      tmp_dir = nil
      if file
        path = tmp_dir = Dir.mktmpdir
        Headdesk::ApkTool.unpack_to(file, tmp_dir)
      end

      # Make sure path exists
      unless Dir.exist?(path)
        STDERR.puts "Could not find path: #{path}"
        CLI.command_help(Thor::Base.shell.new, 'analyze')
        exit 1
      end

      # analyze
      begin
        report = Headdesk::Analyze.at(path)

        if options[:json]
          STDOUT.puts report.to_json
        else
          STDOUT.puts report.to_s
          print_update_message unless Headdesk::Versions.latest_version?
        end
      rescue CliError => cli_err
        STDERR.puts cli_err.message
        CLI.command_help(Thor::Base.shell.new, 'analyze')
        exit 1
      rescue StandardError => err
        STDERR.puts err.message.red
        STDERR.puts err.backtrace.ai
        exit 1
      end
    ensure
      FileUtils.remove_entry(tmp_dir) if tmp_dir
    end
  end
end