Skip to content

Commit

Permalink
feat(save): re-run transfrom on qri save with no args
Browse files Browse the repository at this point in the history
If a dataset defines a transform, running "qri save" without args should re-run that transform.
The code I've added here is hacky & terrible, but we need to move. Will fix in the future.
  • Loading branch information
b5 committed Sep 14, 2018
1 parent b3d07ad commit 0e906ae
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
42 changes: 41 additions & 1 deletion actions/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -171,6 +172,45 @@ func UpdateDataset(node *p2p.QriNode, dsp *dataset.DatasetPod) (ds *dataset.Data
ds.Commit.Title = updates.Commit.Title
ds.Commit.Message = updates.Commit.Message

// TODO - this is so bad. fix. currently createDataset expects paths to
// local files, so we're just making them up on the spot.
if ds.Transform != nil && ds.Transform.ScriptPath[:len("/ipfs/")] == "/ipfs/" {
tfScript, e := node.Repo.Store().Get(datastore.NewKey(ds.Transform.ScriptPath))
if e != nil {
err = e
return
}

f, e := ioutil.TempFile("", "transform.sky")
if e != nil {
err = e
return
}
if _, e := io.Copy(f, tfScript); err != nil {
err = e
return
}
ds.Transform.ScriptPath = f.Name()
}
if ds.Viz != nil && ds.Viz.ScriptPath[:len("/ipfs/")] == "/ipfs/" {
vizScript, e := node.Repo.Store().Get(datastore.NewKey(ds.Viz.ScriptPath))
if e != nil {
err = e
return
}

f, e := ioutil.TempFile("", "viz.html")
if e != nil {
err = e
return
}
if _, e := io.Copy(f, vizScript); err != nil {
err = e
return
}
ds.Viz.ScriptPath = f.Name()
}

// Assign will assign any previous paths to the current paths
// the dsdiff (called in dsfs.CreateDataset), will compare the paths
// see that they are the same, and claim there are no differences
Expand All @@ -183,7 +223,7 @@ func UpdateDataset(node *p2p.QriNode, dsp *dataset.DatasetPod) (ds *dataset.Data
if ds.Structure != nil {
ds.Structure.SetPath("")
}
// ds.Viz.SetPath("")

return
}

Expand Down
30 changes: 18 additions & 12 deletions cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"path/filepath"
"strings"

"io/ioutil"

"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
"io/ioutil"
)

// NewSaveCommand creates a `qri save` cobra command used for saving changes
Expand All @@ -26,22 +27,27 @@ func NewSaveCommand(f Factory, ioStreams IOStreams) *cobra.Command {
Long: `
Save is how you change a dataset, updating one or more of data, metadata, and structure.
You can also update your data via url. Every time you run save, an entry is added to
your dataset’s log (which you can see by running ` + "`qri log <dataset_reference>`" + `).
your dataset’s log (which you can see by running ` + "`qri log <dataset_reference>`" + `).
Every time you save, you can provide a message about what
you changed and why. If you don’t provide a message
Qri will automatically generate one for you.
If the dataset you're changing has defined a transform, running ` + "`qri save`" + `
will re execute the transform. To only re-run the tranform, run save with no args.
Every time you save, you can provide a message about what you changed and why.
If you don’t provide a message Qri will automatically generate one for you.
When you make an update and save a dataset that you originally added from a different
peer, the dataset gets renamed from ` + "`peers_name/dataset_name`" + ` to ` + "`my_name/dataset_name`" + `.
The ` + "`--message`" + `" and ` + "`--title`" + ` flags allow you to add a commit message and title
to the save.`,
The ` + "`--message`" + `" and ` + "`--title`" + ` flags allow you to add a
commit message and title to the save.`,
Example: ` # save updated data to dataset annual_pop:
qri --body /path/to/data.csv me/annual_pop
qri save --body /path/to/data.csv me/annual_pop
# save updated dataset (no data) to annual_pop:
qri --file /path/to/dataset.yaml me/annual_pop`,
qri save --file /path/to/dataset.yaml me/annual_pop
# re-execute a dataset that has a transform:
qri save me/tf_dataset`,
Annotations: map[string]string{
"group": "dataset",
},
Expand Down Expand Up @@ -100,9 +106,9 @@ func (o *SaveOptions) Validate() error {
if o.Ref == "" {
return lib.NewError(lib.ErrBadArgs, "please provide the peername and dataset name you would like to update, in the format of `peername/dataset_name`\nsee `qri save --help` for more info")
}
if o.FilePath == "" && o.BodyPath == "" {
return lib.NewError(lib.ErrBadArgs, "please an updated/changed dataset file (--file) or body file (--body), or both\nsee `qri save --help` for more info")
}
// if o.FilePath == "" && o.BodyPath == "" {
// return lib.NewError(lib.ErrBadArgs, "please an updated/changed dataset file (--file) or body file (--body), or both\nsee `qri save --help` for more info")
// }
return nil
}

Expand Down
1 change: 0 additions & 1 deletion cmd/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestSaveValidate(t *testing.T) {
msg string
}{
{"", "", "", lib.ErrBadArgs.Error(), "please provide the peername and dataset name you would like to update, in the format of `peername/dataset_name`\nsee `qri save --help` for more info"},
{"me/test", "", "", lib.ErrBadArgs.Error(), "please an updated/changed dataset file (--file) or body file (--body), or both\nsee `qri save --help` for more info"},
{"me/test", "test/path.yaml", "", "", ""},
{"me/test", "", "test/bodypath.yaml", "", ""},
{"me/test", "test/filepath.yaml", "test/bodypath.yaml", "", ""},
Expand Down
7 changes: 0 additions & 7 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ type DatasetRequests struct {
node *p2p.QriNode
}

// Repo exposes the DatasetRequest's repo
// TODO - this is an architectural flaw resulting from not having a clear
// order of local > network > RPC requests figured out
// func (r *DatasetRequests) Repo() repo.Repo {
// return r.repo
// }

// CoreRequestsName implements the Requets interface
func (DatasetRequests) CoreRequestsName() string { return "datasets" }

Expand Down

0 comments on commit 0e906ae

Please sign in to comment.