From e3eb1c172f7c8007adea78054f143444e9ca5eb2 Mon Sep 17 00:00:00 2001 From: b5 Date: Tue, 2 Jul 2019 13:02:24 -0400 Subject: [PATCH] feat(status): add initial status check func --- cmd/init.go | 50 +++++++++++++++++++++++++++---- cmd/qri.go | 1 + cmd/status.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 cmd/status.go diff --git a/cmd/init.go b/cmd/init.go index 538c962a2..ae08737b5 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,16 +1,17 @@ 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} @@ -18,12 +19,13 @@ func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *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() }, } @@ -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 @@ -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) } diff --git a/cmd/qri.go b/cmd/qri.go index 8167b3291..0050d4935 100644 --- a/cmd/qri.go +++ b/cmd/qri.go @@ -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), diff --git a/cmd/status.go b/cmd/status.go new file mode 100644 index 000000000..9e549a0cb --- /dev/null +++ b/cmd/status.go @@ -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 +}