Skip to content

Commit

Permalink
refactor(lib.AbsPath): deprecate lib.AbsPath, use qfs.AbsPath
Browse files Browse the repository at this point in the history
AbsPath is a filesystem concern, and belongs in the qfs package.

BREAKING CHANGE:
lib.AbsPath is removed, use github.com/qri-io/qfs.AbsPath instead
  • Loading branch information
b5 committed Apr 5, 2019
1 parent 7ab7476 commit 112362a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 94 deletions.
5 changes: 3 additions & 2 deletions cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/qri-io/dataset"
"github.com/qri-io/ioes"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -106,12 +107,12 @@ func (o *SaveOptions) Complete(f Factory, args []string) (err error) {
// `qri connect` in a different terminal, and that instance is in a different directory;
// that instance won't correctly find the body file we want to load if it's not absolute.
for i := range o.FilePaths {
if err := lib.AbsPath(&o.FilePaths[i]); err != nil {
if err := qfs.AbsPath(&o.FilePaths[i]); err != nil {
return err
}
}

if err := lib.AbsPath(&o.BodyPath); err != nil {
if err := qfs.AbsPath(&o.BodyPath); err != nil {
return fmt.Errorf("body file: %s", err)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dsio"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/base"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
Expand Down Expand Up @@ -54,7 +55,7 @@ type ExportParams struct {
func (r *ExportRequests) Export(p *ExportParams, fileWritten *string) (err error) {
if p.TargetDir == "" {
p.TargetDir = "."
if err = AbsPath(&p.TargetDir); err != nil {
if err = qfs.AbsPath(&p.TargetDir); err != nil {
return err
}
}
Expand Down
54 changes: 6 additions & 48 deletions lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,6 @@ import (
"gopkg.in/yaml.v2"
)

// AbsPath adjusts the provided string to a path lib functions can work with
// because paths for Qri can come from the local filesystem, an http url, or
// the distributed web, Absolutizing is a little tricky
//
// If lib in put params call for a path, running input through AbsPath before
// calling a lib function should help reduce errors. calling AbsPath on empty
// string has no effect
func AbsPath(path *string) (err error) {
if *path == "" {
return
}

*path = strings.TrimSpace(*path)
p := *path

// bail on urls and ipfs hashes
pk := pathKind(p)
if pk == "http" || pk == "ipfs" {
return
}

// TODO - perform tilda (~) expansion
if filepath.IsAbs(p) {
return
}
*path, err = filepath.Abs(p)
return
}

func pathKind(path string) string {
if path == "" {
return "none"
} else if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return "http"
} else if strings.HasPrefix(path, "/ipfs") {
return "ipfs"
} else if strings.HasPrefix(path, "/map") || strings.HasPrefix(path, "/cafs") {
return "cafs"
}
return "file"
}

// 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).
Expand Down Expand Up @@ -96,7 +54,7 @@ func ReadDatasetFiles(pathList ...string) (*dataset.Dataset, error) {
// 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 pathKind(path) {
switch qfs.PathKind(path) {
case "http":
// currently the only supported type of file url is a zip archive
resp, err := http.Get(path)
Expand All @@ -114,7 +72,7 @@ func readSingleFile(path string) (*dataset.Dataset, string, error) {
case "ipfs":
return nil, "", fmt.Errorf("reading dataset files from IPFS currently unsupported")

case "file":
case "local":
f, err := os.Open(path)
if err != nil {
return nil, "", err
Expand Down Expand Up @@ -181,7 +139,7 @@ func readSingleFile(path string) (*dataset.Dataset, string, error) {
return nil, "", fmt.Errorf("error, unrecognized file extension: \"%s\"", fileExt)
}
default:
return nil, "", fmt.Errorf("error, unknown path kind: \"%s\"", pathKind(path))
return nil, "", fmt.Errorf("error, unknown path kind: \"%s\"", qfs.PathKind(path))
}
}

Expand Down Expand Up @@ -239,13 +197,13 @@ func fillDatasetOrComponent(fields map[string]interface{}, path string, ds *data
// their absolute counterpart
func absDatasetPaths(path string, dsp *dataset.Dataset) {
base := filepath.Dir(path)
if dsp.BodyPath != "" && pathKind(dsp.BodyPath) == "file" && !filepath.IsAbs(dsp.BodyPath) {
if dsp.BodyPath != "" && qfs.PathKind(dsp.BodyPath) == "local" && !filepath.IsAbs(dsp.BodyPath) {
dsp.BodyPath = filepath.Join(base, dsp.BodyPath)
}
if dsp.Transform != nil && pathKind(dsp.Transform.ScriptPath) == "file" && !filepath.IsAbs(dsp.Transform.ScriptPath) {
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 && pathKind(dsp.Viz.ScriptPath) == "file" && !filepath.IsAbs(dsp.Viz.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)
}
}
44 changes: 1 addition & 43 deletions lib/file_test.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
package lib

import (
"os"
"path/filepath"
"testing"

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

func TestAbsPath(t *testing.T) {
tmp, err := filepath.Abs(os.TempDir())
if err != nil {
t.Fatal(err)
}

pathAbs, err := filepath.Abs("relative/path/data.yaml")
if err != nil {
t.Fatal(err)
}

httpAbs, err := filepath.Abs("http_got/relative/dataset.yaml")
if err != nil {
t.Fatal(err)
}

cases := []struct {
in, out, err string
}{
{"", "", ""},
{"http://example.com/zipfile.zip", "http://example.com/zipfile.zip", ""},
{"https://example.com/zipfile.zip", "https://example.com/zipfile.zip", ""},
{"relative/path/data.yaml", pathAbs, ""},
{"http_got/relative/dataset.yaml", httpAbs, ""},
{"/ipfs", "/ipfs", ""},
{tmp, tmp, ""},
}

for i, c := range cases {
got := c.in
err := AbsPath(&got)
if !(err == nil && c.err == "" || (err != nil && c.err == err.Error())) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}

func TestReadDatasetFile(t *testing.T) {
func TestReadDatasetFiles(t *testing.T) {
cases := []struct {
description string
path string
Expand Down

0 comments on commit 112362a

Please sign in to comment.