Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: stop using meaningless try/result #1117

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/mihari/commands/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def included(thor)
#
def _search(q, page: 1, limit: 10)
filter = Structs::Filters::Search.new(q:, page:, limit:)
Services::AlertSearcher.result(filter).value!
Services::AlertSearcher.call filter
end
end

Expand All @@ -33,15 +33,14 @@ def _search(q, page: 1, limit: 10)
#
def create(path)
# @type [Mihari::Models::Alert]
alert = Dry::Monads::Try[StandardError] do
raise ArgumentError, "#{path} not found" unless Pathname(path).exist?
raise ArgumentError, "#{path} not found" unless Pathname(path).exist?

params = YAML.safe_load(
ERB.new(File.read(path)).result,
permitted_classes: [Date, Symbol]
)
alert = Services::AlertCreator.call(params)

params = YAML.safe_load(
ERB.new(File.read(path)).result,
permitted_classes: [Date, Symbol]
)
Services::AlertCreator.call params
end.value!
data = Entities::Alert.represent(alert)
puts JSON.pretty_generate(data.as_json)
end
Expand Down Expand Up @@ -103,7 +102,7 @@ def get(id)
# @param [Integer] id
#
def delete(id)
Services::AlertDestroyer.result(id).value!
Services::AlertDestroyer.call id
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mihari/commands/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def included(thor)
#
def _search(q, page: 1, limit: 10)
filter = Structs::Filters::Search.new(q:, page:, limit:)
Services::ArtifactSearcher.result(filter).value!
Services::ArtifactSearcher.call filter
end
end

Expand Down Expand Up @@ -82,7 +82,7 @@ def get(id)
# @param [Integer] id
#
def enrich(id)
Services::ArtifactEnricher.result(id).value!
Services::ArtifactEnricher.call id
end

desc "delete ID", "Delete an artifact"
Expand All @@ -91,7 +91,7 @@ def enrich(id)
# @param [Integer] id
#
def delete(id)
Services::ArtifactDestroyer.result(id).value!
Services::ArtifactDestroyer.call id
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mihari/commands/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def included(thor)
#
def _search(q, page: 1, limit: 10)
filter = Structs::Filters::Search.new(q:, page:, limit:)
Services::RuleSearcher.result(filter).value!
Services::RuleSearcher.call filter
end
end

Expand Down Expand Up @@ -70,7 +70,7 @@ def init(path = "./rule.yml")
warning = "Do you want to overwrite it? (y/n)"
return if Pathname(path).exist? && !(yes? warning)

Services::RuleInitializer.call(path)
Services::RuleInitializer.call path
end

desc "list QUERY", "List/search rules"
Expand Down Expand Up @@ -127,7 +127,7 @@ def get(id)
# @param [String] id
#
def delete(id)
Services::RuleDestroyer.result(id).value!
Services::RuleDestroyer.call id
end
end
end
Expand Down
13 changes: 5 additions & 8 deletions lib/mihari/commands/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ def search(path_or_id)
force_overwrite = options["force_overwrite"] || false
message = "Are you sure you want to overwrite this rule? (y/n)"

# @type [Mihari::Models::Alert]
alert = Dry::Monads::Try[StandardError] do
# @type [Mihari::Rule]
rule = Services::RuleBuilder.call(path_or_id)
# @type [Mihari::Rule]
rule = Services::RuleBuilder.call(path_or_id)
exit 0 if rule.diff? && !force_overwrite && !yes?(message)

exit 0 if rule.diff? && !force_overwrite && !yes?(message)
rule.update_or_create
alert = rule.call

rule.update_or_create
rule.call
end.value!
data = Entities::Alert.represent(alert)
puts JSON.pretty_generate(data.as_json)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mihari/commands/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def included(thor)
#
def _search(q, page: 1, limit: 10)
filter = Structs::Filters::Search.new(q:, page:, limit:)
Services::TagSearcher.result(filter).value!
Services::TagSearcher.call filter
end
end

Expand Down Expand Up @@ -71,7 +71,7 @@ def list_transform(q = "")
# @param [Integer] id
#
def delete(id)
Services::TagDestroyer.result(id).value!
Services::TagDestroyer.call id
end
end
end
Expand Down