Skip to content

Commit 5218224

Browse files
committed
providng a for-file verbose option
1 parent 2309355 commit 5218224

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

lib/code_ownership/cli.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,54 @@ def self.for_file(argv)
111111
raise "Please pass in one file. Use `#{EXECUTABLE} for_file --help` for more info"
112112
end
113113

114-
puts CodeOwnership::Private::ForFileOutputBuilder.build(file_path: files.first, json: !!options[:json], verbose: !!options[:verbose])
114+
if options[:verbose]
115+
for_file_verbose(file: files.first, json: options[:json])
116+
else
117+
for_file_terse(file: files.first, json: options[:json])
118+
end
119+
end
120+
121+
def self.for_file_terse(file:, json:)
122+
team = CodeOwnership.for_file(file)
123+
124+
team_name = team&.name || 'Unowned'
125+
team_yml = team&.config_yml || 'Unowned'
126+
127+
if json
128+
json_output = {
129+
team_name: team_name,
130+
team_yml: team_yml
131+
}
132+
133+
puts json_output.to_json
134+
else
135+
puts <<~MSG
136+
Team: #{team_name}
137+
Team YML: #{team_yml}
138+
MSG
139+
end
140+
end
141+
142+
def self.for_file_verbose(file:, json:)
143+
verbose = CodeOwnership.for_file_verbose(file) || {team_name: 'Unowned', team_config_yml: 'Unowned', reasons: []}
144+
145+
if json
146+
json = {
147+
team_name: verbose[:team_name],
148+
team_yml: verbose[:team_config_yml],
149+
reasons: verbose[:reasons]
150+
}
151+
152+
puts json.to_json
153+
else
154+
messages = ["Team: #{verbose[:team_name]}", "Team YML: #{verbose[:team_config_yml]}"]
155+
if verbose[:reasons].any?
156+
messages << "Reasons:\n- #{verbose[:reasons].join("\n-")}"
157+
end
158+
messages.last << "\n"
159+
160+
puts messages.join("\n")
161+
end
115162
end
116163

117164
def self.for_team(argv)

spec/lib/code_ownership/cli_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def initialize
183183
expect(CodeOwnership::Cli).to receive(:puts).with(<<~MSG)
184184
Team: My Team
185185
Team YML: config/teams/my_team.yml
186-
Description:
186+
Reasons:
187187
- Owner specified in Team YML as an owned_glob `app/**/*.rb`
188188
MSG
189189
subject
@@ -220,17 +220,17 @@ def initialize
220220
subject
221221
end
222222

223-
context 'when run with --verbose' do
223+
context 'when run with multiple files' do
224224
let(:argv) { ['for_file', '--json', '--verbose', 'app/services/my_file.rb'] }
225225
it 'outputs JSONified information to the console' do
226226
json = {
227227
team_name: 'My Team',
228228
team_yml: 'config/teams/my_team.yml',
229-
description: ['Owner specified in Team YML as an owned_glob `app/**/*.rb`']
229+
reasons: ['Owner specified in Team YML as an owned_glob `app/**/*.rb`']
230230
}
231231
expect(CodeOwnership::Cli).to receive(:puts).with(json.to_json)
232232
subject
233-
end
233+
end
234234
end
235235
end
236236

0 commit comments

Comments
 (0)