Skip to content

Commit

Permalink
Merge pull request #466 from nobodywasishere/nobody/stdin-filename
Browse files Browse the repository at this point in the history
Add cli flag to read from STDIN
  • Loading branch information
veelenga authored Aug 21, 2024
2 parents a42b218 + f29cffa commit f72f0b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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?
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: 10 additions & 1 deletion 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,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.
Expand Down

0 comments on commit f72f0b1

Please sign in to comment.