Skip all config when use with --gen-config.

This commit is contained in:
Billy.Zheng 2023-03-08 21:35:31 +08:00
parent 3fbbe3986e
commit d45285d1c9
3 changed files with 15 additions and 3 deletions

View file

@ -82,6 +82,12 @@ module Ameba::Cli
c.colors?.should be_true
end
it "ignores --config if --gen-config flag passed" do
c = Cli.parse_args %w(--gen-config --config my_config.yml)
c.formatter.should eq :todo
c.skip_reading_config?.should be_true
end
describe "-e/--explain" do
it "configures file/line/column" do
c = Cli.parse_args %w(--explain src/file.cr:3:5)

View file

@ -14,7 +14,7 @@ module Ameba::Cli
raise "Invalid usage: Cannot explain an issue and autocorrect at the same time."
end
config = Config.load opts.config, opts.colors?
config = Config.load path: opts.config, colors: opts.colors?, skip_reading_config: opts.skip_reading_config?
config.autocorrect = autocorrect
if globs = opts.globs
@ -51,6 +51,7 @@ module Ameba::Cli
property except : Array(String)?
property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)?
property fail_level : Severity?
property? skip_reading_config = false
property? rules = false
property? all = false
property? colors = true
@ -105,6 +106,7 @@ module Ameba::Cli
parser.on("--gen-config",
"Generate a configuration file acting as a TODO list") do
opts.formatter = :todo
opts.skip_reading_config = true
end
parser.on("--fail-level SEVERITY",

View file

@ -106,9 +106,13 @@ class Ameba::Config
# ```
# config = Ameba::Config.load
# ```
def self.load(path = nil, colors = true)
def self.load(path = nil, colors = true, skip_reading_config = false)
Colorize.enabled = colors
content = read_config(path) || "{}"
content = if skip_reading_config
"{}"
else
read_config(path) || "{}"
end
Config.new YAML.parse(content)
rescue e
raise "Config file is invalid: #{e.message}"