Skip to content

Add config option to CLI #523

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 41 additions & 8 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,26 @@ def run(item)
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Read, format, and write back the source of the given files

--ignore-files=...
A glob pattern to ignore files when processing. This can be specified
multiple times to ignore multiple patterns.

--plugins=...
A comma-separated list of plugins to load.

--print-width=NUMBER
--print-width=...
The maximum line width to use when formatting.

-e SCRIPT
-e ...
Parse an inline string.

--extension=EXTENSION
A file extension matching the content passed in via STDIN or -e. Defaults to 'rb'
--extension=...
A file extension matching the content passed in via STDIN or -e.
Defaults to '.rb'.

--config=...
Path to a configuration file. Defaults to .streerc in the current
working directory.
HELP

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

attr_reader :filepath

def initialize
@filepath = File.join(Dir.pwd, FILENAME)
def initialize(filepath = nil)
if filepath
if File.readable?(filepath)
@filepath = filepath
else
raise ArgumentError, "Invalid configuration file: #{filepath}"
end
else
@filepath = File.join(Dir.pwd, FILENAME)
end
end

def exists?
Expand All @@ -582,8 +599,24 @@ class << self
def run(argv)
name, *arguments = argv

config_file = ConfigFile.new
arguments.unshift(*config_file.arguments)
# First, we need to check if there's a --config option specified
# so we can use the custom config file path.
config_filepath = nil
arguments.each_with_index do |arg, index|
if arg.start_with?("--config=")
config_filepath = arg.split("=", 2)[1]
arguments.delete_at(index)
break
elsif arg == "--config" && arguments[index + 1]
config_filepath = arguments[index + 1]
arguments.delete_at(index + 1)
arguments.delete_at(index)
break
end
end

config_file = ConfigFile.new(config_filepath)
arguments = config_file.arguments.concat(arguments)

options = Options.new
options.parse(arguments)
Expand Down
46 changes: 44 additions & 2 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,48 @@ def test_plugin_args_with_config_file
end
end

def test_config_file_custom_path
with_plugin_directory do |directory|
plugin = directory.plugin("puts 'Custom config!'")
config = <<~TXT
--print-width=80
--plugins=#{plugin}
TXT

filepath = File.join(Dir.tmpdir, "#{SecureRandom.hex}.streerc")
with_config_file(config, filepath) do
contents = "#{"a" * 30} + #{"b" * 30}\n"
result = run_cli("format", "--config=#{filepath}", contents: contents)

assert_equal("Custom config!\n#{contents}", result.stdio)
end
end
end

def test_config_file_custom_path_space_separated
with_plugin_directory do |directory|
plugin = directory.plugin("puts 'Custom config space!'")
config = <<~TXT
--print-width=80
--plugins=#{plugin}
TXT

filepath = File.join(Dir.tmpdir, "#{SecureRandom.hex}.streerc")
with_config_file(config, filepath) do
contents = "#{"a" * 30} + #{"b" * 30}\n"
result = run_cli("format", "--config", filepath, contents: contents)

assert_equal("Custom config space!\n#{contents}", result.stdio)
end
end
end

def test_config_file_nonexistent_path
assert_raises(ArgumentError) do
run_cli("format", "--config=/nonexistent/path.streerc")
end
end

Result = Struct.new(:status, :stdio, :stderr, keyword_init: true)

private
Expand Down Expand Up @@ -342,8 +384,8 @@ def run_cli(command, *args, contents: :default)
tempfile.unlink
end

def with_config_file(contents)
filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
def with_config_file(contents, filepath = nil)
filepath ||= File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
File.write(filepath, contents)

yield
Expand Down