Skip to content

Commit

Permalink
feat(DefaultDatasets): first cuts on requesting default datasets
Browse files Browse the repository at this point in the history
when no config file is present on any CLI command, attempt to
spin up a repo & grap default hashes. This'll definitly break
stuff but we got a demo today so whatever

closes #161
  • Loading branch information
b5 committed Dec 8, 2017
1 parent 92e42ae commit 05a9e2f
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 42 deletions.
8 changes: 5 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ var datasetAddCmd = &cobra.Command{
ErrExit(fmt.Errorf("please provide a --name"))
}

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

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

PrintInfo("Successfully added dataset %s: %s", addDsName, res.Path.String())
Expand All @@ -69,7 +70,8 @@ func initDataset() {
metaFile, err = loadFileIfPath(addDsMetaFilepath)
ExitIfErr(err)

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

p := &core.InitDatasetParams{
Name: addDsName,
Expand Down
13 changes: 8 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func init() {
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
func initConfig() (created bool) {
var err error
home := userHomeDir()
SetNoColor()

Expand Down Expand Up @@ -84,11 +85,13 @@ func initConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(qriPath) // add QRI_PATH env var

err := EnsureConfigFile()
created, err = EnsureConfigFile()
ExitIfErr(err)

err = viper.ReadInConfig()
ExitIfErr(err)

return
}

func configFilepath() string {
Expand All @@ -99,12 +102,12 @@ func configFilepath() string {
return path
}

func EnsureConfigFile() error {
func EnsureConfigFile() (bool, error) {
if _, err := os.Stat(configFilepath()); os.IsNotExist(err) {
fmt.Println("writing config file")
return WriteConfigFile(defaultCfg)
return true, WriteConfigFile(defaultCfg)
}
return nil
return false, nil
}

func WriteConfigFile(cfg *Config) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var infoCmd = &cobra.Command{
}
}

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

for i, arg := range args {
rt, ref := dsfs.RefType(arg)
Expand Down
10 changes: 1 addition & 9 deletions cmd/init-ipfs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/ipfs/go-datastore"
ipfs "github.com/qri-io/cafs/ipfs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,20 +10,13 @@ var (
initIpfsConfigFile string
)

// defaultDatasets is a hard-coded dataset added when a new qri repo is created
// this hash must always be available
var defaultDatasets = []datastore.Key{
// fivethirtyeight comic characters
datastore.NewKey("/ipfs/QmcqkHFA2LujZxY38dYZKmxsUstN4unk95azBjwEhwrnM6"),
}

// initCmd represents the init command
var initIpfsCmd = &cobra.Command{
Use: "init-ipfs",
Short: "Initialize an ipfs repository",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
err := ipfs.InitRepo(viper.GetString(IpfsFsPath), initIpfsConfigFile, defaultDatasets)
err := ipfs.InitRepo(viper.GetString(IpfsFsPath), initIpfsConfigFile)
ExitIfErr(err)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var datasetRemoveCmd = &cobra.Command{
ErrExit(fmt.Errorf("please specify a dataset path or name to get the info of"))
}

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

for _, arg := range args {
rt, ref := dsfs.RefType(arg)
Expand Down
5 changes: 3 additions & 2 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ var datasetRenameCmd = &cobra.Command{
ErrExit(fmt.Errorf("please provide current & new dataset names"))
}

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)
p := &core.RenameParams{
Current: args[0],
New: args[1],
}
res := &repo.DatasetRef{}
err := req.Rename(p, res)
err = req.Rename(p, res)
ExitIfErr(err)

PrintSuccess("renamed dataset %s", res.Name)
Expand Down
41 changes: 26 additions & 15 deletions cmd/repo.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"fmt"
"net"
"net/rpc"
"strings"

ipfs "github.com/qri-io/cafs/ipfs"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/fs"
"github.com/spf13/viper"
Expand All @@ -29,8 +31,25 @@ func GetRepo(online bool) repo.Repo {
return r
}

func GetIpfsFilestore(online bool) *ipfs.Filestore {
fs, err := ipfs.NewFilestore(func(cfg *ipfs.StoreCfg) {
cfg.FsRepoPath = viper.GetString(IpfsFsPath)
cfg.Online = online
})
ExitIfErr(err)
return fs
}

func DatasetRequests(online bool) (*core.DatasetRequests, error) {
r, cli, err := RepoOrClient(online)
if err != nil {
return nil, err
}
return core.NewDatasetRequests(r, cli), nil
}

// RepoOrClient returns either a
func RepoOrClient(online bool) (repo.Repo, *rpc.Client) {
func RepoOrClient(online bool) (repo.Repo, *rpc.Client, error) {
if fs, err := ipfs.NewFilestore(func(cfg *ipfs.StoreCfg) {
cfg.FsRepoPath = viper.GetString(IpfsFsPath)
cfg.Online = online
Expand All @@ -41,26 +60,18 @@ func RepoOrClient(online bool) (repo.Repo, *rpc.Client) {
}

r, err := fs_repo.NewRepo(fs, viper.GetString(QriRepoPath), id)
ExitIfErr(err)
return r, nil
return r, nil, err

} else if strings.Contains(err.Error(), "lock") {
// TODO - bad bad hardcode
conn, err := net.Dial("tcp", ":2504")
if err != nil {
ErrExit(err)
return nil, nil, err
}
return nil, rpc.NewClient(conn)
return nil, rpc.NewClient(conn), nil
} else {
ErrExit(err)
return nil, nil, err
}
return nil, nil
}

func GetIpfsFilestore(online bool) *ipfs.Filestore {
fs, err := ipfs.NewFilestore(func(cfg *ipfs.StoreCfg) {
cfg.FsRepoPath = viper.GetString(IpfsFsPath)
cfg.Online = online
})
ExitIfErr(err)
return fs
return nil, nil, fmt.Errorf("badbadnotgood")
}
44 changes: 43 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package cmd

import (
"flag"
"fmt"
"github.com/ipfs/go-datastore"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,8 +46,46 @@ func Execute() {

func init() {
flag.Parse()
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initialize)

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $QRI_PATH/config.json)")
RootCmd.PersistentFlags().BoolVarP(&noColor, "no-color", "c", false, "disable colorized output")
}

func initialize() {
created := initConfig()
if created {
go addDefaultDatasets()
}
}

// defaultDatasets is a hard-coded dataset added when a new qri repo is created
// these hashes should always/highly available
var defaultDatasets = map[string]datastore.Key{
// fivethirtyeight comic characters
"comic_characters": datastore.NewKey("/ipfs/QmcqkHFA2LujZxY38dYZKmxsUstN4unk95azBjwEhwrnM6/dataset.json"),
}

// Init sets up a repository with sensible defaults
func addDefaultDatasets() error {
req, err := DatasetRequests(true)
if err != nil {
return err
}

for name, ds := range defaultDatasets {
fmt.Printf("attempting to add default dataset: %s\n", ds.String())
res := &repo.DatasetRef{}
err := req.AddDataset(&core.AddParams{
Hash: ds.String(),
Name: name,
}, res)
if err != nil {
fmt.Printf("add dataset %s error: %s\n", ds.String(), err.Error())
return err
}
fmt.Printf("added default dataset: %s\n", ds.String())
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func initRepoIfEmpty(repoPath, configPath string) error {
if err := os.MkdirAll(repoPath, os.ModePerm); err != nil {
return err
}
if err := ipfs.InitRepo(repoPath, configPath, defaultDatasets); err != nil {
if err := ipfs.InitRepo(repoPath, configPath); err != nil {
return err
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ var updateCmd = &cobra.Command{
ErrExit(fmt.Errorf("either a metadata or data option is required"))
}

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

p := &core.GetDatasetParams{
Name: args[0],
Expand Down
7 changes: 5 additions & 2 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ and check each of it's rows against the constraints listed
in the dataset's fields.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

for _, arg := range args {
rt, ref := dsfs.RefType(arg)
p := &core.ValidateDatasetParams{}
Expand Down Expand Up @@ -78,7 +80,8 @@ func validateDataset() {
metaFile, err = loadFileIfPath(validateDsMetaFilepath)
ExitIfErr(err)

req := core.NewDatasetRequests(RepoOrClient(false))
req, err := DatasetRequests(false)
ExitIfErr(err)

p := &core.ValidateDatasetParams{
Name: validateDsName,
Expand Down
15 changes: 14 additions & 1 deletion p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ import (
// One day it would be super nice to bootstrap from a stored history & only
// use these for first-round bootstrapping.
var DefaultBootstrapAddresses = []string{
"/ip4/35.192.124.143/tcp/4001/ipfs/QmQffqhgce94UFS9mSvvqcAWXQNr1bcZRM659VFakySair",
// "/ip4/35.192.124.143/tcp/4001/ipfs/QmQffqhgce94UFS9mSvvqcAWXQNr1bcZRM659VFakySair",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
"/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
"/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
"/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
}

// Bootstrap samples a subset of peers & requests their peers list
Expand Down

0 comments on commit 05a9e2f

Please sign in to comment.