Read about our upcoming Code of Conduct on this issue

Commit a0409827 authored by Ash McKenzie's avatar Ash McKenzie
Browse files

Improve file update info/warnings

parent 5803ce3ba7cf
......@@ -38,7 +38,10 @@ module GDK
end
def recover_cmd_string
"cp -f '#{destination_file}' '#{source_file}'"
<<~CMD
cp -f '#{destination_file}' \\
'#{source_file}'
CMD
end
private
......
......@@ -11,6 +11,8 @@ module GDK
class ErbRenderer
attr_reader :source, :target
WAIT_WARNING_SECS = 5
def initialize(source, target, args = {})
@source = source
@target = target
......@@ -39,7 +41,6 @@ module GDK
if config.config_file_protected?(target)
warn_not_applied!
wait!
return
end
......@@ -60,25 +61,38 @@ module GDK
private
def warn_changes!(temp_file)
diff = Shellout.new(%W[git --no-pager diff --no-index #{colors_arg} -u #{target} #{temp_file}]).run
diff = Shellout.new(%W[git --no-pager diff --no-index #{colors_arg} -u #{target} #{temp_file}]).readlines[4..].join("\n")
GDK::Output.puts
GDK::Output.info("'#{target}' has incoming changes:")
GDK::Output.warn "Your '#{target}' contains changes. Here is the diff:"
GDK::Output.puts <<~DIFF
diff_output = <<~DIFF_OUTPUT
-------------------------------------------------------------------------------------------------------------
#{diff}
-------------------------------------------------------------------------------------------------------------
DIFF
DIFF_OUTPUT
GDK::Output.puts(diff_output, stderr: true)
end
def warn_not_applied!
GDK::Output.warn "The changes to '#{target}' have not been applied."
GDK::Output.puts <<~NOT_APPLIED
GDK::Output.warn "Changes to '#{target}' not applied because it's protected in gdk.yml."
action_options = <<~ACTION_OPTIONS
- To apply these changes, run:
rm #{target} && make #{target}
rm #{target} && make #{target}
- To silence this warning (at your own peril):
touch #{target}
touch #{target}
-------------------------------------------------------------------------------------------------------------
NOT_APPLIED
ACTION_OPTIONS
GDK::Output.puts(action_options, stderr: true)
GDK::Output.puts("Resuming in #{WAIT_WARNING_SECS} seconds..", stderr: true)
sleep(WAIT_WARNING_SECS)
end
def warn_overwritten!
......@@ -86,21 +100,12 @@ module GDK
GDK::Output.puts <<~OVERWRITTEN
#{backup.recover_cmd_string}
If you want to protect this file from being overwritten, see:
https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/configuration.md#overwriting-configuration-files
-------------------------------------------------------------------------------------------------------------
OVERWRITTEN
end
def wait!
GDK::Output.puts <<~WAIT
... Waiting 5 seconds for previous warning to be noticed.
WAIT
sleep 5
end
def backup
@backup ||= Backup.new(target)
end
......
......@@ -97,7 +97,7 @@ RSpec.describe GDK::Backup do
travel_to(now) do
fake_destination_file = File.join(backups_path, 'Procfile.20210506185031')
expect(described_class.new(fake_source_file).recover_cmd_string).to eq("cp -f '#{fake_destination_file}' '#{fake_source_file}'")
expect(described_class.new(fake_source_file).recover_cmd_string).to eq("cp -f '#{fake_destination_file}' \\\n'#{fake_source_file}'\n")
end
end
end
......
......@@ -23,8 +23,8 @@ describe GDK::ErbRenderer do
subject(:renderer) { described_class.new(erb_file.to_s, out_file.to_s, config: config) }
before do
allow(renderer).to receive(:wait!)
allow(renderer).to receive(:backup!)
FileUtils.rm_f(out_file)
end
......@@ -61,8 +61,9 @@ describe GDK::ErbRenderer do
let(:protected_config_files) { ['tmp/*.out'] }
it 'warns about changes and does not overwrite content' do
expect(GDK::Output).to receive(:warn).with(%r{The changes to 'tmp/example.out' have not been applied})
expect(GDK::Output).to receive(:warn).with(%r{Changes to 'tmp/example.out' not applied because it's protected in gdk.yml.})
expect(renderer).to receive(:warn_changes!)
expect(subject).to receive(:sleep).with(described_class::WAIT_WARNING_SECS)
renderer.safe_render!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment