Skip to content

Commit

Permalink
Add set-value-*
Browse files Browse the repository at this point in the history
  • Loading branch information
hahwul committed Sep 18, 2024
1 parent cdb2d46 commit eeb6ef3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
8 changes: 7 additions & 1 deletion docs/_advanced/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ send_with_headers:
- "X-API-Key: ABCD1234"

# The value to set for pvalue
set_pvalue: ""
set_pvalue:
set_pvalue_header:
set_pvalue_cookie:
set_pvalue_query:
set_pvalue_form:
set_pvalue_json:
set_pvalue_path:

# The technologies to use
techs: ""
Expand Down
28 changes: 24 additions & 4 deletions src/config_initializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ class ConfigInitializer
end
end

symbolized_hash
# Transform specific keys from "" to [] of YAML::Any for old version noir config
["set_pvalue"].each do |key|
if symbolized_hash[key] == ""
symbolized_hash[key] = YAML::Any.new([] of YAML::Any)
end
end

final_options = default_options.merge(symbolized_hash) { |_, _, new_val| new_val }
final_options
rescue e : Exception
puts "Failed to read config file: #{e.message}"
puts "Using default config."
Expand All @@ -90,7 +98,13 @@ class ConfigInitializer
"send_proxy" => YAML::Any.new(""),
"send_req" => YAML::Any.new(false),
"send_with_headers" => YAML::Any.new([] of YAML::Any),
"set_pvalue" => YAML::Any.new(""),
"set_pvalue" => YAML::Any.new([] of YAML::Any),
"set_pvalue_header" => YAML::Any.new([] of YAML::Any),
"set_pvalue_cookie" => YAML::Any.new([] of YAML::Any),
"set_pvalue_query" => YAML::Any.new([] of YAML::Any),
"set_pvalue_form" => YAML::Any.new([] of YAML::Any),
"set_pvalue_json" => YAML::Any.new([] of YAML::Any),
"set_pvalue_path" => YAML::Any.new([] of YAML::Any),
"techs" => YAML::Any.new(""),
"url" => YAML::Any.new(""),
"use_filters" => YAML::Any.new([] of YAML::Any),
Expand Down Expand Up @@ -159,8 +173,14 @@ class ConfigInitializer
# e.g "Authorization: Bearer token"
send_with_headers:
# The value to set for pvalue
set_pvalue: "#{options["set_pvalue"]}"
# The value to set for pvalue (Array of strings)
set_pvalue:
set_pvalue_header:
set_pvalue_cookie:
set_pvalue_query:
set_pvalue_form:
set_pvalue_json:
set_pvalue_path:
# The technologies to use
techs: "#{options["techs"]}"
Expand Down
41 changes: 38 additions & 3 deletions src/models/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ class NoirRunner
tiny_tmp.params = [] of Param
endpoint.params.each do |param|
if !param.name.includes? " "
if @options["set_pvalue"] != ""
param.value = @options["set_pvalue"].to_s
end
param.value = apply_pvalue(param.param_type, param.name, param.value).to_s
tiny_tmp.params << param
end
end
Expand Down Expand Up @@ -143,6 +141,43 @@ class NoirRunner
@endpoints = final
end

def apply_pvalue(param_type, param_name, param_value) : String
case param_type
when "query"
pvalue_target = @options["set_pvalue_query"]
when "json"
pvalue_target = @options["set_pvalue_json"]
when "form"
pvalue_target = @options["set_pvalue_form"]
when "header"
pvalue_target = @options["set_pvalue_header"]
when "cookie"
pvalue_target = @options["set_pvalue_cookie"]
when "path"
pvalue_target = @options["set_pvalue_path"]
else
pvalue_target = @options["set_pvalue"]
end

pvalue_target.as_a.each do |pvalue|
if pvalue.to_s.includes? "="
splited = pvalue.to_s.split("=")
if splited[0] == param_name
return splited[1].to_s
end
elsif pvalue.to_s.includes? ":"
splited = pvalue.to_s.split(":")
if splited[0] == param_name
return splited[1].to_s
end
else
return pvalue.to_s
end
end

return param_value.to_s
end

def combine_url_and_endpoints
tmp = [] of Endpoint
target_url = @options["url"].to_s
Expand Down
29 changes: 28 additions & 1 deletion src/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,34 @@ def run_options_parser
parser.separator "\n OUTPUT:".colorize(:blue)
parser.on "-f FORMAT", "--format json", "Set output format\n * plain yaml json jsonl markdown-table\n * curl httpie oas2 oas3\n * only-url only-param only-header only-cookie only-tag" { |var| noir_options["format"] = YAML::Any.new(var) }
parser.on "-o PATH", "--output out.txt", "Write result to file" { |var| noir_options["output"] = YAML::Any.new(var) }
parser.on "--set-pvalue VALUE", "Specifies the value of the identified parameter" { |var| noir_options["set_pvalue"] = YAML::Any.new(var) }
parser.on "--set-pvalue VALUE", "Specifies the value of the identified parameter for all types" do |var|
append_to_yaml_array(noir_options, set_pvalue, var)
end

parser.on "--set-pvalue-header VALUE", "Specifies the value of the identified parameter for headers" do |var|
append_to_yaml_array(noir_options, set_pvalue_header, var)
end

parser.on "--set-pvalue-cookie VALUE", "Specifies the value of the identified parameter for cookies" do |var|
append_to_yaml_array(noir_options, set_pvalue_cookie, var)
end

parser.on "--set-pvalue-query VALUE", "Specifies the value of the identified parameter for query parameters" do |var|
append_to_yaml_array(noir_options, set_pvalue_query, var)
end

parser.on "--set-pvalue-form VALUE", "Specifies the value of the identified parameter for form data" do |var|
append_to_yaml_array(noir_options, set_pvalue_form, var)
end

parser.on "--set-pvalue-json VALUE", "Specifies the value of the identified parameter for JSON data" do |var|
append_to_yaml_array(noir_options, set_pvalue_json, var)
end

parser.on "--set-pvalue-path VALUE", "Specifies the value of the identified parameter for Path parameters" do |var|
append_to_yaml_array(noir_options, set_pvalue_path, var)
end

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

0 comments on commit eeb6ef3

Please sign in to comment.