Skip to content

Commit

Permalink
feat(restore): Restore command for restoring component files in FSI
Browse files Browse the repository at this point in the history
Restore command is used to restore components files in the working directory from the repo. Checkout is only used for fresh checkouts. Refactor a lot of the FSI tests to make them shorter, moving common functionality into a new FSITestRunner struct.

A future PR will add API support for restore.
  • Loading branch information
dustmop committed Aug 27, 2019
1 parent 0883d8d commit 620d95d
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 391 deletions.
112 changes: 25 additions & 87 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/qri-io/ioes"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/repo"
"github.com/qri-io/varName"
"github.com/spf13/cobra"
)

Expand All @@ -17,14 +17,16 @@ func NewCheckoutCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
o := &CheckoutOptions{IOStreams: ioStreams}
cmd := &cobra.Command{
Use: "checkout",
Short: "checkout created a linked directory and writes dataset files to that directory",
Short: "checkout creates a linked directory and writes dataset files to that directory",
Long: ``,
Example: ``,
Annotations: map[string]string{
"group": "dataset",
},
RunE: func(cmd *cobra.Command, args []string) error {
o.Complete(f, args)
if err := o.Complete(f, args); err != nil {
return err
}
return o.Run()
},
}
Expand All @@ -37,107 +39,43 @@ type CheckoutOptions struct {
ioes.IOStreams

Refs *RefSelect
Args []string

FSIMethods *lib.FSIMethods
}

// Complete completes a the command
// Complete configures the checkout command
func (o *CheckoutOptions) Complete(f Factory, args []string) (err error) {
o.Refs, err = GetCurrentRefSelect(f, []string{}, 1)
if err == repo.ErrEmptyRef {
// Not an error, must be a fresh checkout.
o.Refs = NewExplicitRefSelect("")
} else if err != nil {
o.Refs, err = GetCurrentRefSelect(f, args, 1)
if err != nil {
return err
}
o.Args = args
o.FSIMethods, err = f.FSIMethods()
return err
}

// Run executes the `checkout` command
func (o *CheckoutOptions) Run() (err error) {
var freshDatasetRef, refPath, componentName, folderName string

// Process arguments to get dataset name, component name, and/or ref path.
for _, arg := range o.Args {
if strings.HasPrefix(arg, "@/ipfs/") {
if refPath != "" {
return fmt.Errorf("cannot provide more than one ref Path")
}
refPath = arg
continue
}

if isDatasetField.MatchString(arg) {
if componentName != "" {
return fmt.Errorf("cannot provide more than one dataset field")
}
componentName = arg
continue
}

pos := strings.Index(arg, "/")
if pos > -1 {
if freshDatasetRef != "" {
return fmt.Errorf("cannot provide more than one dataset name")
}
freshDatasetRef = arg
// Derive directory name from the dataset name.
folderName = freshDatasetRef[pos+1:]
continue
}

return fmt.Errorf("unknown argument \"%s\"", arg)
if !o.Refs.IsExplicit() {
return fmt.Errorf("checkout requires an explicitly provided dataset ref")
}
ref := o.Refs.Ref()

if !o.Refs.IsLinked() {
// Fresh checkout
if freshDatasetRef == "" {
return fmt.Errorf("TODO A")
}
if refPath != "" || componentName != "" {
return fmt.Errorf("fresh checkout of a dataset can't have a path or component name")
}

if err = qfs.AbsPath(&folderName); err != nil {
return err
}

var res string
err = o.FSIMethods.Checkout(&lib.CheckoutParams{
Dir: folderName,
Ref: freshDatasetRef,
}, &res)
if err != nil {
return err
}
printSuccess(o.Out, "created and linked working directory %s for existing dataset",
folderName)
} else {
// Historic checkout
if freshDatasetRef != "" {
return fmt.Errorf("TODO B")
}
if refPath == "" && componentName == "" {
return fmt.Errorf("historic checkout needs either a path or component name")
}
// Derive directory name from the dataset name.
pos := strings.Index(ref, "/")
if pos == -1 {
return fmt.Errorf("expect '/' in dataset ref")
}
folderName := varName.CreateVarNameFromString(ref[pos+1:])

ref := o.Refs.Ref()
if refPath != "" {
ref += refPath
}
if err = qfs.AbsPath(&folderName); err != nil {
return err
}

var res string
err = o.FSIMethods.CheckoutHistoric(&lib.CheckoutParams{
Ref: ref,
Component: componentName,
}, &res)
if err != nil {
return err
}
printSuccess(o.Out, "TODO dataset")
var res string
err = o.FSIMethods.Checkout(&lib.CheckoutParams{Dir: folderName, Ref: ref}, &res)
if err != nil {
return err
}
printSuccess(o.Out, "created and linked working directory %s for existing dataset", folderName)
return nil
}
1 change: 1 addition & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ func (r *TestRepoRoot) CreateCommandRunner(ctx context.Context) *cobra.Command {
return cmd
}

// GetOutput returns the output from the previously executed command.
func (r *TestRepoRoot) GetOutput() string {
buffer, ok := r.streams.Out.(*bytes.Buffer)
if ok {
Expand Down
Loading

0 comments on commit 620d95d

Please sign in to comment.