Skip to content

Commit

Permalink
adds git fallback cacheOrApi to fix #373
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhedonia committed Jul 17, 2020
1 parent 6e7ff66 commit ce4139d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion buckaroo/DefaultSourceExplorer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@ open Buckaroo.Console
open Buckaroo.RichOutput
open Buckaroo.Logger



type DefaultSourceExplorer (console : ConsoleManager, downloadManager : DownloadManager, gitManager : GitManager) =
let logger = createLogger console (Some "explorer")
let toOptional = Async.Catch >> (Async.map Choice.toOption)

let fromFileCache url revision path =
gitManager.GetFile url revision path |> toOptional


// We fetch from cache or api if possible.
// However fetching from api might be not feasable if credentials are required.
// In such case we fallback to git and try to find the requested commit.
let cacheOrApi (api, url : string, rev : string, path : string) = async {
let! cached = fromFileCache url rev path
match cached with
| Some data -> return data
| None -> return! api rev path
| None ->
match! (api rev path |> Async.Catch) with
| Choice1Of2 result ->
return result
| Choice2Of2 error ->
logger.Info("failed to fetch file using api, falling back to git")
do! gitManager.FindCommit url rev None
return! fromFileCache url rev path |> Async.map (Option.getOrRaise <| error)
}

let extractFileFromHttp (source : HttpLocation) (filePath : string) = async {
Expand Down

0 comments on commit ce4139d

Please sign in to comment.