Skip to content

Commit 4e7a2f3

Browse files
committed
TEMP commit: applying caching refactor from go-task#1771
1 parent 86f986f commit 4e7a2f3

File tree

1 file changed

+87
-105
lines changed

1 file changed

+87
-105
lines changed

taskfile/reader.go

Lines changed: 87 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -181,111 +181,9 @@ func (r *Reader) include(node Node) error {
181181
}
182182

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

291189
var tf ast.Taskfile
@@ -321,3 +219,87 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
321219

322220
return &tf, nil
323221
}
222+
223+
func (r *Reader) loadNodeContent(node Node) (*source, error) {
224+
if !node.Remote() {
225+
ctx, cf := context.WithTimeout(context.Background(), r.timeout)
226+
defer cf()
227+
return node.Read(ctx)
228+
}
229+
230+
cache, err := NewCache(r.tempDir)
231+
if err != nil {
232+
return nil, err
233+
}
234+
235+
if r.offline {
236+
// In offline mode try to use cached copy
237+
cached, err := cache.read(node)
238+
if errors.Is(err, os.ErrNotExist) {
239+
return nil, &errors.TaskfileCacheNotFoundError{URI: node.Location()}
240+
} else if err != nil {
241+
return nil, err
242+
}
243+
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Fetched cached copy\n", node.Location())
244+
245+
return cached, nil
246+
}
247+
248+
ctx, cf := context.WithTimeout(context.Background(), r.timeout)
249+
defer cf()
250+
251+
src, err := node.Read(ctx)
252+
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
253+
// If we timed out then we likely have a network issue
254+
255+
// If a download was requested, then we can't use a cached copy
256+
if r.download {
257+
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout}
258+
}
259+
260+
// Search for any cached copies
261+
cached, err := cache.read(node)
262+
if errors.Is(err, os.ErrNotExist) {
263+
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout, CheckedCache: true}
264+
} else if err != nil {
265+
return nil, err
266+
}
267+
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Network timeout. Fetched cached copy\n", node.Location())
268+
269+
return cached, nil
270+
271+
} else if err != nil {
272+
return nil, err
273+
}
274+
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Fetched remote copy\n", node.Location())
275+
276+
// Get the checksums
277+
cachedChecksum := cache.readChecksum(node)
278+
checksum, err := checksumSource(*src)
279+
if err != nil {
280+
return nil, err
281+
}
282+
283+
var prompt string
284+
if cachedChecksum == "" {
285+
// If the checksum doesn't exist, prompt the user to continue
286+
prompt = fmt.Sprintf(taskfileUntrustedPrompt, node.Location())
287+
} else if checksum != cachedChecksum {
288+
// If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
289+
prompt = fmt.Sprintf(taskfileChangedPrompt, node.Location())
290+
}
291+
292+
if prompt != "" {
293+
if err := r.logger.Prompt(logger.Yellow, prompt, "n", "y", "yes"); err != nil {
294+
return nil, &errors.TaskfileNotTrustedError{URI: node.Location()}
295+
}
296+
297+
// Cache the file
298+
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Caching downloaded file\n", node.Location())
299+
if src, err = cache.write(node, *src); err != nil {
300+
return nil, err
301+
}
302+
}
303+
304+
return src, nil
305+
}

0 commit comments

Comments
 (0)