Skip to content

Commit 986c803

Browse files
committed
change formatter values from symbols to strings
1 parent 6d86fff commit 986c803

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

lib/bashly/libraries/settings/settings.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ strict: false
5252
tab_indent: false
5353

5454
# Choose a post-processor for the generated script:
55-
# formatter: :internal # Use Bashly's internal formatter (compacts newlines)
56-
# formatter: :none # Disable all post-processing
57-
# formatter: :shfmt # Use shfmt if installed
58-
# formatter: shfmt -mn # Use any other string, assuming the command accepts
59-
# # the script as stdin and outputs it to stdout
60-
formatter: :internal
55+
# formatter: internal # Use Bashly's internal formatter (compacts newlines)
56+
# formatter: external # Run the external command `shfmt --case-indent --indent 2`
57+
# formatter: none # Disable formatting entirely
58+
# formatter: <string> # Use a custom shell command to format the script.
59+
# # The command will receive the script via stdin and
60+
# # must output the result to stdout.
61+
# # Example: shfmt --minify
62+
formatter: internal
63+
6164

6265
#-------------------------------------------------------------------------------
6366
# INTERFACE OPTIONS

lib/bashly/script/formatter.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
module Bashly
55
module Script
66
class Formatter
7-
MODES = %i[internal none shfmt]
8-
97
attr_reader :script, :mode
108

119
def initialize(script, mode: nil)
1210
@script = script
13-
@mode = mode || :internal
11+
@mode = mode&.to_s || 'internal'
1412
end
1513

1614
def formatted_script
1715
case mode
18-
when :internal then script.gsub(/\s+\n/m, "\n\n")
19-
when :shfmt then shfmt_result
20-
when :none then script
16+
when 'internal' then script.gsub(/\s+\n/m, "\n\n")
17+
when 'external' then shfmt_result
18+
when 'none' then script
2119
else custom_formatter_result mode
2220
end
2321
end
@@ -34,10 +32,10 @@ def custom_formatter_result(command)
3432
begin
3533
output, error, status = Open3.capture3(*command, stdin_data: script)
3634
rescue Errno::ENOENT
37-
raise Error, "Command not found: `#{command.join(' ')}`"
35+
raise Error, "Command not found: g`#{command.first}`"
3836
end
3937

40-
raise Error, "Failed running `#{command.join(' ')}`:\n\n#{error}" unless status.success?
38+
raise Error, "Failed running g`#{Shellwords.join command}`:\n\n#{error}" unless status.success?
4139

4240
output
4341
end

schemas/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@
220220
{
221221
"type": "string",
222222
"enum": [
223-
":internal",
224-
":none",
225-
":shfmt"
223+
"internal",
224+
"none",
225+
"external"
226226
]
227227
},
228228
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::Error:"Failed running `shfmt --diff`:\n\n">
1+
#<Bashly::Error:"Failed running g`shfmt --diff`:\n\n">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::Error: Command not found: `my_glorious_formatter`>
1+
#<Bashly::Error: Command not found: g`my_formatter`>

spec/bashly/script/formatter_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
expect(subject.formatted_script).to match_approval 'formatter/internal'
1111
end
1212

13-
context 'with mode: :none' do
14-
let(:mode) { :none }
13+
context 'with mode: none' do
14+
let(:mode) { 'none' }
1515

1616
it 'returns the original script' do
1717
expect(subject.formatted_script).to eq script
1818
end
1919
end
2020

21-
context 'with mode: :shfmt' do
22-
let(:mode) { :shfmt }
21+
context 'with mode: external' do
22+
let(:mode) { 'external' }
2323

2424
it 'uses shfmt to format the script' do
2525
expect(subject.formatted_script).to match_approval 'formatter/shfmt'
@@ -35,7 +35,7 @@
3535
end
3636

3737
context 'when the external command does not exist' do
38-
let(:mode) { 'my_glorious_formatter' }
38+
let(:mode) { 'my_formatter' }
3939

4040
it 'raises a Bashly::Error' do
4141
expect { subject.formatted_script }.to raise_approval 'formatter/error-not-found'

support/schema/settings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ properties:
189189
anyOf:
190190
- type: string
191191
enum:
192-
- :internal
193-
- :none
194-
- :shfmt
192+
- internal
193+
- none
194+
- external
195195
- type: string
196196
minLength: 1
197197
default: :internal

0 commit comments

Comments
 (0)