Class: Hedra::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/hedra/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/hedra/cli.rb', line 50

def self.exit_on_failure?
  true
end

Instance Method Details

#audit(url) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hedra/cli.rb', line 90

def audit(url)
  setup_logging
  client = build_http_client
  analyzer = Analyzer.new

  begin
    response = client.get(url)
    result = analyzer.analyze(url, response.headers.to_h)

    if options[:json]
      output = JSON.pretty_generate(result)
      if options[:output]
        File.write(options[:output], output)
        say "Audit saved to #{options[:output]}", :green unless options[:quiet]
      else
        puts output
      end
    else
      print_detailed_result(result)
    end
  rescue StandardError => e
    log_error("Audit failed: #{e.message}")
    exit 1
  end
end

#compare(url1, url2) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hedra/cli.rb', line 143

def compare(url1, url2)
  setup_logging
  client = build_http_client
  analyzer = Analyzer.new

  begin
    response1 = client.get(url1)
    response2 = client.get(url2)

    result1 = analyzer.analyze(url1, response1.headers.to_h)
    result2 = analyzer.analyze(url2, response2.headers.to_h)

    print_comparison(result1, result2)
  rescue StandardError => e
    log_error("Comparison failed: #{e.message}")
    exit 1
  end
end

#export(format) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/hedra/cli.rb', line 165

def export(format)
  unless %w[json csv].include?(format)
    say "Invalid format: #{format}. Use json or csv.", :red
    exit 1
  end

  results = options[:input] ? JSON.parse(File.read(options[:input])) : []
  exporter = Exporter.new
  exporter.export(results, format, options[:output])
  say "Exported to #{options[:output]}", :green unless options[:quiet]
end

#scan(target) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hedra/cli.rb', line 64

def scan(target)
  setup_logging
  urls = options[:file] ? read_urls_from_file(target) : [target]

  client = build_http_client
  analyzer = Analyzer.new
  results = []

  with_concurrency(urls, options[:concurrency]) do |url|
    response = client.get(url)
    result = analyzer.analyze(url, response.headers.to_h)
    results << result
    print_result(result) unless options[:quiet] || options[:output]
  rescue StandardError => e
    log_error("Failed to scan #{url}: #{e.message}")
  end

  export_results(results) if options[:output]
end

#watch(url) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hedra/cli.rb', line 120

def watch(url)
  setup_logging
  client = build_http_client
  analyzer = Analyzer.new

  say "Watching #{url} every #{options[:interval]} seconds. Press Ctrl+C to stop.", :cyan

  loop do
    begin
      response = client.get(url)
      result = analyzer.analyze(url, response.headers.to_h)
      print_result(result)
    rescue StandardError => e
      log_error("Watch check failed: #{e.message}")
    end
    sleep options[:interval]
  end
rescue Interrupt
  say "\nStopped watching.", :yellow
end