Skip to content

Commit

Permalink
feat: add new --exclude-status option
Browse files Browse the repository at this point in the history
  • Loading branch information
ksg97031 committed Sep 21, 2024
1 parent 64e1928 commit 9130a61
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/_advanced/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ format: "plain"
# Whether to display HTTP status codes in the output
show_status: "false"

# Whether to exclude HTTP status codes from the output
exclude_status: ""

# Whether to include the path in the output
include_path: "false"

Expand Down
1 change: 1 addition & 0 deletions docs/_includes/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FLAGS:
--set-pvalue-json VALUE Specifies the value of the identified parameter for JSON data
--set-pvalue-path VALUE Specifies the value of the identified parameter for path parameters
--show-status Display HTTP status codes for discovered endpoints
--exclude-status Exclude specific HTTP status code
--include-path Include file path in the plain result
--no-color Disable color output
--no-log Displaying only the results
Expand Down
2 changes: 2 additions & 0 deletions src/completions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def generate_zsh_completion_script
'--set-pvalue-json[Specifies the value of the identified parameter for JSON data]:value:' \\
'--set-pvalue-path[Specifies the value of the identified parameter for path parameters]:value:' \\
'--show-status[Display HTTP status codes for discovered endpoints]' \\
'--exclude-status[Exclude specific HTTP status codes from the results]:status:' \\
'--include-path[Include file path in the plain result]' \\
'--no-color[Disable color output]' \\
'--no-log[Displaying only the results]' \\
Expand Down Expand Up @@ -60,6 +61,7 @@ def generate_bash_completion_script
--set-pvalue-json
--set-pvalue-path
--show-status
--exclude-status
--include-path
--no-color
--no-log
Expand Down
3 changes: 2 additions & 1 deletion src/config_initializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ConfigInitializer
symbolized_hash = parsed_yaml.transform_keys(&.to_s)

# Transform specific keys from "yes"/"no" to true/false for old version noir config
["color", "debug", "show_status", "include_path", "nolog", "send_req", "all_taggers"].each do |key|
["color", "debug", "show_status", "exclude_status", "include_path", "nolog", "send_req", "all_taggers"].each do |key|
if !symbolized_hash.has_key?(key)
symbolized_hash[key] = default_options[key]
elsif symbolized_hash[key] == "yes"
Expand Down Expand Up @@ -99,6 +99,7 @@ class ConfigInitializer
"exclude_techs" => YAML::Any.new(""),
"format" => YAML::Any.new("plain"),
"show_status" => YAML::Any.new(false),
"exclude_status" => YAML::Any.new(""),
"include_path" => YAML::Any.new(false),
"nolog" => YAML::Any.new(false),
"output" => YAML::Any.new(""),
Expand Down
23 changes: 20 additions & 3 deletions src/models/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class NoirRunner
add_path_parameters

# Set status code
if any_to_bool(@options["show_status"]) == true
if any_to_bool(@options["show_status"]) == true || any_to_bool(@options["exclude_status"]) != ""
update_status_codes
end

Expand Down Expand Up @@ -271,6 +271,19 @@ class NoirRunner
@logger.info "Updating status codes."
final = [] of Endpoint

exclude_status = [] of Int32
@options["exclude_status"].to_s.split(",").each do |code|
if code.size > 0
begin
exclude_status << code.strip.to_i
rescue
@logger.error "Invalid --exclude-status option: '#{code}'"
@logger.error "Please use comma separated numbers."
exit(1)
end
end
end

@endpoints.each do |endpoint|
begin
if endpoint.params.size > 0
Expand All @@ -296,7 +309,9 @@ class NoirRunner
read_timeout: 5.second
)
endpoint.details.status_code = response.status_code
final << endpoint
unless exclude_status.includes?(response.status_code)
final << endpoint
end
else
response = Crest::Request.execute(
method: get_symbol(endpoint.method),
Expand All @@ -307,7 +322,9 @@ class NoirRunner
read_timeout: 5.second
)
endpoint.details.status_code = response.status_code
final << endpoint
unless exclude_status.includes?(response.status_code)
final << endpoint
end
end
rescue e
@logger.error "Failed to get status code for #{endpoint.url} (#{e.message})."
Expand Down
8 changes: 8 additions & 0 deletions src/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ if noir_options["show_status"] == true && noir_options["url"] == ""
exit(1)
end

# Check URL
if noir_options["exclude_status"] != "" && noir_options["url"] == ""
STDERR.puts "ERROR: --exclude-status requires -u or --url flag.".colorize(:yellow)
STDERR.puts "Please use -u or --url to set the URL."
STDERR.puts "If you need help, use -h or --help."
exit(1)
end

# Run Noir
app = NoirRunner.new noir_options
start_time = Time.monotonic
Expand Down
3 changes: 3 additions & 0 deletions src/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ def run_options_parser
noir_options["show_status"] = YAML::Any.new(true)
end

parser.on "--exclude-status 404,500", "Exclude specific HTTP status codes" { |var| noir_options["exclude_status"] = YAML::Any.new(var) }

parser.on "--include-path", "Include file path in the plain result" do
noir_options["include_path"] = YAML::Any.new(true)
end

parser.on "--no-color", "Disable color output" do
noir_options["color"] = YAML::Any.new(false)
end

parser.on "--no-log", "Displaying only the results" do
noir_options["nolog"] = YAML::Any.new(true)
end
Expand Down
2 changes: 1 addition & 1 deletion src/output_builder/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OutputBuilderCommon < OutputBuilder
end
end

if any_to_bool(@options["show_status"]) == true
if any_to_bool(@options["show_status"]) == true || @options["exclude_status"] != ""
r_status = endpoint.details.status_code.to_s.colorize(:light_yellow).toggle(@is_color)
r_buffer += "\n ○ status: #{r_status}"
end
Expand Down

0 comments on commit 9130a61

Please sign in to comment.