@@ -180,111 +180,9 @@ func (r *Reader) include(node Node) error {
180180}
181181
182182func (r * Reader ) readNode (node Node ) (* ast.Taskfile , error ) {
183- var (
184- err error
185- cache * Cache
186- source * source
187- )
188-
189- if node .Remote () {
190- cache , err = NewCache (r .tempDir )
191- if err != nil {
192- return nil , err
193- }
194- }
195-
196- // If the file is remote and we're in offline mode, check if we have a cached copy
197- if node .Remote () && r .offline {
198- if source , err = cache .read (node ); errors .Is (err , os .ErrNotExist ) {
199- return nil , & errors.TaskfileCacheNotFoundError {URI : node .Location ()}
200- } else if err != nil {
201- return nil , err
202- }
203-
204- // TODO: Find a cleaner way to override source when loading from the cache
205- // Without this later usages of ResolveEntrypoint will be relative to the old source location
206- // fr before it got moved into the cache.
207- if n , ok := node .(* RemoteNode ); ok {
208- n .cachedSource = source
209- }
210-
211- r .logger .VerboseOutf (logger .Magenta , "task: [%s] Fetched cached copy\n " , node .Location ())
212- } else {
213-
214- downloaded := false
215- ctx , cf := context .WithTimeout (context .Background (), r .timeout )
216- defer cf ()
217-
218- // Read the file
219- source , err = node .Read (ctx )
220- // If we timed out then we likely have a network issue
221- if node .Remote () && errors .Is (ctx .Err (), context .DeadlineExceeded ) {
222- // If a download was requested, then we can't use a cached copy
223- if r .download {
224- return nil , & errors.TaskfileNetworkTimeoutError {URI : node .Location (), Timeout : r .timeout }
225- }
226- // Search for any cached copies
227- if source , err = cache .read (node ); errors .Is (err , os .ErrNotExist ) {
228- return nil , & errors.TaskfileNetworkTimeoutError {URI : node .Location (), Timeout : r .timeout , CheckedCache : true }
229- } else if err != nil {
230- return nil , err
231- }
232- r .logger .VerboseOutf (logger .Magenta , "task: [%s] Network timeout. Fetched cached copy\n " , node .Location ())
233-
234- // TODO: Find a cleaner way to override source when loading from the cache
235- // Without this later usages of ResolveEntrypoint will be relative to the old source location
236- // fr before it got moved into the cache.
237- if n , ok := node .(* RemoteNode ); ok {
238- n .cachedSource = source
239- }
240-
241- } else if err != nil {
242- return nil , err
243- } else {
244- downloaded = true
245- }
246-
247- // If the node was remote, we need to check the checksum
248- if node .Remote () && downloaded {
249- r .logger .VerboseOutf (logger .Magenta , "task: [%s] Fetched remote copy\n " , node .Location ())
250-
251- // Get the checksums
252- cachedChecksum := cache .readChecksum (node )
253- checksum , err := checksumSource (* source )
254- if err != nil {
255- return nil , err
256- }
257-
258- var prompt string
259- if cachedChecksum == "" {
260- // If the checksum doesn't exist, prompt the user to continue
261- prompt = fmt .Sprintf (taskfileUntrustedPrompt , node .Location ())
262- } else if checksum != cachedChecksum {
263- // If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
264- prompt = fmt .Sprintf (taskfileChangedPrompt , node .Location ())
265- }
266- if prompt != "" {
267- if err := r .logger .Prompt (logger .Yellow , prompt , "n" , "y" , "yes" ); err != nil {
268- return nil , & errors.TaskfileNotTrustedError {URI : node .Location ()}
269- }
270- }
271-
272- // If the hash has changed (or is new)
273- if checksum != cachedChecksum {
274- // Cache the file
275- r .logger .VerboseOutf (logger .Magenta , "task: [%s] Caching downloaded file\n " , node .Location ())
276- if source , err = cache .write (node , * source ); err != nil {
277- return nil , err
278- }
279-
280- // TODO: Find a cleaner way to override source when loading from the cache
281- // Without this later usages of ResolveEntrypoint will be relative to the old source location
282- // fr before it got moved into the cache.
283- if n , ok := node .(* RemoteNode ); ok {
284- n .cachedSource = source
285- }
286- }
287- }
183+ source , err := r .loadNodeContent (node )
184+ if err != nil {
185+ return nil , err
288186 }
289187
290188 var tf ast.Taskfile
@@ -320,3 +218,87 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
320218
321219 return & tf , nil
322220}
221+
222+ func (r * Reader ) loadNodeContent (node Node ) (* source , error ) {
223+ if ! node .Remote () {
224+ ctx , cf := context .WithTimeout (context .Background (), r .timeout )
225+ defer cf ()
226+ return node .Read (ctx )
227+ }
228+
229+ cache , err := NewCache (r .tempDir )
230+ if err != nil {
231+ return nil , err
232+ }
233+
234+ if r .offline {
235+ // In offline mode try to use cached copy
236+ cached , err := cache .read (node )
237+ if errors .Is (err , os .ErrNotExist ) {
238+ return nil , & errors.TaskfileCacheNotFoundError {URI : node .Location ()}
239+ } else if err != nil {
240+ return nil , err
241+ }
242+ r .logger .VerboseOutf (logger .Magenta , "task: [%s] Fetched cached copy\n " , node .Location ())
243+
244+ return cached , nil
245+ }
246+
247+ ctx , cf := context .WithTimeout (context .Background (), r .timeout )
248+ defer cf ()
249+
250+ src , err := node .Read (ctx )
251+ if errors .Is (ctx .Err (), context .DeadlineExceeded ) {
252+ // If we timed out then we likely have a network issue
253+
254+ // If a download was requested, then we can't use a cached copy
255+ if r .download {
256+ return nil , & errors.TaskfileNetworkTimeoutError {URI : node .Location (), Timeout : r .timeout }
257+ }
258+
259+ // Search for any cached copies
260+ cached , err := cache .read (node )
261+ if errors .Is (err , os .ErrNotExist ) {
262+ return nil , & errors.TaskfileNetworkTimeoutError {URI : node .Location (), Timeout : r .timeout , CheckedCache : true }
263+ } else if err != nil {
264+ return nil , err
265+ }
266+ r .logger .VerboseOutf (logger .Magenta , "task: [%s] Network timeout. Fetched cached copy\n " , node .Location ())
267+
268+ return cached , nil
269+
270+ } else if err != nil {
271+ return nil , err
272+ }
273+ r .logger .VerboseOutf (logger .Magenta , "task: [%s] Fetched remote copy\n " , node .Location ())
274+
275+ // Get the checksums
276+ cachedChecksum := cache .readChecksum (node )
277+ checksum , err := checksumSource (* src )
278+ if err != nil {
279+ return nil , err
280+ }
281+
282+ var prompt string
283+ if cachedChecksum == "" {
284+ // If the checksum doesn't exist, prompt the user to continue
285+ prompt = fmt .Sprintf (taskfileUntrustedPrompt , node .Location ())
286+ } else if checksum != cachedChecksum {
287+ // If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
288+ prompt = fmt .Sprintf (taskfileChangedPrompt , node .Location ())
289+ }
290+
291+ if prompt != "" {
292+ if err := r .logger .Prompt (logger .Yellow , prompt , "n" , "y" , "yes" ); err != nil {
293+ return nil , & errors.TaskfileNotTrustedError {URI : node .Location ()}
294+ }
295+
296+ // Cache the file
297+ r .logger .VerboseOutf (logger .Magenta , "task: [%s] Caching downloaded file\n " , node .Location ())
298+ if src , err = cache .write (node , * src ); err != nil {
299+ return nil , err
300+ }
301+ }
302+
303+ return src , nil
304+ }
0 commit comments