Skip to content

Commit

Permalink
fix: cleanup bugs introduced by recent changes
Browse files Browse the repository at this point in the history
lots of little errors have found their way into the CLI tools, working them out
  • Loading branch information
b5 committed Nov 9, 2017
1 parent 990e4a3 commit c8e5a57
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 271 deletions.
117 changes: 117 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package cmd

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
)

var (
addDsFilepath string
addDsMetaFilepath string
addDsName string
addDsUrl string
addDsPassive bool
)

var datasetAddCmd = &cobra.Command{
Use: "add",
Short: "add a dataset",
Long: `add a dataset to your local namespace based on a resource hash, local file, or url`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
if !strings.HasSuffix(args[0], dsfs.PackageFileDataset.String()) {
ErrExit(fmt.Errorf("invalid dataset path. paths should be /ipfs/[hash]/dataset.json"))
}

if addDsName == "" {
ErrExit(fmt.Errorf("please provide a --name"))
}

r := GetRepo(false)
fs := GetIpfsFilestore(true)
req := core.NewDatasetRequests(fs, r)

root := strings.TrimSuffix(args[0], "/"+dsfs.PackageFileDataset.String())
p := &core.AddParams{
Name: addDsName,
Hash: root,
}
res := &repo.DatasetRef{}
err := req.AddDataset(p, res)
ExitIfErr(err)

// PrintInfo("downloading %s...", root)
// _, err := fs.Fetch(cafs.SourceAny, datastore.NewKey(root))
// ExitIfErr(err)

// err = fs.Pin(datastore.NewKey(root), true)
// ExitIfErr(err)

// err = r.PutName(name, datastore.NewKey(args[0]))
// ExitIfErr(err)

PrintInfo("Successfully added dataset %s : %s", addDsName, res.Path.String())
} else {
initDataset()
}
},
}

func initDataset() {
var (
dataFile, metaFile *os.File
err error
)

if addDsFilepath == "" && addDsUrl == "" {
ErrExit(fmt.Errorf("please provide either a file or a url argument"))
} else if addDsName == "" {
ErrExit(fmt.Errorf("please provide a --name"))
}

dataFile, err = loadFileIfPath(addDsFilepath)
ExitIfErr(err)
metaFile, err = loadFileIfPath(addDsMetaFilepath)
ExitIfErr(err)

r := GetRepo(false)
store := GetIpfsFilestore(false)
req := core.NewDatasetRequests(store, r)

p := &core.InitDatasetParams{
Name: addDsName,
Url: addDsUrl,
DataFilename: filepath.Base(addDsFilepath),
}

// this is because passing nil to interfaces is bad
// see: https://golang.org/doc/faq#nil_error
if dataFile != nil {
p.Data = dataFile
}
if metaFile != nil {
p.Metadata = metaFile
}

ref := &repo.DatasetRef{}
err = req.InitDataset(p, ref)
ExitIfErr(err)
// req.Get(&core.GetDatasetParams{ Name: p.Name }, res)
PrintSuccess("initialized dataset %s: %s", ref.Name, ref.Path.String())
}

func init() {
datasetAddCmd.Flags().StringVarP(&addDsName, "name", "n", "", "name to give dataset")
datasetAddCmd.Flags().StringVarP(&addDsUrl, "url", "u", "", "url to file to initialize from")
datasetAddCmd.Flags().StringVarP(&addDsFilepath, "file", "f", "", "data file to initialize from")
datasetAddCmd.Flags().StringVarP(&addDsMetaFilepath, "meta", "m", "", "dataset metadata file")
datasetAddCmd.Flags().BoolVarP(&addDsPassive, "passive", "p", false, "disable interactive init")
RootCmd.AddCommand(datasetAddCmd)
}
9 changes: 4 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ func userHomeDir() string {
return os.Getenv("HOME")
}

func loadFileIfPath(path string, f *os.File) (err error) {
func loadFileIfPath(path string) (file *os.File, err error) {
if path == "" {
return nil
return nil, nil
}

filepath, err := filepath.Abs(path)
if err != nil {
return err
return nil, err
}

f, err = os.Open(filepath)
return
return os.Open(filepath)
}
138 changes: 0 additions & 138 deletions cmd/dataset.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ var infoCmd = &cobra.Command{
}

func init() {
// RootCmd.AddCommand(infoCmd)
RootCmd.AddCommand(infoCmd)
}
19 changes: 2 additions & 17 deletions cmd/init-ipfs.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package cmd

import (
"flag"
"fmt"
"path/filepath"

ipfs "github.com/qri-io/cafs/ipfs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
initIpfsConfigFile string
// initMetaFile string
// initName string
// initPassive bool
// initRescursive bool
)

// initCmd represents the init command
Expand All @@ -23,20 +16,12 @@ var initIpfsCmd = &cobra.Command{
Short: "Initialize an ipfs repository",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if initFile == "" {
ErrExit(fmt.Errorf("please provide a file argument"))
}

path, err := filepath.Abs(initFile)
ExitIfErr(err)

err = ipfs.InitRepo(path, initIpfsConfigFile)
err := ipfs.InitRepo(viper.GetString(IpfsFsPath), initIpfsConfigFile)
ExitIfErr(err)
},
}

func init() {
flag.Parse()
RootCmd.AddCommand(initIpfsCmd)
initIpfsCmd.Flags().StringVarP(&initIpfsConfigFile, "config", "c", "", "config file for initialization")
}
Loading

0 comments on commit c8e5a57

Please sign in to comment.