Skip to content

Commit

Permalink
refactor(context): add context arguments
Browse files Browse the repository at this point in the history
Merge pull request #928 from qri-io/refactor_add_ctx
  • Loading branch information
b5 authored Sep 14, 2019
2 parents 1e462d3 + 03b06a3 commit 5310ebf
Show file tree
Hide file tree
Showing 80 changed files with 468 additions and 323 deletions.
7 changes: 4 additions & 3 deletions actions/actions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"context"
"encoding/base64"
"fmt"
"os"
Expand Down Expand Up @@ -100,7 +101,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(context.Background(), node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -113,7 +114,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(context.Background(), node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -129,7 +130,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(context.Background(), node, tc.Input, nil, nil, SaveDatasetSwitches{Pin: true, ShouldRender: true})
if err != nil {
t.Fatal(err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion actions/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package actions

import (
"bytes"
"context"
"testing"

"github.com/qri-io/dataset"
"github.com/qri-io/qri/base"
)

func TestGetBody(t *testing.T) {
ctx := context.Background()
node := newTestNode(t)
ref := addCitiesDataset(t, node)

ds, err := base.ReadDatasetPath(node.Repo, ref.String())
ds, err := base.ReadDatasetPath(ctx, node.Repo, ref.String())
if err != nil {
t.Fatal(err)
}
Expand Down
33 changes: 17 additions & 16 deletions actions/dataset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"context"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -29,14 +30,14 @@ type SaveDatasetSwitches struct {
}

// SaveDataset initializes a dataset from a dataset pointer and data file
func SaveDataset(node *p2p.QriNode, changes *dataset.Dataset, secrets map[string]string, scriptOut io.Writer, sw SaveDatasetSwitches) (ref repo.DatasetRef, err error) {
func SaveDataset(ctx context.Context, node *p2p.QriNode, changes *dataset.Dataset, secrets map[string]string, scriptOut io.Writer, sw SaveDatasetSwitches) (ref repo.DatasetRef, err error) {
var (
prevPath string
pro *profile.Profile
r = node.Repo
)

prev, mutable, prevPath, err := base.PrepareDatasetSave(r, changes.Peername, changes.Name)
prev, mutable, prevPath, err := base.PrepareDatasetSave(ctx, r, changes.Peername, changes.Name)
if err != nil {
return
}
Expand All @@ -60,7 +61,7 @@ func SaveDataset(node *p2p.QriNode, changes *dataset.Dataset, secrets map[string
mutateCheck := mutatedComponentsFunc(changes)

changes.Transform.Secrets = secrets
if err = ExecTransform(node, changes, prev, scriptOut, mutateCheck); err != nil {
if err = ExecTransform(ctx, node, changes, prev, scriptOut, mutateCheck); err != nil {
return
}
// changes.Transform.SetScriptFile(mutable.Transform.ScriptFile())
Expand Down Expand Up @@ -108,19 +109,19 @@ func SaveDataset(node *p2p.QriNode, changes *dataset.Dataset, secrets map[string
// let's make history, if it exists
changes.PreviousPath = prevPath

return base.CreateDataset(r, node.LocalStreams, changes, prev, sw.DryRun, sw.Pin, sw.Force, sw.ShouldRender)
return base.CreateDataset(ctx, r, node.LocalStreams, changes, prev, sw.DryRun, sw.Pin, sw.Force, sw.ShouldRender)
}

// UpdateRemoteDataset brings a reference to the latest version, syncing to the
// latest history it can find over p2p & via any configured registry
func UpdateRemoteDataset(node *p2p.QriNode, ref *repo.DatasetRef, pin bool) (res repo.DatasetRef, err error) {
func UpdateRemoteDataset(ctx context.Context, node *p2p.QriNode, ref *repo.DatasetRef, pin bool) (res repo.DatasetRef, err error) {
var ldr base.LogDiffResult
ldr, err = node.RequestLogDiff(ref)
ldr, err = node.RequestLogDiff(ctx, ref)
if err != nil {
return
}
for _, add := range ldr.Add {
if err = base.FetchDataset(node.Repo, &add, true, false); err != nil {
if err = base.FetchDataset(ctx, node.Repo, &add, true, false); err != nil {
return
}
}
Expand All @@ -133,14 +134,14 @@ func UpdateRemoteDataset(node *p2p.QriNode, ref *repo.DatasetRef, pin bool) (res
}

// AddDataset fetches & pins a dataset to the store, adding it to the list of stored refs
func AddDataset(node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *repo.DatasetRef) (err error) {
func AddDataset(ctx context.Context, node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *repo.DatasetRef) (err error) {
log.Debugf("add dataset %s. remoteAddr: %s", ref.String(), remoteAddr)
if !ref.Complete() {
// TODO (ramfox): we should check to see if the dataset already exists locally
// unfortunately, because of the nature of the ipfs filesystem commands, we don't
// know if files we fetch are local only or possibly coming from the network.
// instead, for now, let's just always try to add
if _, err := ResolveDatasetRef(node, rc, remoteAddr, ref); err != nil {
if _, err := ResolveDatasetRef(ctx, node, rc, remoteAddr, ref); err != nil {
return err
}
}
Expand Down Expand Up @@ -171,13 +172,13 @@ func AddDataset(node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *re
responses <- res
}()

if err := rc.PullDataset(node.Context(), ref, remoteAddr); err != nil {
if err := rc.PullDataset(ctx, ref, remoteAddr); err != nil {
res.Error = err
return
}
node.LocalStreams.PrintErr("🗼 fetched from registry\n")
if pinner, ok := node.Repo.Store().(cafs.Pinner); ok {
err := pinner.Pin(ref.Path, true)
err := pinner.Pin(ctx, ref.Path, true)
res.Error = err
}
}(refCopy)
Expand All @@ -186,7 +187,7 @@ func AddDataset(node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *re
if node.Online {
tasks++
go func() {
err := base.FetchDataset(node.Repo, ref, true, true)
err := base.FetchDataset(ctx, node.Repo, ref, true, true)
responses <- addResponse{
Ref: ref,
Error: err,
Expand Down Expand Up @@ -225,13 +226,13 @@ func AddDataset(node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *re
return err
}

prevRef.Dataset, err = dsfs.LoadDataset(node.Repo.Store(), prevRef.Path)
prevRef.Dataset, err = dsfs.LoadDataset(ctx, node.Repo.Store(), prevRef.Path)
if err != nil {
log.Debug(err.Error())
return fmt.Errorf("error loading repo dataset: %s", prevRef.Path)
}

ref.Dataset, err = dsfs.LoadDataset(node.Repo.Store(), ref.Path)
ref.Dataset, err = dsfs.LoadDataset(ctx, node.Repo.Store(), ref.Path)
if err != nil {
log.Debug(err.Error())
return fmt.Errorf("error loading added dataset: %s", ref.Path)
Expand Down Expand Up @@ -309,7 +310,7 @@ func ModifyDataset(node *p2p.QriNode, current, new *repo.DatasetRef, isRename bo
}

// DeleteDataset removes a dataset from the store
func DeleteDataset(node *p2p.QriNode, ref *repo.DatasetRef) (err error) {
func DeleteDataset(ctx context.Context, node *p2p.QriNode, ref *repo.DatasetRef) (err error) {
r := node.Repo

if err = repo.CanonicalizeDatasetRef(r, ref); err != nil {
Expand Down Expand Up @@ -345,7 +346,7 @@ func DeleteDataset(node *p2p.QriNode, ref *repo.DatasetRef) (err error) {
return err
}

if err = base.UnpinDataset(r, *ref); err != nil && err != repo.ErrNotPinner {
if err = base.UnpinDataset(ctx, r, *ref); err != nil && err != repo.ErrNotPinner {
return err
}

Expand Down
7 changes: 4 additions & 3 deletions actions/dataset_head.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"context"
"fmt"

"github.com/qri-io/qri/base"
Expand All @@ -10,7 +11,7 @@ import (

// DatasetHead gets commit, structure, meta, viz & transform for a given reference, either
// from the local repo or by asking peers for it, modifying the input ref on success
func DatasetHead(node *p2p.QriNode, ds *repo.DatasetRef) error {
func DatasetHead(ctx context.Context, node *p2p.QriNode, ds *repo.DatasetRef) error {
err := repo.CanonicalizeDatasetRef(node.Repo, ds)
if err != nil && err != repo.ErrNotFound {
log.Debug(err.Error())
Expand All @@ -20,8 +21,8 @@ func DatasetHead(node *p2p.QriNode, ds *repo.DatasetRef) error {
if node == nil {
return fmt.Errorf("%s, and no p2p connection", err.Error())
}
return node.RequestDataset(ds)
return node.RequestDataset(ctx, ds)
}

return base.ReadDataset(node.Repo, ds)
return base.ReadDataset(ctx, node.Repo, ds)
}
8 changes: 6 additions & 2 deletions actions/dataset_head_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package actions

import "testing"
import (
"context"
"testing"
)

func TestDatasetHead(t *testing.T) {
ctx := context.Background()
node := newTestNode(t)
ref := addCitiesDataset(t, node)

if err := DatasetHead(node, &ref); err != nil {
if err := DatasetHead(ctx, node, &ref); err != nil {
t.Error(err.Error())
}
if ref.Dataset == nil {
Expand Down
5 changes: 3 additions & 2 deletions actions/dataset_ref.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"context"
"fmt"

"github.com/qri-io/qri/p2p"
Expand All @@ -21,7 +22,7 @@ import (
// control over local only and network actions. Once we have those, we can attempt
// to load the dataset locally, if it error with DatasetNotFound, or something similar
// we will know that the dataset does not exist locally
func ResolveDatasetRef(node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *repo.DatasetRef) (local bool, err error) {
func ResolveDatasetRef(ctx context.Context, node *p2p.QriNode, rc *remote.Client, remoteAddr string, ref *repo.DatasetRef) (local bool, err error) {
if err := repo.CanonicalizeDatasetRef(node.Repo, ref); err == nil && ref.Path != "" {
return true, nil
} else if err != nil && err != repo.ErrNotFound && err != profile.ErrNotFound {
Expand Down Expand Up @@ -57,7 +58,7 @@ func ResolveDatasetRef(node *p2p.QriNode, rc *remote.Client, remoteAddr string,
if node.Online {
tasks++
go func() {
err := node.ResolveDatasetRef(ref)
err := node.ResolveDatasetRef(ctx, ref)
log.Debugf("p2p ref res: %s", ref)
if !ref.Complete() && err == nil {
err = fmt.Errorf("p2p network responded with incomplete reference")
Expand Down
8 changes: 4 additions & 4 deletions actions/dataset_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestResolveDatasetRef(t *testing.T) {
peers[i] = node.(*p2p.QriNode)
}

if _, err := ResolveDatasetRef(peers[0], nil, "", &repo.DatasetRef{}); err != repo.ErrEmptyRef {
if _, err := ResolveDatasetRef(ctx, peers[0], nil, "", &repo.DatasetRef{}); err != repo.ErrEmptyRef {
t.Errorf("expected repo.ErrEmptRef, got: %s", err)
}

Expand All @@ -44,15 +44,15 @@ func TestResolveDatasetRef(t *testing.T) {

// TODO - fix this lie
peers[2].Online = false
local, err := ResolveDatasetRef(peers[2], nil, "", in)
local, err := ResolveDatasetRef(ctx, peers[2], nil, "", in)
if err == nil {
t.Error("expected offline node to not be able to resolve non-local ref")
}
if local != false {
t.Error("expected local to equal false")
}

if local, err = ResolveDatasetRef(peers[0], nil, "", in); err != nil {
if local, err = ResolveDatasetRef(ctx, peers[0], nil, "", in); err != nil {
t.Error(err.Error())
}
if local != false {
Expand All @@ -62,7 +62,7 @@ func TestResolveDatasetRef(t *testing.T) {
t.Errorf("returned ref mismatch. expected: %s, got: %s", expect, in.String())
}

if local, err = ResolveDatasetRef(peers[1], nil, "", in); err != nil {
if local, err = ResolveDatasetRef(ctx, peers[1], nil, "", in); err != nil {
t.Error(err.Error())
}
if local != true {
Expand Down
Loading

0 comments on commit 5310ebf

Please sign in to comment.