22{-# LANGUAGE DerivingVia #-}
33{-# LANGUAGE TypeFamilies #-}
44
5- module Foliage.RemoteAsset (
6- fetchRemoteAsset ,
7- addFetchRemoteAssetRule ,
5+ module Foliage.FetchURL (
6+ fetchURL ,
7+ addFetchURLRule ,
88)
99where
1010
@@ -24,23 +24,23 @@ import Network.URI.Orphans ()
2424import System.Directory (createDirectoryIfMissing )
2525import System.Exit (ExitCode (.. ))
2626
27- newtype RemoteAsset = RemoteAsset URI
27+ newtype FetchURL = FetchURL URI
2828 deriving (Eq )
2929 deriving (Hashable , Binary , NFData ) via URI
3030
31- instance Show RemoteAsset where
32- show (RemoteAsset uri) = " fetchRemoteAsset " ++ show uri
31+ instance Show FetchURL where
32+ show (FetchURL uri) = " fetchURL " ++ show uri
3333
34- type instance RuleResult RemoteAsset = FilePath
34+ type instance RuleResult FetchURL = FilePath
3535
36- fetchRemoteAsset :: URI -> Action FilePath
37- fetchRemoteAsset = apply1 . RemoteAsset
36+ fetchURL :: URI -> Action FilePath
37+ fetchURL = apply1 . FetchURL
3838
39- addFetchRemoteAssetRule :: FilePath -> Rules ()
40- addFetchRemoteAssetRule cacheDir = addBuiltinRule noLint noIdentity run
39+ addFetchURLRule :: FilePath -> Rules ()
40+ addFetchURLRule cacheDir = addBuiltinRule noLint noIdentity run
4141 where
42- run :: BuiltinRun RemoteAsset FilePath
43- run (RemoteAsset uri) old _mode = do
42+ run :: BuiltinRun FetchURL FilePath
43+ run (FetchURL uri) old _mode = do
4444 unless (uriQuery uri == " " ) $
4545 error (" Query elements in URI are not supported: " <> show uri)
4646
@@ -68,36 +68,7 @@ addFetchRemoteAssetRule cacheDir = addBuiltinRule noLint noIdentity run
6868runCurl :: URI -> String -> String -> Action ETag
6969runCurl uri path etagFile = do
7070 (Exit exitCode, Stdout out) <-
71- traced " curl" $
72- cmd
73- Shell
74- [ " curl"
75- , -- Silent or quiet mode. Do not show progress meter or error messages. Makes Curl mute.
76- " --silent"
77- , -- Fail fast with no output at all on server errors.
78- " --fail"
79- , -- If the server reports that the requested page has moved to a different location this
80- -- option will make curl redo the request on the new place.
81- -- NOTE: This is needed because github always replies with a redirect
82- " --location"
83- , -- This option makes a conditional HTTP request for the specific ETag read from the
84- -- given file by sending a custom If-None-Match header using the stored ETag.
85- -- For correct results, make sure that the specified file contains only a single line
86- -- with the desired ETag. An empty file is parsed as an empty ETag.
87- " --etag-compare"
88- , etagFile
89- , -- This option saves an HTTP ETag to the specified file. If no ETag is sent by the server,
90- -- an empty file is created.
91- " --etag-save"
92- , etagFile
93- , -- Write output to <file> instead of stdout.
94- " --output"
95- , path
96- , " --write-out"
97- , " %{json}"
98- , -- URL to fetch
99- show uri
100- ]
71+ traced " curl" $ cmd Shell curlInvocation
10172 case exitCode of
10273 ExitSuccess -> liftIO $ BS. readFile etagFile
10374 ExitFailure c -> do
@@ -112,7 +83,36 @@ runCurl uri path etagFile = do
11283 ]
11384 -- We can consider displaying different messages based on some fields (e.g. response_code)
11485 Right CurlWriteOut {errormsg} ->
115- error errormsg
86+ error $ unlines [" calling" , unwords curlInvocation, " failed with" , errormsg]
87+ where
88+ curlInvocation =
89+ [ " curl"
90+ , -- Silent or quiet mode. Do not show progress meter or error messages. Makes Curl mute.
91+ " --silent"
92+ , -- Fail fast with no output at all on server errors.
93+ " --fail"
94+ , -- If the server reports that the requested page has moved to a different location this
95+ -- option will make curl redo the request on the new place.
96+ -- NOTE: This is needed because github always replies with a redirect
97+ " --location"
98+ , -- This option makes a conditional HTTP request for the specific ETag read from the
99+ -- given file by sending a custom If-None-Match header using the stored ETag.
100+ -- For correct results, make sure that the specified file contains only a single line
101+ -- with the desired ETag. An empty file is parsed as an empty ETag.
102+ " --etag-compare"
103+ , etagFile
104+ , -- This option saves an HTTP ETag to the specified file. If no ETag is sent by the server,
105+ -- an empty file is created.
106+ " --etag-save"
107+ , etagFile
108+ , -- Write output to <file> instead of stdout.
109+ " --output"
110+ , path
111+ , " --write-out"
112+ , " %{json}"
113+ , -- URL to fetch
114+ show uri
115+ ]
116116
117117type ETag = BS. ByteString
118118
0 commit comments