github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/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/ungtb10d/cli/v2/internal/config"
    10  	"github.com/ungtb10d/cli/v2/internal/text"
    11  	"github.com/ungtb10d/cli/v2/pkg/cmd/gist/shared"
    12  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    13  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    14  	"github.com/ungtb10d/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  		Aliases: []string{"ls"},
    41  		Args:    cobra.NoArgs,
    42  		RunE: func(cmd *cobra.Command, args []string) error {
    43  			if opts.Limit < 1 {
    44  				return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit)
    45  			}
    46  
    47  			opts.Visibility = "all"
    48  			if flagSecret {
    49  				opts.Visibility = "secret"
    50  			} else if flagPublic {
    51  				opts.Visibility = "public"
    52  			}
    53  
    54  			if runF != nil {
    55  				return runF(opts)
    56  			}
    57  			return listRun(opts)
    58  		},
    59  	}
    60  
    61  	cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 10, "Maximum number of gists to fetch")
    62  	cmd.Flags().BoolVar(&flagPublic, "public", false, "Show only public gists")
    63  	cmd.Flags().BoolVar(&flagSecret, "secret", false, "Show only secret gists")
    64  
    65  	return cmd
    66  }
    67  
    68  func listRun(opts *ListOptions) error {
    69  	client, err := opts.HttpClient()
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	cfg, err := opts.Config()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	host, _ := cfg.DefaultHost()
    80  
    81  	gists, err := shared.ListGists(client, host, opts.Limit, opts.Visibility)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if len(gists) == 0 {
    87  		return cmdutil.NewNoResultsError("no gists found")
    88  	}
    89  
    90  	if err := opts.IO.StartPager(); err == nil {
    91  		defer opts.IO.StopPager()
    92  	} else {
    93  		fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
    94  	}
    95  
    96  	cs := opts.IO.ColorScheme()
    97  
    98  	//nolint:staticcheck // SA1019: utils.NewTablePrinter is deprecated: use internal/tableprinter
    99  	tp := utils.NewTablePrinter(opts.IO)
   100  
   101  	for _, gist := range gists {
   102  		fileCount := len(gist.Files)
   103  
   104  		visibility := "public"
   105  		visColor := cs.Green
   106  		if !gist.Public {
   107  			visibility = "secret"
   108  			visColor = cs.Red
   109  		}
   110  
   111  		description := gist.Description
   112  		if description == "" {
   113  			for filename := range gist.Files {
   114  				if !strings.HasPrefix(filename, "gistfile") {
   115  					description = filename
   116  					break
   117  				}
   118  			}
   119  		}
   120  
   121  		gistTime := gist.UpdatedAt.Format(time.RFC3339)
   122  		if tp.IsTTY() {
   123  			gistTime = text.FuzzyAgo(time.Now(), gist.UpdatedAt)
   124  		}
   125  
   126  		tp.AddField(gist.ID, nil, nil)
   127  		tp.AddField(text.RemoveExcessiveWhitespace(description), nil, cs.Bold)
   128  		tp.AddField(text.Pluralize(fileCount, "file"), nil, nil)
   129  		tp.AddField(visibility, nil, visColor)
   130  		tp.AddField(gistTime, nil, cs.Gray)
   131  		tp.EndRow()
   132  	}
   133  
   134  	return tp.Render()
   135  }