chore: Fix verbosity of restore-timestamps

This commit is contained in:
Robert Prehn 2021-04-24 12:01:33 -05:00
parent e160a73d21
commit 27aab5aa63

View file

@ -4,13 +4,13 @@ defmodule TimestampRestorer do
@environment System.get_env("MIX_ENV", "dev") @environment System.get_env("MIX_ENV", "dev")
@db_path "_build/#{@environment}/timestamp-database" @db_path "_build/#{@environment}/timestamp-database"
def sha_all do def sha_all(opts \\ []) do
timestamp_database = load_timestamp_database() timestamp_database = load_timestamp_database()
"**/*.{ex,exs,beam}" "**/*.{ex,exs,beam}"
|> Path.wildcard() |> Path.wildcard()
|> Enum.reduce(%{}, fn filename, acc -> |> Enum.reduce(%{}, fn filename, acc ->
{sha, timestamp} = process(filename, timestamp_database) {sha, timestamp} = process(filename, timestamp_database, opts)
Map.put(acc, sha, timestamp) Map.put(acc, sha, timestamp)
end) end)
|> write_timestamp_database() |> write_timestamp_database()
@ -40,23 +40,27 @@ defmodule TimestampRestorer do
|> (& File.write!(@db_path, &1)).() |> (& File.write!(@db_path, &1)).()
end end
defp process(filename, timestamp_database) do defp process(filename, timestamp_database, opts) do
{verbose, _opts} = Keyword.pop(opts, :verbose, false)
sha = sha(filename) sha = sha(filename)
{:ok, %{mtime: new_timestamp}} = File.lstat(filename, time: :posix) {:ok, %{mtime: new_timestamp}} = File.lstat(filename, time: :posix)
case Map.get(timestamp_database, sha) do case Map.get(timestamp_database, sha) do
nil -> nil ->
:logger.debug("[NEW SHA ] #{filename}: #{new_timestamp}") log("[NEW SHA ] #{filename}: #{new_timestamp}", verbose)
timestamp when timestamp < new_timestamp -> timestamp when timestamp < new_timestamp ->
:logger.debug("[RESTORED ] #{filename}: #{timestamp}") log("[RESTORED ] #{filename}: #{timestamp}", verbose)
File.touch(filename, timestamp) File.touch(filename, timestamp)
timestamp when timestamp >= new_timestamp -> timestamp when timestamp >= new_timestamp ->
:logger.debug("[UNCHANGED] #{filename}: #{timestamp}") log("[UNCHANGED] #{filename}: #{timestamp}", verbose)
end end
{sha, new_timestamp} {sha, new_timestamp}
end end
defp log(_message, false), do: :ok
defp log(message, true), do: :logger.debug(message)
defp sha(filename) do defp sha(filename) do
hash_ref = :crypto.hash_init(:sha) hash_ref = :crypto.hash_init(:sha)
@ -71,6 +75,8 @@ defmodule TimestampRestorer do
end end
end end
{time, _result} = :timer.tc(TimestampRestorer, :sha_all, []) {opts, _args, _errors} = OptionParser.parse(System.argv(), switches: [verbose: :boolean])
{time, _result} = :timer.tc(TimestampRestorer, :sha_all, [opts])
:logger.info("Restored timestamps in #{time / 1_000_000}s") :logger.info("Restored timestamps in #{time / 1_000_000}s")