Skip to content

Commit

Permalink
refactor(lib.FSIMethods): move fsi stuff into FSIMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 authored and dustmop committed Jul 15, 2019
1 parent 3a0a50e commit 57b665d
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 133 deletions.
1 change: 1 addition & 0 deletions cmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Factory interface {
ProfileMethods() (*lib.ProfileMethods, error)
SearchRequests() (*lib.SearchRequests, error)
RenderRequests() (*lib.RenderRequests, error)
FSIMethods() (*lib.FSIMethods, error)
}

// PathFactory is a function that returns paths to qri & ipfs repos
Expand Down
5 changes: 5 additions & 0 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (t TestFactory) ProfileMethods() (*lib.ProfileMethods, error) {
return lib.NewProfileMethods(t.inst), nil
}

// FSIMethods generates a lib.FSIMethods from internal state
func (t TestFactory) FSIMethods() (*lib.FSIMethods, error) {
return lib.NewFSIMethods(t.inst), nil
}

// SearchRequests generates a lib.SearchRequests from internal state
func (t TestFactory) SearchRequests() (*lib.SearchRequests, error) {
return lib.NewSearchRequests(t.node, t.rpc), nil
Expand Down
72 changes: 49 additions & 23 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"

"github.com/qri-io/dataset"
"github.com/qri-io/ioes"
"github.com/qri-io/qri/fsi"
"github.com/qri-io/qri/lib"
Expand All @@ -33,8 +32,9 @@ func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
},
}

cmd.PersistentFlags().StringVar(&o.Name, "name", "", "name of the dataset")
cmd.PersistentFlags().StringVar(&o.Format, "format", "", "format of dataset")
cmd.Flags().StringVar(&o.Name, "name", "", "name of the dataset")
cmd.Flags().StringVar(&o.Format, "format", "", "format of dataset")
cmd.Flags().StringVar(&o.Link, "link", "", "link this directory to an existing dataset")

return cmd
}
Expand All @@ -43,19 +43,43 @@ func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
type InitOptions struct {
ioes.IOStreams

Name string
Format string
Name string
Format string
Link string

DatasetRequests *lib.DatasetRequests
FSIMethods *lib.FSIMethods
}

// Complete completes a dataset reference
func (o *InitOptions) Complete(f Factory) (err error) {
o.DatasetRequests, err = f.DatasetRequests()
if o.DatasetRequests, err = f.DatasetRequests(); err != nil {
return err
}
o.FSIMethods, err = f.FSIMethods()
return err
}

// Run executes the `init` command
func (o *InitOptions) Run() (err error) {
pwd, err := os.Getwd()
if err != nil {
return err
}

if o.Link != "" {
p := &lib.LinkParams{
Dir: pwd,
Ref: o.Link,
}
res := ""
err = o.FSIMethods.CreateLink(p, &res)
if err == nil {
printSuccess(o.ErrOut, "created link: %s", res)
}
return err
}

if _, err := os.Stat(fsi.QriRefFilename); !os.IsNotExist(err) {
return fmt.Errorf("working directory is already linked, .qri-ref exists")
}
Expand All @@ -77,10 +101,6 @@ func (o *InitOptions) Run() (err error) {
}

// Suggestion for the dataset name defaults to the name of the current directory.
pwd, err := os.Getwd()
if err != nil {
return err
}
suggestDataset := varName.CreateVarNameFromString(filepath.Base(pwd))

// Process flags for inputs, prompt for any that were not provided.
Expand Down Expand Up @@ -116,25 +136,31 @@ func (o *InitOptions) Run() (err error) {
}

// Create the link file, containing the dataset reference.
if err = ioutil.WriteFile(fsi.QriRefFilename, []byte(ref), os.ModePerm); err != nil {
return fmt.Errorf("creating %s file: %s", fsi.QriRefFilename, err)
lnkp := &lib.LinkParams{
Dir: pwd,
Ref: o.Link,
}
lnkres := ""
if err = o.FSIMethods.CreateLink(lnkp, &lnkres); err != nil {
return err
}

// Create a skeleton meta.json file.
meta := dataset.Meta{
Qri: "md:0",
Citations: []*dataset.Citation{},
Description: "enter description here",
Title: "enter title here",
HomeURL: "enter home URL here",
Keywords: []string{"example"},
}
data, err := json.MarshalIndent(meta, "", " ")
if err := ioutil.WriteFile("meta.json", data, os.ModePerm); err != nil {
metaSkeleton := []byte(`{
"title": "",
"description": "",
"keywords": [],
"homeURL": ""
}
`)
if err := ioutil.WriteFile("meta.json", metaSkeleton, os.ModePerm); err != nil {
return err
}

var schema map[string]interface{}
var (
schema map[string]interface{}
data []byte
)
if dsFormat == "csv" {
schema = map[string]interface{}{
"type": "array",
Expand Down
9 changes: 9 additions & 0 deletions cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,12 @@ func (o *QriOptions) ConfigMethods() (m *lib.ConfigMethods, err error) {

return lib.NewConfigMethods(o.inst), nil
}

// FSIMethods generates a lib.FSIMethods from internal state
func (o *QriOptions) FSIMethods() (m *lib.FSIMethods, err error) {
if err = o.Init(); err != nil {
return
}

return lib.NewFSIMethods(o.inst), nil
}
26 changes: 14 additions & 12 deletions cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/qri-io/dataset"
Expand Down Expand Up @@ -100,28 +99,25 @@ type SaveOptions struct {
IsLinkedRef bool
Secrets []string

pwd string
fsids *dataset.Dataset

DatasetRequests *lib.DatasetRequests
FSIMethods *lib.FSIMethods
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *SaveOptions) Complete(f Factory, args []string) (err error) {
o.Ref, err = GetDatasetRefString(f, args, 0)

wd, err := os.Getwd()
if err != nil {
if o.pwd, err = os.Getwd(); err != nil {
return err
}
_, o.IsLinkedRef = fsi.GetLinkedFilesysRef(wd)
_, o.IsLinkedRef = fsi.GetLinkedFilesysRef(o.pwd)

if o.IsLinkedRef {
wd, err := os.Getwd()
if err != nil {
return err
}

o.fsids, _, err = fsi.ReadDir(wd)
o.fsids, _, err = fsi.ReadDir(o.pwd)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,11 +208,17 @@ continue?`, true) {
}
fmt.Fprint(o.Out, string(data))
}
if o.IsLinkedRef {
err = ioutil.WriteFile(fsi.QriRefFilename, []byte(res.String()), os.ModePerm)
if err != nil {

if o.IsLinkedRef && !o.DryRun {
p := &lib.LinkParams{
Dir: o.pwd,
Ref: res.String(),
}
res := ""
if err = o.FSIMethods.UpdateLink(p, &res); err != nil {
return err
}
}

return nil
}
65 changes: 8 additions & 57 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"path/filepath"

"github.com/qri-io/qri/fsi"
"github.com/qri-io/ioes"
"github.com/qri-io/qri/fsi"
"github.com/qri-io/qri/lib"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,15 +38,15 @@ type StatusOptions struct {
ioes.IOStreams

Selection string
Dir string
Dir string

DatasetRequests *lib.DatasetRequests
FSIMethods *lib.FSIMethods
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *StatusOptions) Complete(f Factory, args []string) (err error) {
var ok bool

o.Dir, err = os.Getwd()
if err != nil {
return err
Expand All @@ -56,15 +56,15 @@ func (o *StatusOptions) Complete(f Factory, args []string) (err error) {
if !ok {
return fmt.Errorf("this is not a linked working directory")
}
o.DatasetRequests, err = f.DatasetRequests()

o.FSIMethods, err = f.FSIMethods()
return
}

// Run executes the status command
func (o *StatusOptions) Run() (err error) {
res := []lib.StatusItem{}
if err := o.DatasetRequests.Status(&o.Dir,&res); err != nil {
if err := o.FSIMethods.Status(&o.Dir, &res); err != nil {
printErr(o.ErrOut, err)
return nil
}
Expand All @@ -81,53 +81,4 @@ func (o *StatusOptions) Run() (err error) {
}

return nil

// p := lib.GetParams{
// Path: o.Selection,
// Selector: "",
// }
// res := lib.GetResult{}
// if err = o.DatasetRequests.Get(&p, &res); err != nil {
// printErr(o.ErrOut, fmt.Errorf("no previous version of this dataset"))
// printErr(o.ErrOut, fmt.Errorf("meta.json has modifications"))
// printErr(o.ErrOut, fmt.Errorf("schema.json has modifications"))
// // TODO(dlong): Output status of body
// printSuccess(o.ErrOut, "run `qri save` to commit a new version")
// return nil
// }

// wd, err := os.Getwd()
// if err != nil {
// return err
// }

// if _, _, err = fsi.ReadDir(wd); err != nil {
// return err
// }

// isWorkingDirClean := true

// Check status of meta component
// clean, err := checkCleanStatus(o.ErrOut, "meta.json", res.Dataset.Meta)
// isWorkingDirClean = isWorkingDirClean && clean
// if err != nil {
// return err
// }

// // Check status of schema component
// clean, err = checkCleanStatus(o.ErrOut, "schema.json", res.Dataset.Structure.Schema)
// isWorkingDirClean = isWorkingDirClean && clean
// if err != nil {
// return err
// }

// TODO(dlong): Check status of body

// Done, are we clean?
// if isWorkingDirClean {
// printSuccess(o.ErrOut, "working directory clean!")
// } else {
// printSuccess(o.ErrOut, "run `qri save` to commit a new version")
// }
return nil
}
}
6 changes: 4 additions & 2 deletions fsi/fsi.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ namespace fsi_fbs;


table Link {
// reference flatbuffer
ref: string;
// path is the primary path connected to this dataset
path: string;
// current full dataset reference
ref: string;
// reference alias
alias: string;
}

// flatbuffers don't (currently) support using a vector as a root type
Expand Down
Loading

0 comments on commit 57b665d

Please sign in to comment.