Skip to content

Commit

Permalink
make status work on datasets that aren't fsi-linked
Browse files Browse the repository at this point in the history
Merge pull request #832 from qri-io/no_fsi_status
  • Loading branch information
dustmop authored Jul 24, 2019
2 parents 4855892 + 491a9b9 commit 6794c33
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 153 deletions.
6 changes: 3 additions & 3 deletions actions/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func addCitiesDataset(t *testing.T, node *p2p.QriNode) repo.DatasetRef {
t.Fatal(err.Error())
}

ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -129,7 +129,7 @@ func addFlourinatedCompoundsDataset(t *testing.T, node *p2p.QriNode) repo.Datase
t.Fatal(err.Error())
}

ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -145,7 +145,7 @@ func addNowTransformDataset(t *testing.T, node *p2p.QriNode) repo.DatasetRef {
// this was put here to satisfy qri-io/qri/actions.TestUpdateDatasetLocal
tc.Input.Peername = "peer"

ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err := SaveDataset(node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand Down
25 changes: 12 additions & 13 deletions actions/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestUpdateRemoteDataset(t *testing.T) {
ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[]")))

// run a local update to advance history
now0, err := SaveDataset(peers[0], ds, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
now0, err := SaveDataset(peers[0], ds, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestSaveDataset(t *testing.T) {
}
ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[]")))

ref, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ DryRun: true, ShouldRender: true })
ref, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{DryRun: true, ShouldRender: true})
if err != nil {
t.Errorf("dry run error: %s", err.Error())
}
Expand All @@ -145,7 +145,7 @@ func TestSaveDataset(t *testing.T) {
ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[]")))

// test save
ref, err = SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err = SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestSaveDataset(t *testing.T) {
ds.Transform.OpenScriptFile(nil)

// dryrun should work
ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{ DryRun: true, ShouldRender: true })
ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{DryRun: true, ShouldRender: true})
if err != nil {
t.Fatal(err)
}
Expand All @@ -200,7 +200,7 @@ func TestSaveDataset(t *testing.T) {
ds.Transform.OpenScriptFile(nil)

// test save with transform
ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err)
}
Expand All @@ -219,7 +219,7 @@ func TestSaveDataset(t *testing.T) {
},
}

ref, err = SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err = SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestSaveDataset(t *testing.T) {
t.Error(err)
}

ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{ Pin: true, ShouldRender: true })
ref, err = SaveDataset(n, ds, secrets, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Error(err)
}
Expand All @@ -266,7 +266,7 @@ func TestSaveDatasetWithoutStructureOrBody(t *testing.T) {
},
}

_, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ ShouldRender: true })
_, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ShouldRender: true})
expect := "creating a new dataset requires a structure or a body"
if err == nil || err.Error() != expect {
t.Errorf("expected error, but got %s", err.Error())
Expand All @@ -285,22 +285,21 @@ func TestSaveDatasetReplace(t *testing.T) {
Structure: &dataset.Structure{Format: "json", Schema: map[string]interface{}{"type": "array"}},
}
ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[]")))


// test save
_, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ Pin: true })
_, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{Pin: true})
if err != nil {
t.Error(err)
}

ds = &dataset.Dataset{
Peername: "me",
Name: "test_save",
Peername: "me",
Name: "test_save",
Structure: &dataset.Structure{Format: "json", Schema: map[string]interface{}{"type": "object"}},
}
ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte(`{"foo":"bar"}`)))

ref, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{ Replace: true, Pin: true })
ref, err := SaveDataset(n, ds, nil, nil, SaveDatasetSwitches{Replace: true, Pin: true})
if err != nil {
t.Error(err)
}
Expand Down
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func NewServerRoutes(s Server) *http.ServeMux {

fsih := NewFSIHandlers(s.Instance, cfg.API.ReadOnly)
m.Handle("/dsstatus/", s.middleware(fsih.StatusHandler))
m.Handle("/fsilinks/", s.middleware(fsih.LinksHandler))

renderh := NewRenderHandlers(node.Repo)
m.Handle("/render/", s.middleware(renderh.RenderHandler))
Expand Down
26 changes: 26 additions & 0 deletions api/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,29 @@ func (h *FSIHandlers) statusHandler(w http.ResponseWriter, r *http.Request) {
}
util.WriteResponse(w, res)
}

// LinksHandler is the endpoint for getting the list of fsi-linked datasets
func (h *FSIHandlers) LinksHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "GET":
if h.ReadOnly {
readOnlyResponse(w, "/fsilinks")
return
}
h.linksHandler(w, r)
default:
util.NotFoundHandler(w, r)
}
}

func (h *FSIHandlers) linksHandler(w http.ResponseWriter, r *http.Request) {
p := false
res := []*lib.FSILink{}
if err := h.Links(&p, &res); err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, fmt.Errorf("error listing links: %s", err.Error()))
return
}
util.WriteResponse(w, res)
}
126 changes: 5 additions & 121 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/qri-io/ioes"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/lib"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,16 +37,12 @@ type CheckoutOptions struct {

Args []string

DatasetRequests *lib.DatasetRequests
FSIMethods *lib.FSIMethods
FSIMethods *lib.FSIMethods
}

// Complete completes a the command
func (o *CheckoutOptions) Complete(f Factory, args []string) (err error) {
o.Args = args
if o.DatasetRequests, err = f.DatasetRequests(); err != nil {
return err
}
o.FSIMethods, err = f.FSIMethods()
return err
}
Expand All @@ -66,124 +59,15 @@ func (o *CheckoutOptions) Run() (err error) {
}
folderName := ref[pos+1:]

// If directory exists, error.
if _, err = os.Stat(folderName); !os.IsNotExist(err) {
return fmt.Errorf("directory with name \"%s\" already exists", folderName)
}

// Get the dataset from your repo.
p := lib.GetParams{
Path: ref,
Selector: "",
}
res := lib.GetResult{}
if err = o.DatasetRequests.Get(&p, &res); err != nil {
return err
}

// Create a directory.
if err = os.Mkdir(folderName, os.ModePerm); err != nil {
if err = qfs.AbsPath(&folderName); err != nil {
return err
}

// Create the link file, containing the dataset reference.
lnkp := &lib.LinkParams{
Dir: folderName,
Ref: ref,
}
lnkres := ""
if err = o.FSIMethods.CreateLink(lnkp, &lnkres); err != nil {
return err
}

// Prepare dataset.
// TODO(dlong): Move most of this into FSI?
ds := res.Dataset

// Get individual components out of the dataset.
meta := ds.Meta
ds.Meta = nil
schema := ds.Structure.Schema
ds.Structure.Schema = nil

// Structure is kept in the dataset.
bodyFormat := ds.Structure.Format
ds.Structure.Format = ""
ds.Structure.Qri = ""

// Commit, viz, transform are never checked out.
ds.Commit = nil
ds.Viz = nil
ds.Transform = nil

// Meta component.
if meta != nil {
meta.DropDerivedValues()
if !meta.IsEmpty() {
data, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(folderName, "meta.json"), data, os.ModePerm)
if err != nil {
return err
}
}
}

// Schema component.
if len(schema) > 0 {
data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(folderName, "schema.json"), data, os.ModePerm)
if err != nil {
return err
}
}

// Body component.
bf := ds.BodyFile()
data, err := ioutil.ReadAll(bf)
var res string
err = o.FSIMethods.Checkout(&lib.CheckoutParams{Dir: folderName, Ref: ref}, &res)
if err != nil {
return err
}
ds.BodyPath = ""
var bodyFilename string
switch bodyFormat {
case "csv":
bodyFilename = "body.csv"
case "json":
bodyFilename = "body.json"
default:
return fmt.Errorf("unknown body format: %s", bodyFormat)
}
err = ioutil.WriteFile(filepath.Join(folderName, bodyFilename), data, os.ModePerm)
if err != nil {
return err
}

// Dataset (everything else).
ds.DropDerivedValues()
// TODO(dlong): Should more of these move to DropDerivedValues?
ds.Qri = ""
ds.Name = ""
ds.Peername = ""
ds.PreviousPath = ""
if ds.Structure.IsEmpty() {
ds.Structure = nil
}
if !ds.IsEmpty() {
data, err := json.MarshalIndent(ds, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(folderName, "dataset.json"), data, os.ModePerm)
if err != nil {
return err
}
}

printSuccess(o.Out, "created and linked working directory %s for existing dataset", folderName)
return nil
Expand Down
6 changes: 5 additions & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,11 @@ func (r *TestRepoRoot) Delete() {

// CreateCommandRunner returns a cobra runable command.
func (r *TestRepoRoot) CreateCommandRunner(ctx context.Context) *cobra.Command {
r.streams, _, _, _ = ioes.NewTestIOStreams()
in := &bytes.Buffer{}
out := &bytes.Buffer{}
r.streams = ioes.NewIOStreams(in, out, out)
setNoColor(true)

return NewQriCommand(ctx, r.pathFactory, r.testCrypto, r.streams)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestInitStatusSave(t *testing.T) {
run ` + "`qri save`" + ` to commit this dataset
`
if diff := cmpTextLines(output, expect); diff != "" {
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func TestCheckoutSimpleStatus(t *testing.T) {
working directory clean
`
if diff := cmpTextLines(output, expect); diff != "" {
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}

Expand All @@ -190,7 +190,7 @@ working directory clean
run ` + "`qri save`" + ` to commit this dataset
`
if diff := cmpTextLines(output, expect); diff != "" {
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}

Expand All @@ -214,7 +214,7 @@ run ` + "`qri save`" + ` to commit this dataset
run ` + "`qri save`" + ` to commit this dataset
`
if diff := cmpTextLines(output, expect); diff != "" {
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type SaveOptions struct {

Title string
Message string

Replace bool
ShowValidation bool
Publish bool
Expand Down
Loading

0 comments on commit 6794c33

Please sign in to comment.