Module: Mihari::Commands::JSON

Included in:
Mihari::CLI
Defined in:
lib/mihari/commands/json.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mihari/commands/json.rb', line 6

def self.included(thor)
  thor.class_eval do
    desc "import_from_json", "Give a JSON input via STDIN"
    def import_from_json(input = nil)
      with_error_handling do
        json = input || $stdin.gets.chomp
        raise ArgumentError, "Input not found: please give an input in a JSON format" unless json

        json = parse_as_json(json)
        raise ArgumentError, "Invalid input format: an input JSON data should have title, description and artifacts key" unless valid_json?(json)

        title = json["title"]
        description = json["description"]
        artifacts = json["artifacts"]
        tags = json["tags"] || []

        basic = Analyzers::Basic.new(title: title, description: description, artifacts: artifacts, source: "json", tags: tags)

        basic.ignore_old_artifacts = options["ignore_old_artifacts"] || false
        basic.ignore_threshold = options["ignore_threshold"] || 0

        basic.run
      end
    end

    no_commands do
      def parse_as_json(input)
        ::JSON.parse input
      rescue ::JSON::ParserError => _e
        nil
      end
    end
  end
end