Skip to content

Commit

Permalink
Added pre download hook callback
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Oct 9, 2022
1 parent 6f93ad3 commit fd6a261
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support for FreeBSD
- `post_write_hook` callback that is invoked whenever a file is written
- `pre_download_hook` callback that is invoked prior to starting a download

### Changed

Expand Down
9 changes: 9 additions & 0 deletions lib/octo_fetch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ defmodule OctoFetch do
:ok
end

@impl true
def pre_download_hook(_, _) do
:cont
end

@impl true
def download(output_dir, opts \\ []) do
opts = Keyword.merge(unquote(opts), opts)
Expand Down Expand Up @@ -148,6 +153,7 @@ defmodule OctoFetch do
{:ok, artifact_name} <-
generate_artifact_name(downloader_module, version, operating_system, architecture),
full_download_url <- Path.join(github_base_url, artifact_name),
:cont <- downloader_module.pre_download_hook(artifact_name, output_dir),
{:ok, artifact_contents} <- download_artifact(full_download_url, opts),
:ok <- verify_artifact_checksum(artifact_contents, sha_checksum, full_download_url),
{:ok, files_to_write} <-
Expand All @@ -173,6 +179,9 @@ defmodule OctoFetch do
{:error, reason} ->
Logger.warning("Failed to download release from GitHub. #{reason}")
{:error, reason}

:skipped ->
:skipped
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/octo_fetch/downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule OctoFetch.Downloader do

@type os() :: :linux | :darwin | :freebsd | :windows
@type arch() :: :arm64 | :amd64
@type download_result() :: {:ok, successful_files :: list(), failed_files :: list()} | {:error, String.t()}
@type download_result() :: {:ok, successful_files :: list(), failed_files :: list()} | {:error, String.t()} | :skipped

@doc """
This callback generates the base URL for the artifact based on the provided GitHub repo
Expand Down Expand Up @@ -51,6 +51,13 @@ defmodule OctoFetch.Downloader do
"""
@callback post_write_hook(file :: String.t()) :: :ok

@doc """
This callback is invoked prior to a file being downloaded. This gives you
the opportunity to skip the download if you so chose by returning `:skip`. Otherwise,
return `:cont` to continue with the download.
"""
@callback pre_download_hook(file :: String.t(), output_dir :: String.t()) :: :cont | :skip

@doc """
This callback acts as a pass through to the `OctoFetch` module for the
downloader implementation. See `OctoFetch.download/3` for supported `opts`.
Expand Down

0 comments on commit fd6a261

Please sign in to comment.