chore: Fix verbosity of restore-timestamps
This commit is contained in:
		
							parent
							
								
									e160a73d21
								
							
						
					
					
						commit
						27aab5aa63
					
				
					 1 changed files with 13 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -4,13 +4,13 @@ defmodule TimestampRestorer do
 | 
			
		|||
  @environment System.get_env("MIX_ENV", "dev")
 | 
			
		||||
  @db_path "_build/#{@environment}/timestamp-database"
 | 
			
		||||
 | 
			
		||||
  def sha_all do
 | 
			
		||||
  def sha_all(opts \\ []) do
 | 
			
		||||
    timestamp_database = load_timestamp_database()
 | 
			
		||||
 | 
			
		||||
    "**/*.{ex,exs,beam}"
 | 
			
		||||
    |> Path.wildcard()
 | 
			
		||||
    |> Enum.reduce(%{}, fn filename, acc ->
 | 
			
		||||
      {sha, timestamp} = process(filename, timestamp_database)
 | 
			
		||||
      {sha, timestamp} = process(filename, timestamp_database, opts)
 | 
			
		||||
      Map.put(acc, sha, timestamp)
 | 
			
		||||
    end)
 | 
			
		||||
    |> write_timestamp_database()
 | 
			
		||||
| 
						 | 
				
			
			@ -40,23 +40,27 @@ defmodule TimestampRestorer do
 | 
			
		|||
    |> (& File.write!(@db_path, &1)).()
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp process(filename, timestamp_database) do
 | 
			
		||||
  defp process(filename, timestamp_database, opts) do
 | 
			
		||||
    {verbose, _opts} = Keyword.pop(opts, :verbose, false)
 | 
			
		||||
    sha = sha(filename)
 | 
			
		||||
    {:ok, %{mtime: new_timestamp}} = File.lstat(filename, time: :posix)
 | 
			
		||||
 | 
			
		||||
    case Map.get(timestamp_database, sha) do
 | 
			
		||||
      nil ->
 | 
			
		||||
        :logger.debug("[NEW SHA  ] #{filename}: #{new_timestamp}")
 | 
			
		||||
        log("[NEW SHA  ] #{filename}: #{new_timestamp}", verbose)
 | 
			
		||||
      timestamp when timestamp < new_timestamp ->
 | 
			
		||||
        :logger.debug("[RESTORED ] #{filename}: #{timestamp}")
 | 
			
		||||
        log("[RESTORED ] #{filename}: #{timestamp}", verbose)
 | 
			
		||||
        File.touch(filename, timestamp)
 | 
			
		||||
      timestamp when timestamp >= new_timestamp ->
 | 
			
		||||
        :logger.debug("[UNCHANGED] #{filename}: #{timestamp}")
 | 
			
		||||
        log("[UNCHANGED] #{filename}: #{timestamp}", verbose)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    {sha, new_timestamp}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp log(_message, false), do: :ok
 | 
			
		||||
  defp log(message, true), do: :logger.debug(message)
 | 
			
		||||
 | 
			
		||||
  defp sha(filename) do
 | 
			
		||||
    hash_ref = :crypto.hash_init(:sha)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -71,6 +75,8 @@ defmodule TimestampRestorer do
 | 
			
		|||
  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")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue