From d3c048b701df572483ec4b839025db2b9fe8fed1 Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Mon, 19 Aug 2024 13:44:07 -0400 Subject: [PATCH 1/3] Add cli/config option to read from STDIN --- src/ameba/cli/cmd.cr | 6 ++++++ src/ameba/config.cr | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ameba/cli/cmd.cr b/src/ameba/cli/cmd.cr index 5cb9bc0b8..941bad9ba 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? = nil 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..698a56829 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,12 @@ 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)) - .map { |path| Source.new File.read(path), path } + if file = stdin_filename + [Source.new(STDIN.gets_to_end, file)] + else + (find_files_by_globs(globs) - find_files_by_globs(excluded)) + .map { |path| Source.new File.read(path), path } + end end # Returns a formatter to be used while inspecting files. From 68553e63a1112883c2ad872a223403552255cd9c Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Mon, 19 Aug 2024 13:54:40 -0400 Subject: [PATCH 2/3] Update src/ameba/cli/cmd.cr Co-authored-by: Sijawusz Pur Rahnama --- src/ameba/cli/cmd.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ameba/cli/cmd.cr b/src/ameba/cli/cmd.cr index 941bad9ba..f87030785 100644 --- a/src/ameba/cli/cmd.cr +++ b/src/ameba/cli/cmd.cr @@ -60,7 +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? = nil + property stdin_filename : String? property? skip_reading_config = false property? rules = false property? all = false From f29cffa83b7efc3437419ecac9a6a5c83af4e39a Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Mon, 19 Aug 2024 22:59:35 -0400 Subject: [PATCH 3/3] Don't ignore files passed in via cli when reading from stdin --- src/ameba/config.cr | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ameba/config.cr b/src/ameba/config.cr index 698a56829..6f80fba21 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -159,12 +159,14 @@ class Ameba::Config # config.sources # => list of sources pointing to files found by the wildcards # ``` def sources + srcs = (find_files_by_globs(globs) - find_files_by_globs(excluded)) + .map { |path| Source.new File.read(path), path } + if file = stdin_filename - [Source.new(STDIN.gets_to_end, file)] - else - (find_files_by_globs(globs) - find_files_by_globs(excluded)) - .map { |path| Source.new File.read(path), path } + srcs << Source.new(STDIN.gets_to_end, file) end + + srcs end # Returns a formatter to be used while inspecting files.