github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/rename.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/qri-io/ioes"
     7  	"github.com/qri-io/qri/errors"
     8  	"github.com/qri-io/qri/lib"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // NewRenameCommand creates a new `qri rename` cobra command for renaming datasets
    13  func NewRenameCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
    14  	o := &RenameOptions{IOStreams: ioStreams}
    15  	cmd := &cobra.Command{
    16  		Use:     "rename",
    17  		Aliases: []string{"mv"},
    18  		Short:   "change the name of a dataset",
    19  		Long: `Rename changes the name of a dataset.
    20  
    21  Note that if someone has added your dataset to their qri node, and then
    22  you rename your local dataset, your peer's version of your dataset will
    23  not have the updated name. While this won't break anything, it will
    24  confuse anyone who has added your dataset before the change. Try to keep
    25  renames to a minimum.`,
    26  		Example: `  # Rename a dataset named annual_pop to annual_population:
    27    $ qri rename me/annual_pop me/annual_population`,
    28  		Annotations: map[string]string{
    29  			"group": "dataset",
    30  		},
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			if err := o.Complete(f, args); err != nil {
    33  				return err
    34  			}
    35  			if err := o.Validate(); err != nil {
    36  				return err
    37  			}
    38  			return o.Run()
    39  		},
    40  	}
    41  
    42  	return cmd
    43  }
    44  
    45  // RenameOptions encapsulates state for the rename command
    46  type RenameOptions struct {
    47  	ioes.IOStreams
    48  
    49  	From string
    50  	To   string
    51  
    52  	inst *lib.Instance
    53  }
    54  
    55  // Complete adds any missing configuration that can only be added just before calling Run
    56  func (o *RenameOptions) Complete(f Factory, args []string) (err error) {
    57  	if len(args) == 2 {
    58  		o.From = args[0]
    59  		o.To = args[1]
    60  	}
    61  	o.inst, err = f.Instance()
    62  	return
    63  }
    64  
    65  // Validate checks that all user input is valid
    66  func (o *RenameOptions) Validate() error {
    67  	if o.From == "" || o.To == "" {
    68  		return errors.New(lib.ErrBadArgs, "please provide two dataset names, the original and the new name, for example:\n    $ qri rename me/old_name me/new_name\nsee `qri rename --help` for more details")
    69  	}
    70  	return nil
    71  }
    72  
    73  // Run executes the rename command
    74  func (o *RenameOptions) Run() error {
    75  	p := &lib.RenameParams{
    76  		Current: o.From,
    77  		Next:    o.To,
    78  	}
    79  	ctx := context.TODO()
    80  	res, err := o.inst.WithSource("local").Dataset().Rename(ctx, p)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	printSuccess(o.Out, "renamed dataset to %s", res.Name)
    86  	return nil
    87  }