github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/gist/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/andrewhsu/cli/v2/internal/config"
    10  	"github.com/andrewhsu/cli/v2/pkg/cmd/gist/shared"
    11  	"github.com/andrewhsu/cli/v2/pkg/cmdutil"
    12  	"github.com/andrewhsu/cli/v2/pkg/iostreams"
    13  	"github.com/andrewhsu/cli/v2/pkg/text"
    14  	"github.com/andrewhsu/cli/v2/utils"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type ListOptions struct {
    19  	IO         *iostreams.IOStreams
    20  	Config     func() (config.Config, error)
    21  	HttpClient func() (*http.Client, error)
    22  
    23  	Limit      int
    24  	Visibility string // all, secret, public
    25  }
    26  
    27  func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
    28  	opts := &ListOptions{
    29  		IO:         f.IOStreams,
    30  		Config:     f.Config,
    31  		HttpClient: f.HttpClient,
    32  	}
    33  
    34  	var flagPublic bool
    35  	var flagSecret bool
    36  
    37  	cmd := &cobra.Command{
    38  		Use:   "list",
    39  		Short: "List your gists",
    40  		Args:  cobra.NoArgs,
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			if opts.Limit < 1 {
    43  				return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
    44  			}
    45  
    46  			opts.Visibility = "all"
    47  			if flagSecret {
    48  				opts.Visibility = "secret"
    49  			} else if flagPublic {
    50  				opts.Visibility = "public"
    51  			}
    52  
    53  			if runF != nil {
    54  				return runF(opts)
    55  			}
    56  			return listRun(opts)
    57  		},
    58  	}
    59  
    60  	cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 10, "Maximum number of gists to fetch")
    61  	cmd.Flags().BoolVar(&flagPublic, "public", false, "Show only public gists")
    62  	cmd.Flags().BoolVar(&flagSecret, "secret", false, "Show only secret gists")
    63  
    64  	return cmd
    65  }
    66  
    67  func listRun(opts *ListOptions) error {
    68  	client, err := opts.HttpClient()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	cfg, err := opts.Config()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	host, err := cfg.DefaultHost()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	gists, err := shared.ListGists(client, host, opts.Limit, opts.Visibility)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	cs := opts.IO.ColorScheme()
    89  
    90  	tp := utils.NewTablePrinter(opts.IO)
    91  
    92  	for _, gist := range gists {
    93  		fileCount := len(gist.Files)
    94  
    95  		visibility := "public"
    96  		visColor := cs.Green
    97  		if !gist.Public {
    98  			visibility = "secret"
    99  			visColor = cs.Red
   100  		}
   101  
   102  		description := gist.Description
   103  		if description == "" {
   104  			for filename := range gist.Files {
   105  				if !strings.HasPrefix(filename, "gistfile") {
   106  					description = filename
   107  					break
   108  				}
   109  			}
   110  		}
   111  
   112  		gistTime := gist.UpdatedAt.Format(time.RFC3339)
   113  		if tp.IsTTY() {
   114  			gistTime = utils.FuzzyAgo(time.Since(gist.UpdatedAt))
   115  		}
   116  
   117  		tp.AddField(gist.ID, nil, nil)
   118  		tp.AddField(text.ReplaceExcessiveWhitespace(description), nil, cs.Bold)
   119  		tp.AddField(utils.Pluralize(fileCount, "file"), nil, nil)
   120  		tp.AddField(visibility, nil, visColor)
   121  		tp.AddField(gistTime, nil, cs.Gray)
   122  		tp.EndRow()
   123  	}
   124  
   125  	return tp.Render()
   126  }