From 270c9e9af149d5d2c3db2ef6182c1fc31f1ffb59 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Wed, 11 Sep 2024 15:08:56 +0900 Subject: [PATCH] refactor: stop using meaningless try/result (#1117) --- lib/mihari/commands/alert.rb | 19 +++++++++---------- lib/mihari/commands/artifact.rb | 6 +++--- lib/mihari/commands/rule.rb | 6 +++--- lib/mihari/commands/search.rb | 13 +++++-------- lib/mihari/commands/tag.rb | 4 ++-- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/mihari/commands/alert.rb b/lib/mihari/commands/alert.rb index 678be7301..6f582d23d 100644 --- a/lib/mihari/commands/alert.rb +++ b/lib/mihari/commands/alert.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/mihari/commands/artifact.rb b/lib/mihari/commands/artifact.rb index be30b2927..e2e9b2273 100644 --- a/lib/mihari/commands/artifact.rb +++ b/lib/mihari/commands/artifact.rb @@ -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 @@ -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" @@ -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 diff --git a/lib/mihari/commands/rule.rb b/lib/mihari/commands/rule.rb index d2047661a..73c475d50 100644 --- a/lib/mihari/commands/rule.rb +++ b/lib/mihari/commands/rule.rb @@ -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 @@ -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" @@ -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 diff --git a/lib/mihari/commands/search.rb b/lib/mihari/commands/search.rb index 8f39d4999..d1f2345a4 100644 --- a/lib/mihari/commands/search.rb +++ b/lib/mihari/commands/search.rb @@ -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 diff --git a/lib/mihari/commands/tag.rb b/lib/mihari/commands/tag.rb index a432edf11..1df8f932a 100644 --- a/lib/mihari/commands/tag.rb +++ b/lib/mihari/commands/tag.rb @@ -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 @@ -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