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

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/qri-io/ioes"
     8  	"github.com/qri-io/qri/lib"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // NewWhatChangedCommand creates a new `qri whatchanged` command that shows what changed at a commit
    13  func NewWhatChangedCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
    14  	o := &WhatChangedOptions{IOStreams: ioStreams}
    15  	cmd := &cobra.Command{
    16  		Use:    "whatchanged DATASET",
    17  		Hidden: true,
    18  		Short:  "shows what changed at a particular commit",
    19  		Long: `Shows what changed for components at a particular commit, that is, which
    20  were added, modified or removed.`,
    21  		Example: `  # Show what changed for the head commit
    22    $ qri whatchanged me/dataset_name`,
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			if err := o.Complete(f, args); err != nil {
    25  				return err
    26  			}
    27  			return o.Run()
    28  		},
    29  	}
    30  
    31  	return cmd
    32  }
    33  
    34  // WhatChangedOptions encapsulates state for the whatchanged command
    35  type WhatChangedOptions struct {
    36  	ioes.IOStreams
    37  
    38  	Instance *lib.Instance
    39  
    40  	Refs *RefSelect
    41  }
    42  
    43  // Complete adds any missing configuration that can only be added just before calling Run
    44  func (o *WhatChangedOptions) Complete(f Factory, args []string) (err error) {
    45  	if o.Instance, err = f.Instance(); err != nil {
    46  		return err
    47  	}
    48  	o.Refs, err = GetCurrentRefSelect(f, args, 1)
    49  	return err
    50  }
    51  
    52  // Run executes the whatchanged command
    53  func (o *WhatChangedOptions) Run() (err error) {
    54  	ctx := context.TODO()
    55  	inst := o.Instance
    56  
    57  	params := lib.WhatChangedParams{Ref: o.Refs.Ref()}
    58  	res, err := inst.Dataset().WhatChanged(ctx, &params)
    59  	if err != nil {
    60  		printErr(o.ErrOut, err)
    61  		return nil
    62  	}
    63  
    64  	for _, si := range res {
    65  		printInfo(o.Out, fmt.Sprintf("  %s: %s", si.Component, si.Type))
    66  	}
    67  
    68  	return nil
    69  }