Sha256: 24dc5e06eee1ea27e9f2bf055178fdcd5c56a9b1113f4e2f8bc7ae6db0808939

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "pathname"

module Mihari
  module Commands
    module Rule
      class << self
        def included(thor)
          thor.class_eval do
            desc "validate [PATH]", "Validate a rule file"
            #
            # Validate format of a rule
            #
            # @param [String] path
            #
            def validate(path)
              rule = Structs::Rule.from_path_or_id(path)

              begin
                rule.validate!
                Mihari.logger.info "Valid format. The input is parsed as the following:"
                Mihari.logger.info rule.data.to_yaml
              rescue RuleValidationError
                nil
              end
            end

            desc "init [PATH]", "Initialize a new rule file"
            #
            # Initialize a new rule file
            #
            # @param [String] path
            #
            #
            def init(path = "./rule.yml")
              warning = "#{path} exists. Do you want to overwrite it? (y/n)"
              return if Pathname(path).exist? && !(yes? warning)

              initialize_rule path

              Mihari.logger.info "A new rule file has been initialized: #{path}."
            end

            no_commands do
              #
              # @return [Mihari::Structs::Rule]
              #
              def rule_template
                Structs::Rule.from_path File.expand_path("../templates/rule.yml.erb", __dir__)
              end

              #
              # Create a new rule
              #
              # @param [String] path
              # @param [Dry::Files] files
              #
              # @return [nil]
              #
              def initialize_rule(path, files = Dry::Files.new)
                files.write(path, rule_template.yaml)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mihari-5.3.1 lib/mihari/commands/rule.rb
mihari-5.3.0 lib/mihari/commands/rule.rb
mihari-5.2.4 lib/mihari/commands/rule.rb
mihari-5.2.3 lib/mihari/commands/rule.rb