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

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/qri-io/ioes"
     7  	"github.com/qri-io/qri/lib"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  // NewPushCommand creates a `qri push` subcommand
    12  func NewPushCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
    13  	o := &PushOptions{IOStreams: ioStreams}
    14  	cmd := &cobra.Command{
    15  		Use:     "push DATASET [DATASET...] [flags]",
    16  		Short:   "send a dataset to a remote",
    17  		Aliases: []string{"publish"},
    18  		Long: `Push sends datasets to a remote qri node. A push updates the dataset log on the
    19  remote and sends one version of dataset data to the remote. To push multiple
    20  dataset versions, run push multiple times, specifying the version hash to push.
    21  
    22  If no remote is specified, qri pushes to the registry.`,
    23  		Example: `  # push a dataset to the registry
    24    $ qri push me/dataset
    25  
    26    # push a specific version of a dataset to the registry:
    27    $ qri push me/dataset@/ipfs/QmHashOfVersion`,
    28  		Annotations: map[string]string{
    29  			"group": "network",
    30  		},
    31  		Args: cobra.MaximumNArgs(1),
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			if err := o.Complete(f, args); err != nil {
    34  				return err
    35  			}
    36  			return o.Run()
    37  		},
    38  	}
    39  
    40  	cmd.Flags().BoolVarP(&o.Logs, "logs", "", false, "send only dataset history")
    41  	cmd.Flags().StringVarP(&o.Remote, "remote", "", "", "name of remote to push to")
    42  
    43  	return cmd
    44  }
    45  
    46  // PushOptions encapsulates state for the push command
    47  type PushOptions struct {
    48  	ioes.IOStreams
    49  
    50  	Refs   *RefSelect
    51  	Logs   bool
    52  	Remote string
    53  
    54  	inst *lib.Instance
    55  }
    56  
    57  // Complete adds any missing configuration that can only be added just before calling Run
    58  func (o *PushOptions) Complete(f Factory, args []string) (err error) {
    59  	if o.inst, err = f.Instance(); err != nil {
    60  		return err
    61  	}
    62  	if o.Refs, err = GetCurrentRefSelect(f, args, 1); err != nil {
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  // Run executes the push command
    69  func (o *PushOptions) Run() error {
    70  	ctx := context.TODO()
    71  	for _, ref := range o.Refs.RefList() {
    72  		p := lib.PushParams{
    73  			Ref:    ref,
    74  			Remote: o.Remote,
    75  		}
    76  
    77  		// Though push is pushing to a remote, it has to resolve datasets
    78  		// from your local collection.
    79  		res, err := o.inst.WithSource("local").Dataset().Push(ctx, &p)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		printInfo(o.Out, "pushed dataset %s", res)
    84  	}
    85  
    86  	return nil
    87  }