Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add cli flag to read from STDIN #466

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ameba/cli/cmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down