Skip to content

Commit

Permalink
feat(status): add initial status check func
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 authored and dustmop committed Jul 8, 2019
1 parent 8e5a203 commit e3eb1c1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
50 changes: 45 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package cmd

import (
"os"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/qri-io/ioes"
"github.com/qri-io/qri/lib"
"github.com/spf13/cobra"
)

// NewInitCommand creates new `qri init` command that connects a directory to
// NewInitCommand creates new `qri init` command that connects a directory to
// the local filesystem
func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
o := &InitOptions{IOStreams: ioStreams}
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"ls"},
Short: "initialize a dataset directory",
Long: ``,
Long: ``,
Example: ``,
Annotations: map[string]string{
"group": "dataset",
},
RunE: func(cmd *cobra.Command, args []string) error {
o.Complete(f)
return o.Run()
},
}
Expand All @@ -37,7 +39,14 @@ func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
type InitOptions struct {
ioes.IOStreams

Peername string
Peername string
DatasetRequests *lib.DatasetRequests
}

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

// Run executes the init command
Expand All @@ -47,5 +56,36 @@ func (o *InitOptions) Run() (err error) {
return err
}

return ioutil.WriteFile(".qri_ref", []byte(fmt.Sprintf("%s/%s", o.Peername, filepath.Base(pwd))), os.ModePerm)
ref := fmt.Sprintf("%s/%s", o.Peername, filepath.Base(pwd))
path := "dataset.yaml"

p := lib.GetParams{
Path: ref,
Selector: "",
}

res := lib.GetResult{}
if err = o.DatasetRequests.Get(&p, &res); err != nil {
if err = ioutil.WriteFile(".qri_ref", []byte(ref), os.ModePerm); err != nil {
return fmt.Errorf("creating dataset reference: %s", err)
}

if _, err := os.Stat(path); os.IsNotExist(err) {
if err := ioutil.WriteFile(path, []byte(blankYamlDataset), os.ModePerm); err != nil {
return err
}
printSuccess(o.Out, "initialized qri dataset %s", path)
return nil
}
return fmt.Errorf("'%s' already exists", path)
}

if _, err := os.Stat(path); os.IsNotExist(err) {
if err := ioutil.WriteFile("dataset.yaml", res.Bytes, os.ModePerm); err != nil {
return err
}
}

ref = fmt.Sprintf("%s/%s@%s%s", o.Peername, filepath.Base(pwd), res.Dataset.Commit.Author.ID, res.Dataset.Path)
return ioutil.WriteFile(".qri_ref", []byte(ref), os.ModePerm)
}
1 change: 1 addition & 0 deletions cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ https://github.com/qri-io/qri/issues`,
NewSaveCommand(opt, ioStreams),
NewSearchCommand(opt, ioStreams),
NewSetupCommand(opt, ioStreams),
NewStatusCommand(opt, ioStreams),
NewUseCommand(opt, ioStreams),
NewUpdateCommand(opt, ioStreams),
NewValidateCommand(opt, ioStreams),
Expand Down
82 changes: 82 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"

"github.com/qri-io/ioes"
"github.com/qri-io/qri/lib"
"github.com/spf13/cobra"
"github.com/qri-io/fill"
)

// NewStatusCommand creates new `qri status` command that statuss datasets for the local peer & others
func NewStatusCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
o := &StatusOptions{IOStreams: ioStreams}
cmd := &cobra.Command{
Use: "status",
Aliases: []string{"ls"},
Short: "Show current dataset status",
Long: ``,
Example: ``,
Annotations: map[string]string{
"group": "dataset",
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, args); err != nil {
return err
}
return o.Run()
},
}

cmd.Flags().StringVarP(&o.Format, "format", "f", "", "set output format [json]")

return cmd
}

// StatusOptions encapsulates state for the Status command
type StatusOptions struct {
ioes.IOStreams

Format string
Selection string

DatasetRequests *lib.DatasetRequests
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *StatusOptions) Complete(f Factory, args []string) (err error) {
o.Selection = PwdSelection()
if o.Selection == "" {
return fmt.Errorf("this is not a qri directory")
}
o.DatasetRequests, err = f.DatasetRequests()
return
}

// Run executes the status command
func (o *StatusOptions) Run() (err error) {
p := lib.GetParams{
Path: o.Selection,
Selector: "",
}
res := lib.GetResult{}
if err = o.DatasetRequests.Get(&p, &res); err != nil {
return err
}

dirData, err := ioutil.ReadFile("dataset.yaml")
if err != nil {
return err
}

if !bytes.Equal(res.Bytes, dirData) {
printErr(o.ErrOut, fmt.Errorf("dataset.yaml has been modified"))
return nil
}

printSuccess(o.ErrOut, "working directory clean!")
return nil
}

0 comments on commit e3eb1c1

Please sign in to comment.