diff --git a/src/ameba/cli/cmd.cr b/src/ameba/cli/cmd.cr index 5cb9bc0b8..f87030785 100644 --- a/src/ameba/cli/cmd.cr +++ b/src/ameba/cli/cmd.cr @@ -16,6 +16,7 @@ module Ameba::Cli config = Config.load opts.config, opts.colors?, opts.skip_reading_config? config.autocorrect = autocorrect + config.stdin_filename = opts.stdin_filename if globs = opts.globs config.globs = globs @@ -59,6 +60,7 @@ module Ameba::Cli property describe_rule : String? property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)? property fail_level : Severity? + property stdin_filename : String? property? skip_reading_config = false property? rules = false property? all = false @@ -140,6 +142,10 @@ module Ameba::Cli parser.on("--no-color", "Disable colors") do opts.colors = false end + + parser.on("--stdin-filename FILENAME", "Read source from STDIN") do |file| + opts.stdin_filename = file + end end opts diff --git a/src/ameba/config.cr b/src/ameba/config.cr index 195c70587..6f80fba21 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -86,6 +86,9 @@ class Ameba::Config # Returns `true` if correctable issues should be autocorrected. property? autocorrect = false + # Returns a filename if reading source file from STDIN. + property stdin_filename : String? = nil + @rule_groups : Hash(String, Array(Rule::Base)) # Creates a new instance of `Ameba::Config` based on YAML parameters. @@ -156,8 +159,14 @@ class Ameba::Config # config.sources # => list of sources pointing to files found by the wildcards # ``` def sources - (find_files_by_globs(globs) - find_files_by_globs(excluded)) + srcs = (find_files_by_globs(globs) - find_files_by_globs(excluded)) .map { |path| Source.new File.read(path), path } + + if file = stdin_filename + srcs << Source.new(STDIN.gets_to_end, file) + end + + srcs end # Returns a formatter to be used while inspecting files.