-
Notifications
You must be signed in to change notification settings - Fork 66
/
file.go
214 lines (190 loc) · 6.23 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package lib
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/qri-io/dataset"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/base/archive"
"github.com/qri-io/qri/base/fill"
"gopkg.in/yaml.v2"
)
// ReadDatasetFiles reads zero or more files, each representing a dataset or component of a
// dataset, and deserializes them, merging the results into a single dataset object. It is an
// error to provide any combination of files whose contents overlap (modify the same component).
func ReadDatasetFiles(pathList ...string) (*dataset.Dataset, error) {
// If there's only a single file provided, read it and return the dataset.
if len(pathList) == 1 {
ds, _, err := readSingleFile(pathList[0])
return ds, err
}
// If there's multiple files provided, read each one and merge them. Any exclusive
// component is an error, any component showing up multiple times is an error.
foundKinds := make(map[string]bool)
ds := dataset.Dataset{}
for _, p := range pathList {
component, kind, err := readSingleFile(p)
if err != nil {
return nil, err
}
if kind == "zip" || kind == "ds" {
return nil, fmt.Errorf("conflict, cannot save a full dataset with other components")
}
if _, ok := foundKinds[kind]; ok {
return nil, fmt.Errorf("conflict, multiple components of kind \"%s\"", kind)
}
foundKinds[kind] = true
ds.Assign(component)
}
return &ds, nil
}
// readSingleFile reads a single file, either a full dataset or component, and returns it as
// a dataset and a string specifying the kind of component that was created
func readSingleFile(path string) (*dataset.Dataset, string, error) {
ds := dataset.Dataset{}
switch qfs.PathKind(path) {
case "http":
// currently the only supported type of file url is a zip archive
resp, err := http.Get(path)
if err != nil {
return nil, "", err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, "", err
}
resp.Body.Close()
err = archive.UnzipDatasetBytes(data, &ds)
return &ds, "zip", nil
case "ipfs":
return nil, "", fmt.Errorf("reading dataset files from IPFS currently unsupported")
case "local":
f, err := os.Open(path)
if err != nil {
return nil, "", err
}
fileExt := strings.ToLower(filepath.Ext(path))
switch fileExt {
case ".yaml", ".yml":
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, "", err
}
fields := make(map[string]interface{})
if err = yaml.Unmarshal(data, fields); err != nil {
return nil, "", err
}
kind, err := fillDatasetOrComponent(fields, path, &ds)
return &ds, kind, err
case ".json":
fields := make(map[string]interface{})
if err = json.NewDecoder(f).Decode(&fields); err != nil {
if strings.HasPrefix(err.Error(), "json: cannot unmarshal array") {
err = fmt.Errorf("json has top-level type \"array\", cannot be a dataset file")
}
return nil, "", err
}
kind, err := fillDatasetOrComponent(fields, path, &ds)
return &ds, kind, err
case ".zip":
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, "", err
}
err = archive.UnzipDatasetBytes(data, &ds)
return &ds, "zip", err
case ".star":
// starlark files are assumed to be a transform script with no additional
// tranform component details:
ds.Transform = &dataset.Transform{ScriptPath: path}
ds.Transform.SetScriptFile(qfs.NewMemfileReader("transform.star", f))
return &ds, "tf", nil
case ".html":
// html files are assumped to be a viz script with no additional viz
// component details
// TODO(dlong): Deprecate viz, assume "html" is a readme
ds.Viz = &dataset.Viz{ScriptPath: path}
ds.Viz.Format = "html"
ds.Viz.SetScriptFile(qfs.NewMemfileReader("viz.html", f))
return &ds, "vz", nil
case ".md":
// md files are assumped to be a readme file
ds.Readme = &dataset.Readme{ScriptPath: path}
ds.Readme.Format = "md"
ds.Readme.SetScriptFile(qfs.NewMemfileReader("readme.md", f))
return &ds, "rm", nil
default:
return nil, "", fmt.Errorf("error, unrecognized file extension: \"%s\"", fileExt)
}
default:
return nil, "", fmt.Errorf("error, unknown path kind: \"%s\"", qfs.PathKind(path))
}
}
func fillDatasetOrComponent(fields map[string]interface{}, path string, ds *dataset.Dataset) (string, error) {
var target interface{}
target = ds
kind := ""
// Look for the component key in the file.
if kindStr, ok := fields["qri"].(string); ok && len(kindStr) >= 2 {
kind = kindStr[:2]
}
// If no key found, see if the path matches one of the recognized component filenames
if kind == "" {
basename := filepath.Base(path)
basename = strings.TrimSuffix(basename, filepath.Ext(basename))
switch basename {
case "meta":
kind = "md"
case "structure":
kind = "st"
}
}
switch kind {
case "", "ds":
// nothing to do, default case is the Dataset itself
kind = "ds"
case "rm":
ds.Readme = &dataset.Readme{}
target = ds.Readme
case "md":
ds.Meta = &dataset.Meta{}
target = ds.Meta
case "cm":
ds.Commit = &dataset.Commit{}
target = ds.Commit
case "st":
ds.Structure = &dataset.Structure{}
target = ds.Structure
case "tf":
ds.Transform = &dataset.Transform{}
target = ds.Transform
default:
return "", fmt.Errorf("unknown component key %q", kind)
}
if err := fill.Struct(fields, target); err != nil {
return "", err
}
absDatasetPaths(path, ds)
return kind, nil
}
// absDatasetPaths converts any relative filepath references in a Dataset to
// their absolute counterpart
func absDatasetPaths(path string, dsp *dataset.Dataset) {
base := filepath.Dir(path)
if dsp.BodyPath != "" && qfs.PathKind(dsp.BodyPath) == "local" && !filepath.IsAbs(dsp.BodyPath) {
dsp.BodyPath = filepath.Join(base, dsp.BodyPath)
}
if dsp.Transform != nil && qfs.PathKind(dsp.Transform.ScriptPath) == "local" && !filepath.IsAbs(dsp.Transform.ScriptPath) {
dsp.Transform.ScriptPath = filepath.Join(base, dsp.Transform.ScriptPath)
}
if dsp.Viz != nil && qfs.PathKind(dsp.Viz.ScriptPath) == "local" && !filepath.IsAbs(dsp.Viz.ScriptPath) {
dsp.Viz.ScriptPath = filepath.Join(base, dsp.Viz.ScriptPath)
}
if dsp.Readme != nil && qfs.PathKind(dsp.Readme.ScriptPath) == "local" && !filepath.IsAbs(dsp.Readme.ScriptPath) {
dsp.Readme.ScriptPath = filepath.Join(base, dsp.Readme.ScriptPath)
}
}