Skip to content

Commit 5fbd417

Browse files
committed
Add config option to CLI
Fixes #478
1 parent 137b4be commit 5fbd417

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

lib/syntax_tree/cli.rb

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,26 @@ def run(item)
455455
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
456456
Read, format, and write back the source of the given files
457457
458+
--ignore-files=...
459+
A glob pattern to ignore files when processing. This can be specified
460+
multiple times to ignore multiple patterns.
461+
458462
--plugins=...
459463
A comma-separated list of plugins to load.
460464
461-
--print-width=NUMBER
465+
--print-width=...
462466
The maximum line width to use when formatting.
463467
464-
-e SCRIPT
468+
-e ...
465469
Parse an inline string.
466470
467-
--extension=EXTENSION
468-
A file extension matching the content passed in via STDIN or -e. Defaults to 'rb'
471+
--extension=...
472+
A file extension matching the content passed in via STDIN or -e.
473+
Defaults to '.rb'.
474+
475+
--config=...
476+
Path to a configuration file. Defaults to .streerc in the current
477+
working directory.
469478
HELP
470479

471480
# This represents all of the options that can be passed to the CLI. It is
@@ -563,8 +572,16 @@ class ConfigFile
563572

564573
attr_reader :filepath
565574

566-
def initialize
567-
@filepath = File.join(Dir.pwd, FILENAME)
575+
def initialize(filepath = nil)
576+
if filepath
577+
if File.readable?(filepath)
578+
@filepath = filepath
579+
else
580+
raise ArgumentError, "Invalid configuration file: #{filepath}"
581+
end
582+
else
583+
@filepath = File.join(Dir.pwd, FILENAME)
584+
end
568585
end
569586

570587
def exists?
@@ -582,8 +599,24 @@ class << self
582599
def run(argv)
583600
name, *arguments = argv
584601

585-
config_file = ConfigFile.new
586-
arguments.unshift(*config_file.arguments)
602+
# First, we need to check if there's a --config option specified
603+
# so we can use the custom config file path.
604+
config_filepath = nil
605+
arguments.each_with_index do |arg, index|
606+
if arg.start_with?("--config=")
607+
config_filepath = arg.split("=", 2)[1]
608+
arguments.delete_at(index)
609+
break
610+
elsif arg == "--config" && arguments[index + 1]
611+
config_filepath = arguments[index + 1]
612+
arguments.delete_at(index + 1)
613+
arguments.delete_at(index)
614+
break
615+
end
616+
end
617+
618+
config_file = ConfigFile.new(config_filepath)
619+
arguments = config_file.arguments.concat(arguments)
587620

588621
options = Options.new
589622
options.parse(arguments)

test/cli_test.rb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,48 @@ def test_plugin_args_with_config_file
308308
end
309309
end
310310

311+
def test_config_file_custom_path
312+
with_plugin_directory do |directory|
313+
plugin = directory.plugin("puts 'Custom config!'")
314+
config = <<~TXT
315+
--print-width=80
316+
--plugins=#{plugin}
317+
TXT
318+
319+
filepath = File.join(Dir.tmpdir, "#{SecureRandom.hex}.streerc")
320+
with_config_file(config, filepath) do
321+
contents = "#{"a" * 30} + #{"b" * 30}\n"
322+
result = run_cli("format", "--config=#{filepath}", contents: contents)
323+
324+
assert_equal("Custom config!\n#{contents}", result.stdio)
325+
end
326+
end
327+
end
328+
329+
def test_config_file_custom_path_space_separated
330+
with_plugin_directory do |directory|
331+
plugin = directory.plugin("puts 'Custom config space!'")
332+
config = <<~TXT
333+
--print-width=80
334+
--plugins=#{plugin}
335+
TXT
336+
337+
filepath = File.join(Dir.tmpdir, "#{SecureRandom.hex}.streerc")
338+
with_config_file(config, filepath) do
339+
contents = "#{"a" * 30} + #{"b" * 30}\n"
340+
result = run_cli("format", "--config", filepath, contents: contents)
341+
342+
assert_equal("Custom config space!\n#{contents}", result.stdio)
343+
end
344+
end
345+
end
346+
347+
def test_config_file_nonexistent_path
348+
assert_raises(ArgumentError) do
349+
run_cli("format", "--config=/nonexistent/path.streerc")
350+
end
351+
end
352+
311353
Result = Struct.new(:status, :stdio, :stderr, keyword_init: true)
312354

313355
private
@@ -342,8 +384,8 @@ def run_cli(command, *args, contents: :default)
342384
tempfile.unlink
343385
end
344386

345-
def with_config_file(contents)
346-
filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
387+
def with_config_file(contents, filepath = nil)
388+
filepath ||= File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
347389
File.write(filepath, contents)
348390

349391
yield

0 commit comments

Comments
 (0)