Skip to content

Commit

Permalink
feat(get): support getting script file fields
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Feb 2, 2019
1 parent c5501d7 commit da8ae46
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
37 changes: 16 additions & 21 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/dsdiff"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/actions"
"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/p2p"
Expand Down Expand Up @@ -391,26 +389,22 @@ func (h *DatasetHandlers) peerListHandler(w http.ResponseWriter, r *http.Request
// TODO - make this less bad. this should happen lower and lib Params should be used to set the response
// body to well-formed JSON
func addBodyFile(res *repo.DatasetRef) error {
file := res.Dataset.BodyFile()
if file == nil {
log.Error("no body file")
return fmt.Errorf("no response body file")
}

if res.Dataset.Structure.Format == dataset.JSONDataFormat.String() {
data, err := ioutil.ReadAll(res.Dataset.Body.(io.Reader))
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
res.Dataset.Body = json.RawMessage(data)
return nil
}

file, ok := res.Dataset.Body.(qfs.File)
if !ok {
log.Error("response body isn't a qfs.File")
return fmt.Errorf("response body isn't a qfs.File")
}

in := res.Dataset.Structure
// if err := in.Decode(res.Dataset.Structure); err != nil {
// return err
// }

st := &dataset.Structure{}
st.Assign(in, &dataset.Structure{
Format: "json",
Expand All @@ -419,6 +413,7 @@ func addBodyFile(res *repo.DatasetRef) error {

data, err := actions.ConvertBodyFile(file, in, st, 0, 0, true)
if err != nil {
log.Errorf("converting body file to JSON: %s", err)
return fmt.Errorf("converting body file to JSON: %s", err)
}
res.Dataset.Body = json.RawMessage(data)
Expand Down Expand Up @@ -448,10 +443,10 @@ func (h *DatasetHandlers) addHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *DatasetHandlers) saveHandler(w http.ResponseWriter, r *http.Request) {
dsp := &dataset.Dataset{}
ds := &dataset.Dataset{}

if r.Header.Get("Content-Type") == "application/json" {
err := json.NewDecoder(r.Body).Decode(dsp)
err := json.NewDecoder(r.Body).Decode(ds)
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
Expand All @@ -464,12 +459,12 @@ func (h *DatasetHandlers) saveHandler(w http.ResponseWriter, r *http.Request) {
return
}
if args.Peername != "" {
dsp.Peername = args.Peername
dsp.Name = args.Name
ds.Peername = args.Peername
ds.Name = args.Name
}
}
} else {
if err := dsutil.FormFileDataset(r, dsp); err != nil {
if err := dsutil.FormFileDataset(r, ds); err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
Expand All @@ -478,7 +473,7 @@ func (h *DatasetHandlers) saveHandler(w http.ResponseWriter, r *http.Request) {
res := &repo.DatasetRef{}
scriptOutput := &bytes.Buffer{}
p := &lib.SaveParams{
Dataset: dsp,
Dataset: ds,
Private: r.FormValue("private") == "true",
DryRun: r.FormValue("dry_run") == "true",
ReturnBody: r.FormValue("return_body") == "true",
Expand All @@ -492,9 +487,9 @@ func (h *DatasetHandlers) saveHandler(w http.ResponseWriter, r *http.Request) {
util.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("parsing secrets: %s", err))
return
}
} else if dsp.Transform != nil && dsp.Transform.Secrets != nil {
} else if ds.Transform != nil && ds.Transform.Secrets != nil {
// TODO remove this, require API consumers to send secrets separately
p.Secrets = dsp.Transform.Secrets
p.Secrets = ds.Transform.Secrets
}

if err := h.Save(p, res); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion base/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ func CreateDataset(r repo.Repo, streams ioes.IOStreams, ds, dsPrev *dataset.Data
return
}

// need to open here b/c we might be doing a dry-run, which would mean we have
// references to files in a store that won't exist after this function call
// TODO (b5): this should be replaced with a call to OpenDataset with a qfs that
// knows about the store
if resBody, err = r.Store().Get(ref.Dataset.BodyPath); err != nil {
fmt.Println("error getting from store:", err.Error())
log.Error("error getting from store:", err.Error())
}
ref.Dataset.SetBodyFile(resBody)
return
Expand Down
1 change: 1 addition & 0 deletions base/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Select(r repo.Repo, ref repo.DatasetRef, path string) (interface{}, error)
}

// ApplyPath gets a dataset value by applying a case.Sensitve.dot.separated.path
// ApplyPath cannot select file fields
func ApplyPath(ds *dataset.Dataset, path string) (interface{}, error) {
var value reflect.Value
value, err := pathValue(ds, path)
Expand Down
5 changes: 5 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,10 @@ func (o *GetOptions) Run() (err error) {
}

_, err = o.Out.Write(res.Bytes)
if err != nil {
return err
}
// commands should always be newline-terminiated, which isn't included in res.Bytes
_, err = o.Out.Write([]byte{'\n'})
return err
}
9 changes: 5 additions & 4 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ func (o *UpdateOptions) Run() (err error) {
}

if o.Secrets != nil {
if !confirm(o.Out, o.In, `
Warning: You are providing secrets to a dataset transformation.
Never provide secrets to a transformation you do not trust.
continue?`, true) {
secretsMsg := `
Warning: You are providing secrets to a dataset transformation.
Never provide secrets to a transformation you do not trust.
continue?`
if !confirm(o.Out, o.In, secretsMsg, true) {
return
}

Expand Down
17 changes: 10 additions & 7 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/rpc"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -146,7 +147,7 @@ func (r *DatasetRequests) Get(p *GetParams, res *GetResult) (err error) {
res.Bytes = bufData
return err
} else if p.Selector == "" {
// `qri get` loads only the dataset head
// `qri get` without a selector loads only the dataset head
switch p.Format {
case "json":
if p.Concise {
Expand All @@ -158,6 +159,14 @@ func (r *DatasetRequests) Get(p *GetParams, res *GetResult) (err error) {
res.Bytes, err = yaml.Marshal(res.Dataset)
}
return err
} else if p.Selector == "transform.script" && ds.Transform != nil && ds.Transform.ScriptFile() != nil {
// accomodate two special case script file fields
// TODO (b5): this is a hack that should be generalized
res.Bytes, err = ioutil.ReadAll(ds.Transform.ScriptFile())
return
} else if p.Selector == "viz.script" && ds.Viz != nil && ds.Viz.ScriptFile() != nil {
res.Bytes, err = ioutil.ReadAll(ds.Viz.ScriptFile())
return
} else {
// `qri get <selector>` loads the dataset but only returns the applicable component / field
value, err := base.ApplyPath(res.Dataset, p.Selector)
Expand Down Expand Up @@ -279,12 +288,6 @@ func (r *DatasetRequests) Save(p *SaveParams, res *repo.DatasetRef) (err error)
}
}

// TODO (b5): think about the ReturnBody flag now that dataset.Dataset should
// encode cleanly. probs just remove it
// if p.ReturnBody && ref.Dataset != nil {
// ref.Dataset.Body = body
// }

*res = ref
return nil
}
Expand Down

0 comments on commit da8ae46

Please sign in to comment.