15370: Fix flaky test.
[arvados.git] / sdk / go / keepclient / collectionreader.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package keepclient
6
7 import (
8         "errors"
9         "os"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12         "git.arvados.org/arvados.git/sdk/go/manifest"
13 )
14
15 // ErrNoManifest indicates the given collection has no manifest
16 // information (e.g., manifest_text was excluded by a "select"
17 // parameter when retrieving the collection record).
18 var ErrNoManifest = errors.New("Collection has no manifest")
19
20 // CollectionFileReader returns a Reader that reads content from a single file
21 // in the collection. The filename must be relative to the root of the
22 // collection.  A leading prefix of "/" or "./" in the filename is ignored.
23 func (kc *KeepClient) CollectionFileReader(collection map[string]interface{}, filename string) (arvados.File, error) {
24         mText, ok := collection["manifest_text"].(string)
25         if !ok {
26                 return nil, ErrNoManifest
27         }
28         fs, err := (&arvados.Collection{ManifestText: mText}).FileSystem(nil, kc)
29         if err != nil {
30                 return nil, err
31         }
32         return fs.OpenFile(filename, os.O_RDONLY, 0)
33 }
34
35 func (kc *KeepClient) ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error) {
36         fs, err := (&arvados.Collection{ManifestText: m.Text}).FileSystem(nil, kc)
37         if err != nil {
38                 return nil, err
39         }
40         return fs.OpenFile(filename, os.O_RDONLY, 0)
41 }