Skip to content

Commit

Permalink
refactor: stop using meaningless try/result
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Sep 11, 2024
1 parent d3d7bf4 commit 4916b62
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
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

0 comments on commit 4916b62

Please sign in to comment.